# Blog de Frédéric

## Tag - xpcom

Wednesday, January 29 2014

## New MathML Firefox add-ons on AMO

While the patches for MathML integration in MediaWiki are progressively being reviewed and merged and while we are working on the support for Open Type fonts with a MATH table in Gecko, I finally found time to check the progress in Mozilla's add-on SDK. In particular, since the last time I tried (some years ago) they have introduced a cleaner interface for content scripts as well as the possibility to use XPCOM for missing features. Hence I have been able to update some of my experimental MathML add-ons. I have submitted two new add-ons to Mozilla's AMO that I hope could be useful to some people:

• MathJax Native MathML, an add-on to force MathJax to switch to Gecko's MathML support without having to use the MathJax menu to change the output mode and works even on Websites where that menu is disabled. This also removes MathJax's automatic rescaling and inline-block span that are currently causing random rendering bugs with Gecko's native MathML (and will confuse possible future line-breaking support anyway).
• MathML Copy (at the moment only partially reviewed by the AMO team), an add-on to copy MathML and TeX into the clipboard. For MathML, two flavors are copied: the source as plain text (to paste in your favorite text editor) and the MathML as HTML (to paste in Thunderbird, MDN, any Gecko-based HTML editor etc). Copying TeX is only possible when it is provided via the standard MathML annotation method, which is the case in e.g. LaTeXML and Instiki documents as well as in Wikipedia in the future.

As usual, there is room for improvements and bug fixes, but that's a start. In particular I would be happy to get translations for the two strings of the MathML Copy add-on: "Copy MathML Formula" and "Copy TeX Source". Also, because I used the add-on SDK these add-ons are unfortunately only available for Firefox at the moment...

Tuesday, October 5 2010

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

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 \left(M\right),\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}±\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}^{\infty }\phantom{\rule{thinmathspace}{0ex}}\left[{a}_{n}\mathrm{cos}\left(nx\right)+{b}_{n}\mathrm{sin}\left(nx\right)\right]$

Thursday, September 16 2010

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

I have started to write a math parser usable by Mozilla-based applications. In particular, this could help to add math editing features to Mozilla's editors (BlueGriffon™, Komposer or Thunderbird etc). Of course, it will also be usable by Mozilla's extensions, such that Firemath. I will probably give more details in subsequent blog posts but the main ideas are given in that one.

First, the parser is usable through classical XPCOM calls. With the current interface, we get something like (in Javascript):

mathparser =
Components.classes['@mozilla.org/editor/mathparser;1'].
createInstance(Components.interfaces.nsIMathParser);

node = mathparser.parse(document, input,
Components.interfaces.nsIMathParser.MATHPARSER_MODE_SIMPLE);

where input is a string representing a mathematical formula and node is the output MathML tree. Note the third parameter of the parse function, which allows to choose a parser mode. For the moment, I have only written one for very basic formulas. However, I plan to add at least a LaTeX-like mode.

As an example, if we provide the following input strings "{∑_{i=1}^{+∞} 1/n^2} = π^2/6" and "{∫_0^{+∞} {ⅆx}/{4(x+1)√x}} = π/4" the parser outputs:

$\sum _{i=1}^{+\infty }\frac{1}{{n}^{2}}=\frac{{\pi }^{2}}{6}$${\int }_{0}^{+\infty }\frac{dx}{4\left(x+1\right)\sqrt{x}}=\frac{\pi }{4}$

Note that the core engine is produced using the famous parser generator Bison and hence it will be easy to adapt the work of itex2MML. The lexical analyzer is written directly and we get a very nice feature: unicode support! If people are interested, I have some patches applyable to mozilla-central...