MathJax v4: TeX input and MathML Output

Note that this example requires a browser with native MathML support.

When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are $$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$

This example shows how to process a complete HTML page containing TeX notation into math in MathML format, illustrating one way to get a replacement for the version 2 NativeMML output, which is not included in versions 3 or 4 of MathJax.

The code below sets up a new renderAction that replaces the usual typeset one (due to it being named typeset with priority 150). This new action uses the MathJax.startup.toMML() function to convert the internal math items into serialized MathML, and then inserts the results into mjx-container elements that it sets as the typesetRoot of the math items. This will be put into the page automatically by a later renderAction that updates the page, one of the default actions.

The first <style> element sets up CSS so that the mjx-container will be set as a separate line with space above and below it when the math is a displayed equation (usually set up by th eoutput jax, but we don't have one). The second <style> element is used to store the styles needed by the expression explorer, which is set up below.

The ready() function in the startup section first does the usual ready action (setting up the document, theinput and output jax, etc.), and then it gets the styles needed for the explorer and puts them into the <style> element described above.

The pageReady() function renders the page when it first becomes available. This is needed because there is no MathJax.typeset() or MathJax.typesetPromise(), as no output jax is loaded, so the startup code does not create these two functions.

Finally, the menu item for the assistive MathML extension is turned off (to prevent it from loading), and the ready() function is used to disable its menu item so it can’t be turned on. Since we are producing MathML directly, there is no need for it.

Note that most modern browser implement a limited subset of MathML called MathML-Core. MathJax produces MathML3, but MathML-Core does not include a number of the features that MathJax uses. In particular, the mathvariant attribute is not supported (except on <mi> elements, and then only mathvariant="normal" is allowed. MathJax uses mathvariant to handle characgter variants like those produced \mathbb and similar macros, and those will not display properly in browser that only support MathML-Core if this example code is used. More sophisticated MathML conversion is available in the tex2mml with user input example.