Home > JavaScript, math > Pretty equations in HTML

Pretty equations in HTML

You may have seen the nice looking equations in my last post. As part of the process of writing that post I researched MathML as a way of embedding the equations in the post. it turned out to be a complex markup language which renders very nicely but has a few big drawbacks. The first is that only Firefox supports MathML in its core so far. There is a plugin for Internet Explorer which supports it but I doubt it’s widely installed. The second problem is that you can’t just put MathML in your web page and have Firefox interpret it. It turns out you need a bit of Javascript to make it pick up and render the MathML. This is great and makes equations render very well in Firefox but writing the MathML is a pain.

Enter ASCIIMathML, a JavaScript library which converts a simple ASCII format to MathML for rendering in MathML compliant browsers. The syntax is pretty much what you would expect with a few tricks up its sleeve. A simple equation, such as `y=x^2` is written as y=x^2. A more complicated example from my last post such as:
`arc=2*cos^-1(1-r_2^2/(2*r_1^2))*r_1`
is written as:

arc=2*cos^-1(1-r_2^2/(2*r_1^2))*r_1

Another example with a square root:
`y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)`

y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)

Now, those if you viewing these posts in a non-Firefox browser are probably wondering why you are also seeing pretty equations here. The short answer is that you’re seeing images imstead of MathML rendered by your browser. The long answer involves a four part toolchain. The first piece is ASCIIMathML, slightly altered to insert an image tag instead of MathML. The images are generated by a PHP script which first runs the ASCII through ASCHIIMathPHP, a version of ASCIIMathML.js to PHP. The resulting MathML has the times symbol replace by one which the later tools understand (replace ⋅ with ·). The resulting MathML is then passed to svgmath, a python script which converts MathML to SVG. SVG would be great but Internet Explorer still doesn’t support it without a plugin. Because of this the SVG is then passed through inkscape via command-line to render out a png which is what you see in your browser.
Here’s some sample PHP to run these commands:

$ascii = 'y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)';
 
$pngFile = md5($ascii).'.png';
if (!file_exists($pngFile)) {
    require 'ASCIIMathPHP-2.0.cfg.php';
    require 'ASCIIMathPHP-2.0.class.php';
 
    $ascii_math = new ASCIIMathPHP($symbol_arr);
    $ascii_math->setExpr($ascii);
    $ascii_math->genMathML();
 
    $mathml = $ascii_math->getMathML();
 
    $mathml = str_replace('⋅', '·', $mathml);
 
    $mathmlFile = tempnam(sys_get_temp_dir(), 'mathml');
    $td = fopen($mathmlFile, 'w');
    fwrite($td, $mathml);
 
    $svgFile = tempnam(sys_get_temp_dir(), 'svg');
    system("/usr/bin/env python math2svg.py -s $mathmlFile > $svgFile");
 
    system("inkscape -f $svgFile -e $pngFile");
 }
 
header('Content-Type: image/png');
die(file_get_contents($pngFile));

All of this is also wrapped up in a WordPress plugin which includes the JS file in the header. Unfortunately, I don’t have an easy way to package this up as it requires python and inkscape but if you would like to get more info just let me know and I’ll send you more information.

UPDATE: I finally did update the plugin to be useful for others and posted it.