# Relationship between MathML and TeX

What is the relationship, if any, between Tex and MathML?

I am interested in representing mathematical information generally, but is TeX going to be a legacy format going forward? Is there any attempt to automatically transform one into the other? Is MathML obviously going to replace TeX in the near future? Essentially I am in a position where I need a math markup language, I am writing a math editor, and I am free to choose either (or both, but that represents twice the work AFAIK) as suits me. Is TeX in theory any more powerful than MathML? I mean more complete in terms of underlying markup capabilities, of the class of things it can represent?

Meta this - I understand that this question might give offense only because whenever there are two different technologies that have the same aim they are sometimes thought of as "rivals" or "competitors" and have their respective "camps". Please know I am a total newbie in this area and in no way am I making a judgement about the worthiness of either technology. I am really just trying to understand how practitioners understand their own world.

-
Have you already found the texmath Haskell library? I can be used for LaTeX to MathML conversion and tried out online. –  sr_ May 29 '12 at 9:06
awesome thank you! –  John Thompson May 29 '12 at 9:53
They have a good comparison on wikipedia here –  BlueRaja - Danny Pflughoeft May 29 '12 at 16:23

MathML is very verbose and is a pain to write manually, which is no problem if your code is automatically generated. For programs it is easy to parse, since it is XML.

TeX math code was designed to be written by humans and results in much shorter code. I cannot answer your question if one format will replace the other. As I mentioned they serve different purposes.

Is there any attempt to automatically transform one into the other that anyone is aware of?

I cannot speak for LaTeX, but in ConTeXt you can input MathML directly. Information about MathML in ConTeXt and the Wiki - MathML. Here is an example (credits to Hans Hagen) taken from one of the ConTeXt manuals:

\usemodule[mathml]
\starttext
\xmlprocessdata{}{
<?xml version="1.0" encoding="UTF-8"?>
<math xmlns="http://www.w3c.org/mathml" version="2.0">
<apply>
<eq/>
<apply>
<diff/>
<bvar>
<ci> x </ci>
</bvar>
<apply>
<root/>
<ci> u </ci>
</apply>
</apply>
<apply>
<times/>
<apply>
<divide/>
<cn> 1 </cn>
<apply>
<times/>
<cn> 2 </cn>
<apply>
<root/>
<ci> u </ci>
</apply>
</apply>
</apply>
<apply>
<diff/>
<bvar>
<ci> x </ci>
</bvar>
<ci> u </ci>
</apply>
</apply>
</apply>
[/itex]
}{}
\stoptext


The output:

For a TeX to MathML conversion, you can use the XML export, see ConTeXt wiki - epub and my answer to the question Converting ConTeXt document to HTML.

-
thanks so much for the links –  John Thompson May 29 '12 at 9:54

I'd like to quote, with kind permission of the original author, the text of an article about LaTeX and MathML from access2science. The aim of the website is to provide "articles and links on accessibility of science, technology, engineering, and math (STEM). Its purpose is to provide practical information to people with print disabilities and to their friends, parents, and teachers.". The representation of mathematics in a consistent and standardised way is very important to the community that this website serves. The article originated as a post to the blindmath mailing list in response to a similar query about the roles of LaTeX and MathML.

There seems to be some confusion regarding LaTeX and MathML here. I'd like to help straighten that out, if I may. The confusion is with regarding their roles.

