Add a data-mathml attribute to each math element (original) (raw)
This math will have a data-mathml attribute containing the MathML representation of the expression, and adata-original attribute holding the original TeX format of the expression. The results are shown below the typeset expression. The semantic-enrichment and speech attributes are removed for easier viewing. Note that <,> and & are encoded as entities within the attribute values when the tag is serialized, butnode.getAttribute('data-mathml') would give a string where the entities would be replaced by their usual characters.
\sqrt{\frac{1-x}{1+x}}$$
This example shows how to automatically generate adata-mathml attribute on the HTML output for each math expression that contains the serialized MathML version of the expression. It also adds a data-original attribute that gives the original form of the expression (i.e., the original TeX or MathML markup). These attributes could be picked up by other tools that may need access to the mathematics after it has been converted to HTML or SVG tags, for example.
The process uses the startup.ready() function to do the normal startup, then attaches a post-filter to the output jax that looks up the original format of the math, sets thatdata-original attribute to that, and uses thestartup.toMML() function to set thedata-mathml attribute to the serialized MathML for the expression using the internal MathML representation generated by MathJax. Some attributes are removed to simplify the structure.
Finally, we wait for the initial typesetting to comple using theMathJax.startup.promise, and when it dos, we transfer themjx-container node to the output area, removing some attributes and showing each attribute on a separate line for clarity.