LaTeXMathML: translating LaTeX math notation dynamically to Presentation MathML


Note. This is the original version of LaTeXMathML. A more recent and greatly extended version is available from Jeff Knisley here. There is also a port of LaTeXMathML into perl by Peter Williams here. See also the Acknowledgement and Warning below.

Here is an easy way for mathematicians who are familiar with LaTeX to get mathematics into HTML web pages.
  1. (a) Copy the text
      <script type="text/javascript"
      src="https://www.maths.nottingham.ac.uk/plp/pmadw/LaTeXMathML.js">
      </script>
    
    between <head> and </head> in your HTML file (before or after the <title>).
    (b) Alternatively, and much better, take your own copy of the program by right-clicking on the link LaTeXMathML.js (and then Save As), and change the src line in (a) to point to your own copy.
  2. Include any mathematics in LaTeX format between \$...\$. (To print a dollar sign, escape it as \\$.)
  3. It is possible to add definitions of new characters on individual web pages by using newcommand (note NOT \newcommand) within the <script> line, in a way described by Peter Jipsen here.

Acknowledgement. This JavaScript program has been created by modifying Peter Jipsen's ASCIIMathML.js script. He deserves full credit for the original implementation; any bugs have probably been introduced by me.
Warning. I am not now maintaining this program. I retired at the end of 2007, and I do not know how long this web site will continue to exist—I hope it will remain for a long time, but that may not be within my control. Alternative (and more developed) versions of LaTeXMathML will hopefully continue to be available from within Peter Jipsen's ASCIIMathML and from the web sites mentioned in the Note above.