LaTeX is an input format. It is how we mathematicians write our articles, books, webpages, and anything else where mathematics is involved. (And often anything where mathematics isn't involved.) It is not designed to be read as-is. It is intended to be processed into a suitable output format and then read.

If anyone thinks that they can read LaTeX and understand what is going on, then I have a few documents I can post samples from which will soon disabuse you of that notion. Of course, very simple LaTeX can be read. Something like x^2 + y^2 = z^2 is fairly easy to understand, but try something more complicated like

\sum_{m = 2 \over m \text{prime}}^{\infty} \frac{1}{m^s}


and you'll see what I mean. And that's fairly simple compared to what can be written. When you realize that LaTeX (or rather, TeX) is completely programmable, then you'll see that you can find absolutely anything in a LaTeX document.

MathML is an output format. It is not designed to be written directly, but it is designed to be read. Of course, one needs a suitable renderer: a browser for the sighted and something like MathPlayer for those who want their mathematics read aloud, but then the same is true of any output format. As it is an open standard, it is a reasonable task to design a program to render MathML in to any desired medium.

It is possible, though not always straightforward, to convert LaTeX to MathML. One reason why it is not always straightforward is that TeX (the program underlying LaTeX) often needs to know things about its output. When run normally, TeX has complete control over the process and so can know exactly how the output will be seen. When producing MathML (or XHTML), it can't know exactly how the output will be seen. But those are technical difficulties that can usually be avoided.

The main difficulty is that most websites don't bother with this route. They convert the LaTeX mathematics to a graphic which is then displayed, with the original LaTeX as the alt text. Because of how it is produced, the LaTeX is usually very simple (no complicated macros), and so it may be possible to get by with reading the alt text.

So if you want to read mathematics, look for MathML. If you want to write mathematics, learn LaTeX (or another TeX variant).

Now, to (some of) your questions.

Is MathML obviously going to replace TeX in the near future?

No, because they fulfil different roles. I use LaTeX to produce MathML documents. I couldn't do without either of LaTeX or MathML in my workflow.

Is TeX in theory any more powerful than MathML? I mean more complete in terms of underlying markup capabilities, of the class of things it can represent?

Yes. TeX is a programming language. One of its strengths is the extent to which it can be customised and extended. MathML is a markup language. It is thus rather restricted when it comes to extending and customising it.

Meta this-I understand that this question might give offense only because whenever there are two different technologies that have the same aim they are sometimes thought of as "rivals" or "competitors" and have their respective "camps". Please know I am a total newbie in this area and in no way am I making a judgement about the worthiness of either technology. I am really just trying to understand how practitioners understand their own world.

The point of the article I quoted is that these technologies are in no ways rivals. If you wanted to invent a rivalry here, it would be better to play off PDF and MathML, or TeX and ... well, there isn't really anything like TeX.

To answer your real question: which format you should use, the answer depends on what you are going to do with it. What is the eventual output of the editor? Are you going to run the output through TeX to produce nice documents, or will it end up on a webpage? Is the user ever going to see the stored document? Once a document has been written, how much flexibility are you going to allow the user to have?

My instinct would be that if you don't know, you should pick MathML. My reason being that as it is a markup format and a web standard, it will be easier to work with in the program and easier to ensure that you know exactly what the editor will produce for the given input.

-
FWIW, ConTeXt can parse TeX and generate MathML, but it generates presentation MathML and not content MathML. There are a few helper macros to remove ambiguity. So, if you have a link to few hard to parse TeX examples, I'd be curious to test them out with the ConTeXt parser and see how they behave. –  Aditya May 29 '12 at 13:30
@Aditya The original article was explaining why it was better to use MathML in webpages rather than raw LaTeX code, so the point wasn't that it was difficult for TeX to parse the code, but for a human to read the raw TeX and understand what will be produced. Nonetheless, as I think you know I'm interested in general in converting TeX to MathML so I'd be interested in testing the ConTeXt parser, though I know very little about ConTeXt itself. –  Loop Space May 29 '12 at 17:44

Clearly the designers of LaTeX and MathML are deadly rivals and never speak to each other (or to the designers of OpenMath for that matter). [I've been asked to point out in the comments that this sentence is frivolous: there is a an overlap between the designers of LaTeX2e, MathML, OpenMath and this answer.]

Tex and MathML can't really be compared as TeX is (essentially) a typesetter and MathML (essentially) is just an input language. However TeX comes with certain input conventions and MathML input expressions often end up being typeset, so there is an overlap.

Note that MathML is naturally Unicode-centric: almost all characters are accessed via Unicode. The extent to which TeX is Unicode aware depends a lot on which TeX engine you are using. Another difference is that TeX is oriented towards a situation in which the author has full knowledge of fonts and page size etc being used, and so, if necessary can add fine tune adjustments (although many journal submission processes suffer from these "tweaks":-) The design MathML on the other hand is perhaps more influenced by a typical browser scenario in which the author has very limited control over the fonts used and virtually no control over the page size.

You might also look at my answer in the related question:

Why is LaTeX used as the defacto standard for math equations?

There are simply lots of pieces of software for going from one to the other.

In direction (La)TeX → MathML

• LaTeXML (NIST) (my personal favourite at present, all the MathML examples in DLMF were produced via this software)

• tex4ht (Also very good, very customisable but historically a bit hard to control)

• itex I haven't direct experience of this one but another popular convertor.

• the tex input parser of MathJaX (probably the most popular way to put Mathematics on the web, used at many of the sister sites in this stackexchange network.

In the direction direction MathML → TeX

Several XSLT stylesheets, I'll just mention mine at

• pmml2tex This was used to generate many of the sample images and TeX rendering in the HTML and PDF versions of the MathML spec.
-
Funny. I have no idea about this world so I was overly cautious. Can I put it this way, If the goal is expressive power is TeX theoretically better the way a pushdown machine is better than a finite state machine ? Is it your perception that TeX will continue to be the defacto way of marking up math because say there's something it has that will always provide higher fidelity output? –  John Thompson May 29 '12 at 9:50
I think your first paragraph should be marked as sarcasm lest the original poster think it's true. –  Matthew Leingang May 29 '12 at 10:41
@MatthewLeingang suppose so:-) –  David Carlisle May 29 '12 at 11:09
Tex is a programming language and MathML is a declarative syntax, so asking if TeX is more expressive is like asking if C is more expressive than ascii. It's possible to write a MathML parser in TeX @Marco mentioned the one in context, xmltex also supports MathML. It is not possible to write a parser for anything in MathML, it has no programming features at all. TeX's strengths are hand authoring and fine typography, so many people use it as an authoring format for mathml and as a print engine for mathml. MathML is a standard markup that interacts with TeX, Word, Mathematica, Web Browsers... –  David Carlisle May 29 '12 at 11:18
ConTeXt also parses TeX and generates MathML when generating XML/XHTML output. –  Aditya May 29 '12 at 13:31

MathML is definitely not an "input language" in the sense that most will interpret the phrase. It is not meant to be written by humans. It is a computer representation of mathematical notation. TeX, on the other hand, is both a human input language (people type it into computer programs to express mathematics) AND a math typesetting system that converts TeX language into a printed page or PDF or equation image for use in a web page. The TeX language can be used as a computer representation but it some disadvantages over MathML for that purpose.

MathML is already in use in many publishing companies and many programs (my company's included) use MathML to exchange math notation data with other programs. It is also part of many document standards, including DITA, EPUB3, and many more.

Anyone interested in math notation display should be aware of MathJax (www.mathjax.com), an open source JavaScript display engine for LaTeX and MathML that works in all modern web browsers. My company, Design Science, is one of its founders, along with the AMS and SIAM professional societies. Its sponsors include other publishing companies, professional societies, and educational software companies.

-

There are a bunch of good answers here, but in my opinion there is one point which has been overlooked: that is the ability for MathML to give a semantic representation of the maths, using content MathML, in addition to typesetting the layout of the maths. LaTex does not do this.

This is described in more detail in the MathML wiki, but to give a very simple example, say you have the code

y = mx + c


in LaTex. This tells you how to render the output, but there is nothing to tell you that m is multiplied by x. Whereas, if you are using content MathML, this 'implicit' multiplication is represented in the syntax tree

$<apply> <plus/> <apply> <times/> <ci>m</ci> <ci>x</ci> </apply> <ci>c</ci> </apply>$


which also incorporates the order of precedence for evaluating the equation.

It is trivial to transform from this 'semantic' representation to a LaTex or (presentation) MathML, but going in the other direction is ambiguous and a much more difficult problem. In this aspect MathML serves a different purpose thatn Tex.

-
I think you have a math background. Nothing is trivial in TeX world ;) –  percusse Nov 28 '14 at 11:39