## An XPCOM component to parse mathematical expressions into MathML (part 2)

By fredw on Tuesday, October 5 2010, 21:47 - Permalink

So I'm finally done with the implementation of itex2MML in my mathparser. The current set of patches is available here and can be used in any Mozilla product based on mozilla-central. For those who don't know, itex2MML is a converter from a LaTeX-like syntax to MathML which was originally written, about ten years ago, by Paul Gartside for the Mozilla MathML Project. It has been maintained since then by Jacques Distler, who has made a great work to improve and extend it (and has also reported several bugs that helped us to make our MathML layout engine better ;-). Hence it is a mature tool and it is worth being based on it in order to provide a decent LaTeX-like parser.

You can find a list of itex2MML commands as well as various examples. All itex2MML commands are supported in my mathparser, except inclusion of SVG graphics, XML entities and obsolete maction's commands. There are also some additional features such that support for Unicode characters in the LaTeX input. Below are random demos:

`$$\int_M K\;dA+\int_{\partial M}k_g\;ds=2\pi\chi(M), \, $$`

$${\int}_{M}K\phantom{\rule{thickmathspace}{0ex}}\mathrm{dA}+{\int}_{\partial M}{k}_{g}\phantom{\rule{thickmathspace}{0ex}}\mathrm{ds}=2\pi \chi (M),\phantom{\rule{thinmathspace}{0ex}}$$

```
$$\oint_S \mathbf{E} \cdot \mathrm{d}\mathbf{A} =
\frac{Q}{\varepsilon_0},$$
```

$${\oint}_{S}E\cdot \mathrm{d}A=\frac{Q}{{\epsilon}_{0}},$$

```
$$u = \root{3}{-{q \over 2} \pm \sqrt{{q^2 \over 4} + {p^3 \over
27}}}$$
```

$$u=\sqrt[3]{-\frac{q}{2}\pm \sqrt{\frac{{q}^{2}}{4}+\frac{{p}^{3}}{27}}}$$

```
$$\frac{a_0}{2} + \sum_{n=1}^\infty \, [a_n \cos(n x) + b_n \sin(n
x)]$$
```

$$\frac{{a}_{0}}{2}+\sum _{n=1}^{\mathrm{\infty}}\phantom{\rule{thinmathspace}{0ex}}[{a}_{n}\mathrm{cos}(nx)+{b}_{n}\mathrm{sin}(nx)]$$

## Comments

Nice job Frédéric !!! One quesiton though: can't you release that as an XPI adding the component instead of a patch?

The middle two worked on Planet but unfortunately the first and last were mangled.

@Daniel: Yes, it would certainly be a good idea. I've never made an XPI before so I can't guarantee whether it is feasible, but I plan to try.

@Neil: Thanks, it seems to be an issue of Planet. I've opened bug 602304.

What would be utterly awesome would be for someone to rewrite this in Javascript. Then you could do:

<script src="itex2mathml.js"></script>

...

<div class="itex">

$$\int_M K\;dA+\int_{\partial M}k_g\;ds=2\pi\chi(M), \, $$

</div>

And the script would find the div, run the conversion, and replace the div with the appropriate MathML markup. Then, people could just cut and paste iTex code into web pages, include the script, and voila! :-)

@Gerv: there are already scripts doing similar things. However, this basically means replacing a standard for the Web (MathML) by javascript + LaTeX, so I don't think it is really a good idea. Consider the case of HTML: people can either write static documents with their favorite editors or use server-side scripts to generate pages. I've never seen people using javascript in a Web page to dynamically convert a Wiki syntax to HTML... why should it be different for MathML?

I guess Wikimedia (the software behind Wikipedia) could make use of this! I really hate the PNG rendering with grayscale antialiasing and no way to copy from it.

@Finn Furz: You are not alone to hate the PNG images!

However, my parser is rather intended to be used in mozilla-based applications. For server-side converters, Jacques Distler's original itex2MML does the work. See his blog or his wiki for example:

http://golem.ph.utexas.edu/~distler...

http://golem.ph.utexas.edu/wiki/ins...

Note that some work have already been done in Wikimedia:

https://bugzilla.wikimedia.org/show...

For copying MathML, let's also mention the bug in Mozilla:

https://bugzilla.mozilla.org/show_b...