Differences from LaTeX (see the examples here; these differences apply to my version of LaTeXMathML, but not necessarily to the more developed versions mentioned above).
  1. Equations cannot be delimited by \[...\] or \begin{displaymath}...\end{displaymath}, but much the same effect can be achieved by the sequence
    <blockquote>\$\displaystyle{ ... }\$</blockquote>
    Unlike in LaTeX, \displaystyle and \texstyle behave as functions acting on what immediately follows them, and so if you want the whole equation set in display-style, then the braces are necessary. (If you use \displaystyle{ or \texstyle{ in an array entry, make sure that the closing } is inside the same array entry.)
  2. Aligned equations can be produced by the sequence
    <blockquote>\$\begin{eqnarray} ... \end{eqnarray}\$</blockquote>
    Note that, unlike in LaTeX, dollars are needed here: the command \begin{eqnarray} is only recognized once the equation has already started. As in LaTeX, eqnarrays are set in display-style by default.
  3. The sequences \begin{eqnarray}, \end{eqnarray}, \begin{array} and \end{array} are not parsed but are recognized as literal character strings; so they will not be recognized if you insert any spaces into them.
  4. LaTeX commands are not interpreted outside equations, and non-mathematical LaTeX commands (e.g., \label) are not interpreted at all. There is no mechanism for numbering equations; \eqno and \nonumber are not recognized.
  5. In arrays, \multicolumn is not recognized (although in principle it could be added), and there is no mechanism for inserting vertical lines between columns or horizontal lines between rows, or for changing the inter-column or inter-row space. (Using \\[1ex] simply causes the next line of the array to start with [1ex].) Otherwise, most pure LaTeX mathematical commands are recognized, but most AMS additional commands aren't. Note that some commands, like \scriptstyle, are implemented but ignored by some browsers.
  6. Optional arguments are not recognized, but a new command \root has been introduced to take the place of \sqrt with an optional argument. Thus to get $\root32$ use \root32, not \sqrt[3]2.
  7. Although constructs can be nested within equations, it is not possible to nest equation and non-equation. For example, the construct
    $\qquad$\$...\mbox{...\$.\$...}\$
    will not work.
  8. It seems to be a feature of MathML, or possibly a bug in some browsers, that if you end an equation with </math>, with the punctuation mark outside the equation, then the browser will feel entitled to put the equation at the end of one line and the punctuation mark at the start of the next line. To avoid this happening with inline equations in LaTeXMathML, it is necessary always to include closing punctuation marks inside the \$...\$ — exactly the opposite of what one should do in LaTeX.
  9. There is no "Roman" command in MathML, and so \mathrm has been implemented in the same way as \mbox; in particular, spaces in the input will appear in the output.
  10. A small number of purely numerical fractions exist and are produced by commands of the form \frac12 (with no braces or spaces)—see the list below. Otherwise \frac behaves as in LaTeX.
  11. < is a significant character in HTML. To print it use \lt in equations (and &lt; outside equations).
  12. It may be necessary to insert extra braces in order to prevent big operators such as summation signs, without limits, from growing too large—see the examples.
  13. The behaviour of LaTeXMathML is undetermined if there are syntax errors in the input, e.g., unmatched braces; but generally speaking, it tries to produce output even in situations where LaTeX would report an error and give up.
Fonts: Note that there is no "Roman" command in MathML, and so \mathrm (and \textrm) have been made synonyms for \mbox.
\mathrm{a b c A B C~1+2=3} $\to$ $\textrm{a b c A B C~1+2=3}$
\mathit{a b c A B C~1+2=3} $\to$ $\mathit{a b c A B C~1+2=3}$
\mathbf{a b c A B C~1+2=3} $\to$ $\mathbf{a b c A B C~1+2=3}$
\mathsf{a b c A B C~1+2=3} $\to$ $\mathsf{a b c A B C~1+2=3}$ (in some browsers, unlike in LaTeX, letters remain italic)
\mathsf{\mathrm{a b c A B C~1+2=3}} $\to$ $\mathsf{\mathrm{a b c A B C~1+2=3}}$
\mathtt{a b c A B C~1+2=3} $\to$ $\mathtt{a b c A B C~1+2=3}$ (in some browsers, unlike in LaTeX, letters remain italic)
The following three fonts work only for capital letters and, except in Internet Explorer, only at the top level of subnesting.
\mathbb{ABC{DEF}GHI} $\to$ $\mathbb{ABC{DEF}GHI}$
\mathbb{AB...Z} $\to$ $\mathbb{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$
\mathcal{AB...Z} $\to$ $\mathcal{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$
\mathfrak{AB...Z} $\to$ $\mathfrak{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$

Spaces: $\qquad$\! −3/18em$\textrm{|} \! \textrm{|}\qquad$ (often apparently ignored)
nothing, 0em$\quad$ $\textrm{|} \textrm{|}$
\,3/18em $\textrm{|} \, \textrm{|}$
\> or \:$\qquad$4/18em $\textrm{|} \: \textrm{|}$
\;5/18em $\textrm{|} \; \textrm{|}$
~1/3em $\textrm{|} ~ \textrm{|}$
\quad1em $\textrm{|} \quad \textrm{|}$
\qquad2em $\textrm{|} \qquad \textrm{|}$

Diacritical marks:$\qquad$ \acute, \grave, \breve, \check $\acute a \acute W ~ \grave a \grave W ~ \breve a \breve W ~ \check a \check W$
\dot, \dotdot, \mathring $\dot a \dot W ~ \ddot a \ddot W ~ \mathring a \mathring W$
\hat, \widehat, \tilde, \widetilde $\hat a \hat W ~ \widehat a \widehat W ~ \tilde a \tilde W ~ \widetilde a \widetilde W$
\vec, \overrightarrow, \overleftarrow$\qquad$ $\vec a \vec W ~ \overrightarrow a \overrightarrow W ~ \overleftarrow a \overleftarrow W$
\bar, \overline, \underline $\bar a \bar W ~ \overline a \overline W ~ \underline a \underline W$
\overbrace{...}^{26} $\overbrace{a+b+\cdots+z}^{26}$
\underbrace{...}_{26} $\underbrace{a+b+\cdots+z}_{26}$
\overbrace and \underbrace have been implemented using brackets rather than braces, because there are no horizontal brace characters in the current fonts, and the MathML names &OverBrace; and &UnderBrace; are unrecognized, and indeed produce error messages, in current browsers.

Primes: a'b''c'''d''''e $\to$ $a'b''c'''d''''e$
a^\prime b^{\prime\prime} c^{\prime\prime\prime} d^{\prime\prime\prime\prime} $\to$ $a^\prime b^{\prime\prime} c^{\prime\prime\prime} d^{\prime\prime\prime\prime}$

Greek letters: \alpha, \beta, etc. (note no \omicron) $\to$ $\alpha \beta \gamma \delta \epsilon \zeta \eta \theta \iota \kappa \lambda \mu \nu \xi \pi \rho \sigma \tau \upsilon \phi \chi \psi \omega$
\varepsilon, \vartheta, \varpi, \varrho, \varsigma, \varphi $\to$ $\varepsilon \vartheta \varpi \varrho \varsigma \varphi$
\Gamma, \Delta, \Theta, \Lambda, \Xi, \Pi, \Sigma, \Upsilon, \Phi, \Psi, \Omega $\to$ $\Gamma \Delta \Theta \Lambda \Xi \Pi \Sigma \Upsilon \Phi \Psi \Omega$

Fractions: \frac12, \frac14, \frac34, etc $\to$ $\frac12~\frac14~\frac34~\frac13~\frac23~\frac15~\frac25~\frac35~ \frac45~\frac16~\frac56~\frac18~\frac38~\frac58~\frac78$

Delimiters: (, ), [ or \lbrack, ] or \rbrack, \{ or \lbrace, \} or \rbrace, \lfloor, \rfloor, \lceil, \rceil, \langle, \rangle, /, \backslash, | or \vert, \| or \Vert, \uparrow, \Uparrow, \downarrow, \Downarrow, \updownarrow, \Updownarrow:
normal:$~~~( ~ ) ~ [ ~ ] ~ \{ ~ \} ~ \lfloor ~ \rfloor ~ \lceil ~ \rceil ~ \langle ~ \rangle ~ / ~ \backslash ~ \vert ~ \Vert ~ \uparrow ~ \Uparrow ~ \downarrow ~ \Downarrow ~ \updownarrow ~ \Updownarrow$
\big:$~\qquad \big( ~ \big) ~ \big[ ~ \big] ~ \big\{ ~ \big\} ~ \big\lfloor ~ \big\rfloor ~ \big\lceil ~ \big\rceil ~ \big\langle ~ \big\rangle ~ \big/ ~ \big\backslash ~ \big\vert ~ \big\Vert ~ \big\uparrow ~ \big\Uparrow ~ \big\downarrow ~ \big\Downarrow ~ \big\updownarrow ~ \big\Updownarrow$
\Big:$~\qquad \Big( ~ \Big) ~ \Big[ ~ \Big] ~ \Big\{ ~ \Big\} ~ \Big\lfloor ~ \Big\rfloor ~ \Big\lceil ~ \Big\rceil ~ \Big\langle ~ \Big\rangle ~ \Big/ ~ \Big\backslash ~ \Big\vert ~ \Big\Vert ~ \Big\uparrow ~ \Big\Uparrow ~ \Big\downarrow ~ \Big\Downarrow ~ \Big\updownarrow ~ \Big\Updownarrow$
\bigg:$\qquad \bigg( ~ \bigg) ~ \bigg[ ~ \bigg] ~ \bigg\{ ~ \bigg\} ~ \bigg\lfloor ~ \bigg\rfloor ~ \bigg\lceil ~ \bigg\rceil ~ \bigg\langle ~ \bigg\rangle ~ \bigg/ ~ \bigg\backslash ~ \bigg\vert ~ \bigg\Vert ~ \bigg\uparrow ~ \bigg\Uparrow ~ \bigg\downarrow ~ \bigg\Downarrow ~ \bigg\updownarrow ~ \bigg\Updownarrow$
\Bigg:$\qquad \Bigg( ~ \Bigg) ~ \Bigg[ ~ \Bigg] ~ \Bigg\{ ~ \Bigg\} ~ \Bigg\lfloor ~ \Bigg\rfloor ~ \Bigg\lceil ~ \Bigg\rceil ~ \Bigg\langle ~ \Bigg\rangle ~ \Bigg/ ~ \Bigg\backslash ~ \Bigg\vert ~ \Bigg\Vert ~ \Bigg\uparrow ~ \Bigg\Uparrow ~ \Bigg\downarrow ~ \Bigg\Downarrow ~ \Bigg\updownarrow ~ \Bigg\Updownarrow$
List of characters not included above
(Characters that are not available have been replaced by the nearest available alternative)
Binary Relations
$\lt$ \lt $\gt$ \gt $\ne$ \neq or \ne
$\le$ \leq or \le $\ge$ \geq or \ge $\equiv$ \equiv
$\leqslant$ \leqslant $\geqslant$ \geqslant $:=$ :=
$\ll$ \ll $\gg$ \gg $\doteq$ \doteq
$\prec$ \prec $\succ$ \succ $\sim$ \sim
$\preceq$ \preceq $\succeq$ \succeq $\simeq$ \simeq
$\subset$ \subset $\supset$ \supset $\approx$ \approx
$\subseteq$ \subseteq $\supseteq$ \supseteq $\cong$ \cong
$\sqsubset$ \sqsubset $\sqsupset$ \sqsupset $\Join$ \Join
$\sqsubseteq$ \sqsubseteq $\sqsupseteq$ \sqsupseteq $\bowtie$ \bowtie
$\in$ \in $\ni$ \ni, \owns $\notin$ \notin
$\vdash$ \vdash $\dashv$ \dashv $\models$ \models
$\mid \mid $\parallel$ \parallel $\perp$ \perp
$\smile \smile $\frown$ \frown $\asymp$ \asymp
$\propto$ \propto
Binary Operators
$\pm$ \pm $\mp$ \mp $\triangleleft$ \triangleleft
$\times$ \times $\div$ \div $\triangleright$ \triangleright
$\cdot$ \cdot $\setminus$ \setminus $\star$ \star
$\cup$ \cup $\cap$ \cap $\ast$ \ast
$\sqcup$ \sqcup $\sqcap$ \sqcap $\circ$ \circ
$\vee$ \vee, \lor $\wedge$ \wedge, \land $\bullet$ \bullet
$\oplus$ \oplus $\ominus$ \ominus $\diamond$ \diamond
$\odot$ \odot $\oslash$ \oslash $\uplus$ \uplus
$\otimes$ \otimes $\bigcirc$ \bigcirc $\amalg$ \amalg
$\bigtriangleup$ \bigtriangleup $\bigtriangledown$ \bigtriangledown $\dagger$ \dag or \dagger
$\lhd$ \lhd $\rhd$ \rhd $\ddagger$ \ddag or \ddagger
$\unlhd$ \unlhd $\unrhd$ \unrhd $\wr$ \wr
 
BIG Operators
$\sum$ \sum $\prod$ \prod $\bigoplus$ \bigoplus
$\bigcup$ \bigcup $\bigcap$ \bigcap $\bigotimes$ \bigotimes
$\bigvee$ \bigvee $\bigwedge$ \bigwedge $\bigodot$ \bigodot
$\int$ \int $\oint$ \oint $\biguplus$ \biguplus
$\bigsqcup$ \bigsqcup $\bigsqcap$ \bigsqcap $\coprod$ \coprod
 
Arrows
$\gets$ \leftarrow or \gets $\longleftarrow$ \longleftarrow $\uparrow$ \uparrow
$\to$ \rightarrow or \to $\longrightarrow$ \longrightarrow $\downarrow$ \downarrow
$\leftrightarrow$ \leftrightarrow $\longleftrightarrow$ \longleftrightarrow $\updownarrow$ \updownarrow
$\Leftarrow$ \Leftarrow $\Longleftarrow$ \Longleftarrow $\Uparrow$ \Uparrow
$\Rightarrow$ \Rightarrow $\Longrightarrow$ \Longrightarrow $\Downarrow$ \Downarrow
$\Leftrightarrow$ \Leftrightarrow $\Longleftrightarrow$ \Longleftrightarrow $\Updownarrow$ \Updownarrow
$\mapsto$ \mapsto $\longmapsto$ \longmapsto $\iff$ \iff (bigger spaces)
 
Miscellaneous Symbols
$\ldots$ \ldots $\cdots$ \cdots $\forall$ \forall
$\vdots$ \vdots $\ddots$ \ddots $\exists$ \exists
$\Re$ \Re $\Im$ \Im $\aleph$ \aleph
$\hbar$ \hbar $\ell$ \ell $\wp$ \wp
$\emptyset$ \emptyset $\infty$ \infty $\surd$ \surd
$\partial$ \partial $\nabla$ \nabla $\triangle$ \triangle
$\square$ \square $\diamond$ \diamond
$\Box$ \Box $\Diamond$ \Diamond
$\bot$ \bot $\top$ \top $\angle$ \angle
$\neg$ \neg or \lnot $\therefore$ \therefore

Author:
Douglas R. Woodall