#3978·KaTeX

Using underset does not render valid MathML

Author: spockeCreated Sep 6, 2024Updated Aug 30, 2026
Labelsbug

Before reporting a bug

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:

  1. 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 math element to see if they are all MathML namespace elements.
  2. Check that the red status message appear for \\\underset formulas so rendering that to MathML produces invalid markup and thus causing some elements like underset and mo to become HTML elements.
  3. Try different formulas without \\\underset and 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