Using underset does not render valid MathML
Author: spockeCreated Sep 6, 2024Updated Aug 30, 2026
Labelsbug
Before reporting a bug
- Check common issues.
- Check the bug is reproducible in the demo. If not, check KaTeX is up-to-date and installed correctly.
- Search for existing issues.
Describe the bug: Using underset renders a MathML structure that is not valid so some elements become HTML elements that causes issues with sanitizers like dompurify that prohibits html elements from existing inside a mathml namespace.
To Reproduce: Steps to reproduce the behavior:
- Go to https://codepen.io/spocke78/pen/zYVyzJP?editors=1010 this takes a tex formula converts it to MathML then runs a check on the elements inside the
mathelement to see if they are all MathML namespace elements. - Check that the red status message appear for
\\\undersetformulas so rendering that to MathML produces invalid markup and thus causing some elements likeundersetandmoto become HTML elements. - Try different formulas without
\\\undersetand notice that they pass meaning they don't produce anything other than MathML elements.
Expected behavior:
The mathml output option should produce valid MathML.
I expect the output for \\\underset ab to be something like:
<math xmlns="http://www.w3.org/1998/Math/MathML">
<semantics>
<mrow>
<munder>
<mi>b</mi>
<mi>a</mi>
</munder>
</mrow>
<annotation encoding="application/x-tex">\\\underset{a}{b}</annotation>
</semantics>
</math>Instead we get this incorrect output:
<math xmlns="http://www.w3.org/1998/Math/MathML">
<semantics>
<mrow>
<mi>
<munder>
<mo>
<mi>b</mi>
</mo>
<mi>a</mi>
</munder>
</mi>
</mrow>
<annotation encoding="application/x-tex">\\\underset ab</annotation>
</semantics>
</math>Environment (please complete the following information):
- KaTeX Version: 0.16.11
- Device: Desktop
- OS: MacOS
- Browser: Chrome, Firefox, Safari
Source: KaTeX/KaTeX