# converting Mathml to LaTeX in python programming

I am writing one educational software right now. It's kind of online test for school students. I add up the questions using my editor, I need the math functionality therefore I need to use this editor, other editors are very simple and not that smart and powerful like this editor.

But , The editor gives me output into Mathml, now this Mathml also includes style tags along with the main text. Therefore I do not find it feasible to store into the databases. Therefore I am trying to convert the Mathml to LaTeX.

As I am developing the project in Python Django, So I need a Python Library that can do the conversion for me. I have searched many libraries but they just convert the text to Mathml or convert the text to LaTeX. If anyone know about this please let me know.

The xslt at https://github.com/davidcarlisle/web-xslt/tree/master/pmml2tex makes a reasonable job of making tex from mathml for print It is used to generate many of the sample images in the MathML spec, and the PDF version of the spec, which is generated via latex.

However it isn't really designed to generate archival or editable latex so the tex markup it generates is a mess.

Still it might give you are start, although to be honest the reasons for needing latex rather than mathml were not that clear.

• Hello David Carlisle for the early reply , I am making an editor that would let the teacher create questions and its respective answers. Now for mathematics formulae I need to use Wiris. But it generates lots of style plus mathml tags along with the text specified, but this text is troublesome for storing in db and even manipulating on the web pages and Wiris is the best editor so far I found that is matching my needs, therefore I thinking to transform the text into latex and store in db and when I need the data I would simply show on pages using mathjax – Chitrank Dixit Jul 13 '15 at 18:38

You can try doing it like mathconverter does:

import os
from lxml import etree

def mathml2latex_yarosh(equation):
""" MathML to LaTeX conversion with XSLT from Vasil Yaroshevich """
xslt_file = os.path.join('mathconverter', 'xsl_yarosh', 'mmltex.xsl')
dom = etree.fromstring(equation)
xslt = etree.parse(xslt_file)
transform = etree.XSLT(xslt)
newdom = transform(dom)
return unicode(newdom)

mathml = """<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<mfrac>
<mrow><mi>x</mi></mrow>
<mrow><mi>y</mi></mrow>
</mfrac>
</mrow>
[/itex]"""
tex = mathml2latex_yarosh(mathml)
print tex

Result is $\frac{x}{y}$.

I'm a bit late to the party, but you might want to consider using ConTeXt for MathML input as it is supported out-of-the-box.

\usemodule[mathml]

\starttext

\xmlprocessdata{}{
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<mfrac>
<mrow><mi>x</mi></mrow>
<mrow><mi>y</mi></mrow>
</mfrac>
</mrow>
[/itex]
}{}

\stoptext