LaTeX2MathML Documentation


1 Brief presentation


LaTeX2MathML is a php5 written program that allows you convert LaTeX math formulas to MathML.

1.1 About MathML


Do you know that your web-browser can display math formulas as if there were hand-written ? Still, it works better with firefox (2, 3,...) or opera (9.5+, 10...). If you want to display mathml with internet explorer it is also possible, you just have to download mathplayer, here. Anyway, if you use firefox, you should download the math fonts here : http://www.mozilla.org/projects/mathml/fonts/. Thus, to read this article, which contains mathml, you will need one of the previously mentionned browser. This document is a brief presentation of the LaTeX2MathML program.

1.2 Downloading LaTeX2MathML


The project is hosted by sourceforge.net, you can download the latest release at http://sourceforge.net/projects/latex2mathml/.

1.3 First example


This page is translated by LaTeX2MathML, for example, this formula :
\begin{pmatrix} \int_{-\infty}^{+\infty} \frac{1}{1+x^2} d x & \sum_{n=0}^{+\infty} \frac{1}{x_n+1} & E = mc^2 \\ e = -n \frac{d\phi}{dt} & a & b \\ u=Ri & E=Ri^2 & \dots \\ 0 & \frac{\pi^2}{6} = \sum_{n=0}^{+\infty}\text{qqchose} & 0 \end{pmatrix}
is converted and rendered (by your browser) as : ( - + 1 1 + x 2 d x n = 0 + 1 x n + 1 E = m c 2 e = - n d ϕ d t a b u = R i E = R i 2 0 π 2 6 = n = 0 + s o m e t h i n g 0 ) .



2 How to use LaTeX2MathML


2.1 Installing the script


The script is pretty easy to use, and some examples are given in the archive. However, you can find here some instructions.

2.1.1 Basic example

Here's a very baisc example :

// Serve the document as an xml file, you must do this, 
// but it could not work with some web browsers, see the 2nd example.
header("Content-type: application/xhtml+xml; charset=utf-8");

// These files are required to get the script fully functional.
require('config.class.php');
require(
'commands.class.php');
require(
'config.php');
require(
'latex2xml.class.php');

// Instantiation of the class.
$l2xml LaTeX2Xml::getInstance();

// Parse a simple formula.
$l2xml->parseMath("\\frac{\pi}{2}");

// Display the MathML of the previous formula.
echo $l2xml->parse();

With this script, you can only display one formula, here : π 2 .

2.1.2 Display formulas in XHTML documents

Now imagine that you want to display some formulas on your website. Your website has to be written in xhtml 1.1 and it MUST be valid when it's checked by w3c's validator : http://validator.w3.org/. You should use the following DTD :
 
<!DOCTYPE html PUBLIC
    
"-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"
    "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd"
>

Also, prefer utf-8 charset, which is really better than iso for rendering mathml and required for LaTeX2MathML. The document should be served as "application/xhtml+xml", which could be a problem with some browsers. A good solution for implementing that properly is :
 
if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml"
   || 
stristr($_SERVER["HTTP_USER_AGENT"],"MathPlayer"))
    
header("Content-type: application/xhtml+xml; charset=utf-8");
else
    
header("Content-type:text/html; charset=utf-8");

Obviously, for a browser which doesn't support mathml the document will be served as text/html and, thus, will not render mathml. Another issue is that the output given by LaTeX2MathML contains the xml prolog. If you want to include MathML in your XHTML pages, you will need to remove this part. Tlast line
 echo $l2xml->parse(); 

have to be replaced by :
 echo preg_replace('/<\?xml(?:.+)\?>(.+)/s''$1'$l2xml->parse()); 



To be continued...