Anki Forums

Configure MathJax Macros on macOS

I am using the built-in MathJax support to render math for a while, on both macOS desktop and iOS mobile application. Recently I would like to introduce several shortcuts, so following this I add this script at the beginning of both the front side and the back side in the card template:

<script type="text/x-mathjax-config">
MathJax.Hub.Config({  
    TeX: {
        Macros: {
            R: ['{\\mathbb {R}}']
        }
    },
});
MathJax.Hub.Configured();  
</script>

But after this typing \R in math environment doesn’t give the correct render result, and instead gives

image

What could potentially be the problem? I am using Anki 2.1.37 on macOS 11.1.

Since that post, MathJax was updated to MathJax 3, which is much, much faster than the previous MathJax 2.
However this also changed how to configure MathJax.

For more information, see this section on configuring MathJax 3 after it’s been loaded, and this section on configuring macros.

I just tested it out, and I found one way which certainly works:

Define the macros with MathJax itself, by putting this into the card template, and before the cards main content:

\(
\def \R \\mathbb{R}
\)

Disclaimer: I was involved in updating to MathJax 3. I will look further into the configurability.

3 Likes

As an alternative, according to the links @hengiesel provided, macros can also be defined with a script like the following:

<script>
    MathJax.config.tex['macros'] = {
        R: '{\\mathbb {R}}',
    };
    MathJax.startup.getComponents();
</script>

3 Likes

Thanks for your answers. I tested on macOS and iOS and found @hengiesel 's answer works on both platforms. Using JavaScript like

however, doesn’t work on iOS, and I think it’s a problem related to different rendering mechanisms or strategies. Nevertheless, doing \def is robust.

I suggest this solution be added to the Anki documentation to prevent further confusion. I will try to figure out how to do this later, or you can give me some clues about how to do this.

I don’t have an iOS device, so I don’t know anything about AnkiMobile. This is just a guess, but if the version of MathJax bundled with AnkiMobile is 2.x, the following script might work.

<script>
    if (typeof MathJax !== 'undefined') {
        const jaxVer = MathJax.version.split('.')[0];
        if (jaxVer === '3') {
            MathJax.config.tex['macros'] = {
                R: '\\mathbb {R}',
            };
            MathJax.startup.getComponents();
        } else if (jaxVer === '2') {
            MathJax.Hub.Config({
                TeX: {
                    Macros: {
                        R: '\\mathbb {R}',
                    },
                },
            });
            MathJax.Hub.Configured();
        }
    }
</script>

Note that AnkiDroid 2.15, which is currently in its Alpha version will also update to MathJax 3, at which point the different Anki implementations will have the same MathJax version again.

1 Like

AnkiMobile’s on MathJax 3 already. I’ve made a note to look into why the JS approach is not working, but in this case the other approach looks simpler.

1 Like

This answer really helped me. Though, for reference, my code only worked with curly brackets and without double backslash:

(
\def \R {\mathbb{R}}
)

Furthermore, the docs seem to say that it should be:

(
\def \R {{\mathbb{R} }}
)

This gives me an error. Perhaps I’m missing something? Again, my code is working for me so I’m happy : )

For posterity, the double bracket solution works, you just have to separate them with a space,
(
\def \R { {\mathbb{R} } }
)

1 Like