# Configure MathJax Macros on macOS

**URL:** <https://forums.ankiweb.net/t/configure-mathjax-macros-on-macos/6535>\
**Category:** Help\
**Created:** [January 15, 2021, 1:30am UTC](https://forums.ankiweb.net/t/configure-mathjax-macros-on-macos/6535 "2021-01-15T01:30:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tansongchen](https://avatars.discourse-cdn.com/v4/letter/t/b38774/32.png) [@tansongchen](https://forums.ankiweb.net/u/tansongchen)\
**Post date:** [January 15, 2021, 1:30am UTC](https://forums.ankiweb.net/t/configure-mathjax-macros-on-macos/6535/1 "2021-01-15T01:30:54Z")

</div>

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](https://www.reddit.com/r/Anki/comments/btqz3m/custom_mathjax_macros_on_desktopandroid/) I add this script at the beginning of both the front side and the back side in the card template:

```auto
<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](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/4/40ad3ead8daf0e158607b7b8e2c8b9cb6379a112.png)

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

---

<div class="post-metadata">

**Author:** ![hengiesel](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/hengiesel/32/920_2.png) [@hengiesel](https://forums.ankiweb.net/u/hengiesel)\
**Post date:** [January 15, 2021, 11:55am UTC](https://forums.ankiweb.net/t/configure-mathjax-macros-on-macos/6535/2 "2021-01-15T11:55:22Z")

</div>

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](http://docs.mathjax.org/en/latest/web/configuration.html#configuring-mathjax-after-it-is-loaded), and [this section on configuring macros](http://docs.mathjax.org/en/latest/input/tex/macros.html).

---

<div class="post-metadata">

**Author:** ![hengiesel](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/hengiesel/32/920_2.png) [@hengiesel](https://forums.ankiweb.net/u/hengiesel)\
**Post date:** [January 15, 2021, 1:09pm UTC](https://forums.ankiweb.net/t/configure-mathjax-macros-on-macos/6535/3 "2021-01-15T13:09:44Z")

</div>

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:

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

```

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

---

<div class="post-metadata">

**Author:** ![hkr](https://avatars.discourse-cdn.com/v4/letter/h/c6cbf5/32.png) [@hkr](https://forums.ankiweb.net/u/hkr)\
**Post date:** [January 15, 2021, 3:27pm UTC](https://forums.ankiweb.net/t/configure-mathjax-macros-on-macos/6535/4 "2021-01-15T15:27:13Z")

</div>

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

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

```

 ![mathjax3_macro](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/f/fcfeb4f25d470c4679f3fd4bc7895debf13c5c4b.png)

---

<div class="post-metadata">

**Author:** ![tansongchen](https://avatars.discourse-cdn.com/v4/letter/t/b38774/32.png) [@tansongchen](https://forums.ankiweb.net/u/tansongchen)\
**Post date:** [January 16, 2021, 1:14pm UTC](https://forums.ankiweb.net/t/configure-mathjax-macros-on-macos/6535/5 "2021-01-16T13:14:57Z")

</div>

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

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

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.

---

<div class="post-metadata">

**Author:** ![hkr](https://avatars.discourse-cdn.com/v4/letter/h/c6cbf5/32.png) [@hkr](https://forums.ankiweb.net/u/hkr)\
**Post date:** [January 17, 2021, 7:54pm UTC](https://forums.ankiweb.net/t/configure-mathjax-macros-on-macos/6535/6 "2021-01-17T19:54:19Z")

</div>

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.

```auto
<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>

```

---

<div class="post-metadata">

**Author:** ![hengiesel](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/hengiesel/32/920_2.png) [@hengiesel](https://forums.ankiweb.net/u/hengiesel)\
**Post date:** [January 17, 2021, 10:45pm UTC](https://forums.ankiweb.net/t/configure-mathjax-macros-on-macos/6535/7 "2021-01-17T22:45:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [January 18, 2021, 2:04am UTC](https://forums.ankiweb.net/t/configure-mathjax-macros-on-macos/6535/8 "2021-01-18T02:04:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://forums.ankiweb.net/u/Hugo)\
**Post date:** [March 24, 2021, 7:15pm UTC](https://forums.ankiweb.net/t/configure-mathjax-macros-on-macos/6535/9 "2021-03-24T19:15:40Z")

</div>

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](http://docs.mathjax.org/en/latest/input/tex/macros.html) 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 : )

---

<div class="post-metadata">

**Author:** ![Kyle](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@Kyle](https://forums.ankiweb.net/u/Kyle)\
**Post date:** [April 18, 2021, 10:17pm UTC](https://forums.ankiweb.net/t/configure-mathjax-macros-on-macos/6535/10 "2021-04-18T22:17:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [May 1, 2023, 5:07am UTC](https://forums.ankiweb.net/t/configure-mathjax-macros-on-macos/6535/11 "2023-05-01T05:07:02Z")

</div>


