Search code examples
xsltlatextransformationmathml

Convert MathML to Latex


Inspired by this post, I tried to convert the MathML example, which looks like this:

<math>
  <mrow>
    <mfrac>
      <mrow><mi>x</mi></mrow>
      <mrow><mi>y</mi></mrow>
    </mfrac>
  </mrow>
</math>

to Latex using xsltproc. First I downloaded the XSLT files from here. Now, for those of use who don't speak MathML, the above example is a fraction: 1/2

After I've unzipped the xslt file and I created the MathML file (input.mml) I have the following files:

$ ls
README  cmarkup.xsl  entities.xsl  glayout.xsl  input.mml  mmltex.xsl  scripts.xsl  tables.xsl  tokens.xsl

So, when I do

$> xsltproc mmltex.xsl input.mml




                    x


                    y

Which is not correct, its not a Latex fraction. Any suggestions what I might be doing wrong here ?


Solution

  • Only getting the text content as the result of a transformation very often indicates that none of the templates in the stylesheet were applied. Then, buil-in templates step in and skip everything except for text nodes, which are sent to output.

    Looking at the stylesheets you use, the stylesheets assume that the MathML input is in a namespace. For example, look at this template match I have taken from the mmmltext.xsl stylesheet:

    match="m:math[not(@mode) or @mode='inline'][not(@display)] | m:math[@display='inline']">
    

    As you can see, the elements are prefixed with m:, and, more interestingly, m: is declared as

    xmlns:m="http://www.w3.org/1998/Math/MathML"
    

    On the other hand, the input file you show is not in any namespace. Try the following input file:

    <math xmlns="http://www.w3.org/1998/Math/MathML">
      <mrow>
        <mfrac>
          <mrow><mi>x</mi></mrow>
          <mrow><mi>y</mi></mrow>
        </mfrac>
      </mrow>
    </math>
    

    In the XML document above, I have declared a default namespace on the math element. All of its child elements also take on this namespace.

    Then, the output contains:

    \frac{x}{y}
    

    As the W3C page says:

    Typical usage [of this namespace] would be:

    <math xmlns="http://www.w3.org/1998/Math/MathML">