Can anyone help me understand how to call MathJax methods in JupyterLab 4?

Background: My Python+Javascripåt library JupyterQuiz generates interactive quizzes that can include LaTeX, but I need to call the appropriate method to tell MathJax to typeset the LaTeX code, because it is loaded by Javascript. For MathJax 3, the usual way to do this to to pass the element to MathJax.typeset(), but the MathJax global object in JupyterLab 4+ does not have the appropriate methods – it still looks like the pre-loading version of the MathJax object.

Reference: From the MathJax 3 docs: “The global variable MathJax is used to store the configuration for MathJax. Once MathJax is loaded, however, MathJax changes the MathJax variable to contain the various methods needed to control MathJax.”

It seems that the MathJax variable never gets updated in JupyterLab 4.

Any help would be greatly appreciated!

In Jupyter extension ecosystem ILatexTypesetter interface exposes the typeset(node: HTMLElement) method for extensions; instead of using the MathJax directly (which is an implementation details and the user may have either a version 2 or 3 or 4 installed, or even no mathjax at all but katex instead) I would recommend using ILatexTypesetter. This is a token exposed by jupyterlab/rendermime so you would do something like:

import { ILatexTypesetter } from '@jupyterlab/rendermime';

export const plugin: JupyterFrontEndPlugin<void> = {
  id: '@my-org/my-extension',
  requires: [ILatexTypesetter],
  activate: (app: JupyterFrontEnd, latexTypesetter: ILatexTypesetter) => {
    console.log('latex typesetter instance:', latexTypesetter);
    latexTypesetter.typeset(yourHTMLNode);
  }
}

Thank you for the reply!

I’m not sure I understand your answer, however. If I am misunderstanding, please let me know.

I really, really do not want users to have to install an extension in order to use my library. The JupyterQuiz quiz generator embeds everything needed into the output cells, so that the same output works in both JupyterLab and in the HTML output of Jupyter Book. Users can even load a notebook with an embedded quiz without even having the JupyterQuiz library installed. LaTeX rendering worked up until JupyterLab 4, and I took care of the details of whether to call MathJax 2 or MathJax 3 typesetting commands.

In JupyterLab 4, the MathJax variable is initialized, but then it never finishes the initialization because the usual MathJax methods (particularly MathJax.typeset()) are not available. I don’t understand why that is the case. I guess one option is for my code to call whatever methods are needed to get MathJax to load properly?

I really, really do not want users to have to install an extension in order to use my library

That’s a fine design constrain. Just FYI the frontend extensions can now be installed via pip/conda so it is not that obtrusive for users anymore.

Users can even load a notebook with an embedded quiz without even having the JupyterQuiz library installed.

In that case I would suggest that you open an enhancement request on JupyterLab issue tracker explaining on a minimal reproducible example, how the equations in outputs are not typeset and suggesting JupyterLab auto-detects these and typesets them accordingly.

LaTeX rendering worked up until JupyterLab 4, and I took care of the details of whether to call MathJax 2 or MathJax 3 typesetting commands.

As far as I know this was never the encoured nor supported way of typesetting. This goes both ways: extension developers should not play with implementation details (or at least not expect these to be the same across major releases), and extension developers should not need to have to resort to playing with implementation details. JupyterLab should be able to do better for you :slight_smile:

In JupyterLab 4, the MathJax variable is initialized, but then it never finishes the initialization because the usual MathJax methods (particularly MathJax.typeset()) are not available. I don’t understand why that is the case.

JupyterLab does not ship a full MathJax package but a repackaged version which tailors to its use case and the initialization path may be different than the one described in MathJax’s documentation. Feel free to explore the code for details - it’s all open source.

I guess one option is for my code to call whatever methods are needed to get MathJax to load properly?

No, please don’t do that (see above).

1 Like

Thanks for your guidance. I have opened an issue here: Provide method to typeset dynamically generated math content · Issue #15614 · jupyterlab/jupyterlab · GitHub