MathJax v4: TeX to MathML






This example shows how to produce MathML output from MathJax's TeX input, without the need for a native MathML output jax. We do this by defining a new renderAction that replaces the usual output jax's typesetting operation; its job is to create the usual mjx-container node and set its innerHTML to the serialzied version of the internal MathML produced by MathJax.

We use MathJax.startup.document.convertPromise, the function that underlies the functions like `MathJax.tex2mmlPromise()` in order to obtain the container node, and when the promise resolves, we simply insert that container into the page and tell the menu code that it is there (since we don't have an output jax to do that for us). Using the promise-based convertion function allows for the possibility that they use \require to load extensions dynamically, or one is loaded automatically by the autoload extension.

When the user presses the Render TeX button or switches the display checkbox or one of the other controls, the Interactive.convert() function runs. The comments in the code explain how the conversion process is handled. Note that the user interface is disabled during the typesetting process, since the conversion is done asynchronously in this example. This prevents the user from starting a new typeset operation while one is already in progress.

Most browser only implement MathML-Core, a limited version of MathML that doesn't include all the features that MathJax uses. In particular, MathML-Core doesn't include the mathvariant attribute that MathJax uses to implement commands like `\mathbf`, `\mathbb`, and so on, so using MathJax MathML output directly may mean that some font variants may not show up properly here. The Convert mathvariants checkbox will enable code that converts the characters within elements that use mathvariant attributes into their counterparts in the Math Alphanumerics uncide block, as required by MathML-Core. Note, however, that not all characters have such counterparts, so you may not be able to generate a bold plus sign or an italic number, for example, as there are no unicode equivalents for those. When this occurs, we add a style attribute as a fall-back to get the proper variant, when possible.

The code for doing the mathvariant conversion is in a separate mml-core.js script.