1

I want to use Mathjax on my website. I put in the <head> section of public/index.html:

<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>

And, in my component :

<template>
    <div v-html="note_content"></div>
</template>

<script>
import { typeset, dummy_typeset } from '../assets/js/text_processing.js';
import showdown from 'showdown';

const converter = new showdown.Converter({ tables: true });

export default {
    data () {
        return {
        }
    },
    computed: {
        note_content: function() {
            return typeset(this.exchange_data);
        }
    },
    props: ['exchange_data'],
    watch: {
        note_content: function() {
            Mathjax.typesetPromise();
        }
    }
}
</script>

However on runtime, I get the error :

Uncaught (in promise) ReferenceError: Mathjax is not defined

What I do not understand is that the css which is located in the head of public/index.html is correctly loaded and rendered in the component. Also I read elsewhere that loading external javascript this way should make it available to all components. What is wrong ?

3
  • have you tried without the async attribute? Commented Sep 14, 2021 at 8:04
  • yes, still get Mathjax not defined. Commented Sep 14, 2021 at 8:28
  • What browser are you using? I'm having a comparable problem on Firefox. It might be a Firefox-compatibility issue: github.com/mathjax/MathJax/issues/2399 Commented Mar 26, 2022 at 23:43

2 Answers 2

1

I think it is connected to webpack config, but i might be wrong. Anyway, have you tried this method?

How to add external JS scripts to VueJS Components?

It enforces the script to be loaded.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried to put the mathjax javascript in the src/assets directory and then load it using import '../assets/ext/polyfill.min.js?features=es6'; and import '../assets/ext/tex-mml-chtml.js'; . I still gt the error that Mathjax is undefined, but now the imports are being processed.
Since I changed my approach I created a new question: stackoverflow.com/questions/69215530/…
Thank to your answer I understood that public is not processed by webpack and I am moving resources into src/assets.
I'm happy it worked out. Good luck with the project!
I found the bug, this is ridiculous.
0

Contrarily to what is written at https://docs.mathjax.org/en/latest/web/typeset.html, the syntax for asynchronous rendering is not :

Mathjax.typesetPromise()

But (notice the case):

MathJax.typesetPromise()

The typo could be detected from the surrounding context.

Also not that to use typeset Mathjax in a Vue component, the virtual DOM should be processed before calling the typesetPromise method, so an example of working code would be:

watch: {
    note_content: function() {
        this.$nextTick(MathJax.typesetPromise);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.