MathJax doesn't load macros on card that doesn't have definitions

Description

I have a note template with MathJax macros defined on the first card. I have generally found that this is fine for getting the macros to be available on all the other cards of the template… and sometimes it is (or it will load specific macros but not all or properly). However, I have some interesting behaviour.

Steps to Reproduce

  1. (I did this in a brand new profile)
  2. Create a note template with multiple cards (e.g. Basic (and reversed) copy)
  3. Add a card to some deck with \[\dd{f}{x}\ on the front and \[\dd{f}{x} = f'(x)\] on the back.
  4. Hit Browse and make sure you are viewing Notes
  5. On the card you created in (2), select Cards and check you are viewing card type 1.
  6. Set the front template of card type 1 to
<script>
MathJax.config.tex["macros"] = {
	dd: ["\\frac{ {\\mathrm{d}}#1}{ {\\mathrm{d}}#2}", 2]
};
if (typeof is_already_run == "undefined") {
	is_already_run = true;
	MathJax.startup.getComponents();
}
</script>
{{Front}}
  1. Hit save and re-view Cards. It should render fine for both front and back previews in both card type 1 and card type 2.
  2. Close Cards and switch the browser to view Cards. Open Card 2 of the note with its Cards button.
  3. Things are broken:
    • on card type 2, both front and back preview show red for undefined MathJax functions
    • switching to card type 1, only the mid-bar of the fraction is rendered
    • back to card type 2, only the mid-bar
    • close Cards and open Card 2’s Preview, shows undefined MathJax
    • close browser and study the new cards, Card 1 renders fine (grade it Good) and Card 2 renders fine (do NOT grade it)
    • go back to Decks and study again, Card 2 render with red undefined MathJax (grade it Again) and Card 1 only renders the fraction’s mid-bar

Screenshots of a more complex example:

Same setup: macros defined on card type 1, starting with viewing card type 2.

Card type 2 back preview:

Card type 1 back preview:

Card type 2 back preview:

Anki Version

  • Version ⁨23.12.1 (1a1d4d54)⁩
  • Python 3.9.15 Qt 6.5.3 PyQt 6.5.3

Screenshot of loading card type 1 first by browsing with Notes view:

Apologies if I’ve misunderstood you. If you’re saying you want the macros to be available on both card template 1 and 2, you need to place them in the front template of both 1 and 2.

Fair enough. Though the fact that it works if you first see card template 1 then 2 I feel suggests you would only need it on one side; I don’t know what the manual says about this.

Also, it would certainly be convenient if there were some magic way of having the macros in one place and just always loading them when needed. I have two note types that use the same macros so keeping track of the code for all templates on all notes quickly becomes annoying.

If I understand you correctly, you can put them in a JavaScript file and import it in your notetype template.

For example I put my predefined macros in _MathJax.js, lying in collection.media. Then I use <script src="_MathJax.js"></script> to import it. Whenever I want to modify my macros or MathJax settings, I just need to change the JavaScript file instead of tracking all notetypes including them.

MathJax.config.tex['macros'] = {
    ...
}
MathJax.startup.getComponents();

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.