4

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 ?

2 Answers 2

5

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">

Sign up to request clarification or add additional context in comments.

Comments

2

I was looking for a quick way to achieve this online.

The below is a link to an npm package that converts MathML to LateX. https://npm.runkit.com/mathml-to-latex

Simply copy, paste and run

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.