20

As far as I understand there are two ways to display LaTeX on the web:

  • The MathJax JavaScript library, which allows you to embed LaTeX directly within HTML tags, but such LaTeX is converted by MathJax to MathML, an XML format which browsers can understand, unlike LaTeX. The disadvantages of this approach is that MathJax support for LaTeX is limited (and perhaps it would be useful if someone could outline some of its most important limitations).

  • Use a command-line tool to convert LaTeX to MathML (I've found this tool, but wonder if there is a command line tool I can install on Linux or Windows or better on both). Apparently, this solution has fewer limitations. I would like to know what the best tools to use for this conversion are, and whether MathML has any limitations as far as the following go:

    1. the MathML language's ability to encode everything that can be encoded with LaTeX syntax, and

    2. any possible limitations due to incomplete browser support for MathML (judging from this page, Chrome support seems to be severaly limited compared to Firefox, but this also means that if a user of my site is not using Firefox, then they will not be able to see math properly, which is a real drawback!).

Furthermore, if there are any other approaches to displaying math on the web, without resorting to images and PDF files, then I would like to know about them, but I think the alternatives I mentioned are most likely the two and only most viable ones.

Paulo Ney
  • 2,405
  • 1
    the mathjax library will render mathml on all current browsers, although since it will optionaly read tex syntax as well, if you are targetting mathjax conversion to mathml first may make less sense. although it is much quicker rendering the mathml in browsers that support it. Butthi squestion is a duplicate I'll find the older ones. – David Carlisle Feb 08 '15 at 14:45
  • OK, so you say use MathJax because it renders better in all browsers. But then there is something still not clear. If MathJax converts LaTeX to MathML programmatically, and then the MathML is passed to the browser, why would MathML produced by MathJax look better in browsers than MathML embedded within HTML? This is what's puzzling me. Thanks. – John Sonderson Feb 08 '15 at 14:50
  • 1
    I list some convertor tools here, although that's not really the duplicate question I had in mind, sure it was on thsi site somewhere... http://tex.stackexchange.com/questions/57717/relationship-between-mathml-and-tex – David Carlisle Feb 08 '15 at 14:50
  • 1
    mathjax is a large library that has several input parsers and several output generators. Basically it will take one of tex-like, mathml, or asciimath (a plain text simple markup), and convert to an internal mathml form, then whatever the input you can specify the output as one of svg, mathml, or html+css, so on modern browsers with good svg or css support one of the output formats will give good results, and the input format is a choice of the page author – David Carlisle Feb 08 '15 at 14:52
  • 1
    what I actually do on my own pages, is use mathml in the page, which allows on firefox mathjax to be skipped entirely so the page renders much faster with no external dependencies, and on everything else it uses mathjax compare this page in chrome and firefox – David Carlisle Feb 08 '15 at 14:56
  • @DavidCarlisle, the output MathJax output on Wikipedia must be SVG then, because I cannot see html+css alone being able to render the math, and as I pointed out chrome does not render mathml well. So, if your observation is correct, SVG must be what MathJax creates programmatically. – John Sonderson Feb 08 '15 at 15:26
  • @DavidCarlisle, you say you use MathML for Firefox, and MathJax for chrome and other browsers; the former renders faster, but only on firefox, so you use MathJax for everything else. But how do you manage this? You must detect the browser type in your html using JavaScript. I'm surprised you can get by without a PHP script to generate both versions of the code (MathML and MathJax). It seems you've used NAG's nagmathml.js script to do the job, I don't see any other JavaScript included on your page's HTML source. – John Sonderson Feb 08 '15 at 15:30
  • 1
    Yes the code used to detect the browser is all in that script which you may use/adapt as needed (I wrote it:-) – David Carlisle Feb 08 '15 at 15:48
  • Isn't this off-topic? – cfr Feb 08 '15 at 15:59

5 Answers5

9

I'm surprised no one mentioned pandoc, which is a perfectly suitable tool to convert LaTeX to html documents.

Using the following markdown file (that uses a LaTeX syntax):

% Pandoc math demos

$a^2 + b^2 = c^2$

$v(t) = v_0 + \frac{1}{2}at^2$

$\gamma = \frac{1}{\sqrt{1 - v^2/c^2}}$

$\exists x \forall y (Rxy \equiv Ryx)$

$p \wedge q \models p$

$\Box\diamond p\equiv\diamond p$

$\int_{0}^{1} x dx = \left[ \frac{1}{2}x^2 \right]_{0}^{1} = \frac{1}{2}$

$e^x = \sum_{n=0}^\infty \frac{x^n}{n!} = \lim_{n\rightarrow\infty} (1+x/n)^n$

You can get, depending on the options:

pandoc math.text -s -o mathDefault.html => http://pandoc.org/demo/mathDefault.html

pandoc math.text -s --mathml -o mathMathML.html => http://pandoc.org/demo/mathMathML.html

pandoc math.text -s --webtex -o mathWebTeX.html => http://pandoc.org/demo/mathWebTeX.html

pandoc math.text -s --mathjax -o mathMathJax.html => http://pandoc.org/demo/mathMathJax.html

pandoc math.text -s --katex -o mathKaTeX.html => https://pandoc.org/demo/mathKaTeX.html

The first option might seem limited, but since it only uses unicode, it is really portable and reliable.

Clément
  • 5,591
  • [WARNING] Could not convert TeX math 'v(t) = v_0 + \frac{1}{2}at^2', rendering as TeX [WARNING] Could not convert TeX math '\gamma = \frac{1}{\sqrt{1 - v^2/c^2}}', rendering as TeX [WARNING] Could not convert TeX math '\int_{0}^{1} x dx = \left[ \frac{1}{2}x^2 \right]{0}^{1} = \frac{1}{2}', rendering as TeX [WARNING] Could not convert TeX math 'e^x = \sum{n=0}^\infty \frac{x^n}{n!} = \lim_{n\rightarrow\infty} (1+x/n)^n', rendering as TeX – Vinod Jun 11 '22 at 01:46
9

LaTeXML provides several tools for converting TeX into XML/HTML/MathML. One of those tools is latexmlmath. For example, on a command line, the input:

latexmlmath '\mathbf{F}_{2}=k \frac{q_{1} q_{2} \hat{\mathbf{r}}_{21}}{r_{21}^{2}}'

has the output:

<?xml version="1.0" encoding="UTF-8"?>
<math xmlns="http://www.w3.org/1998/Math/MathML" alttext="\mathbf{F}_{2}=k\frac{q_{1}q_{2}\hat{\mathbf{r}}_{21}}{r_{21}^{2}}" display="block">
  <mrow>
    <msub>
      <mi></mi>
      <mn>2</mn>
    </msub>
    <mo>=</mo>
    <mrow>
      <mi>k</mi>
      <mo>⁢</mo>
      <mfrac>
        <mrow>
          <msub>
            <mi>q</mi>
            <mn>1</mn>
          </msub>
          <mo>⁢</mo>
          <msub>
            <mi>q</mi>
            <mn>2</mn>
          </msub>
          <mo>⁢</mo>
          <msub>
            <mover accent="true">
              <mi></mi>
              <mo stretchy="false">^</mo>
            </mover>
            <mn>21</mn>
          </msub>
        </mrow>
        <msubsup>
          <mi>r</mi>
          <mn>21</mn>
          <mn>2</mn>
        </msubsup>
      </mfrac>
    </mrow>
  </mrow>
</math>
Teepeemm
  • 6,708
5

tex4ht can be used to export LaTeX to MathML. Its advantage is that it supports custom commands, either from the user and from non standard packages, out of the box. It can also convert the whole LaTeX document to HTML with MathML included.

The disadvantage of MathML is poor browser support, which is practically non-existent (apart from Firefox, but it's not perfect). Fortunately, Mathjax can be used to add MathML support in non-supporting browsers.

To use MathML and MathJax for the rendering, use the following call to make4ht:

make4ht  filename.tex "mathml,mathjax"
michal.h21
  • 50,697
2

Your options are many, many indeed! There are literally dozens of programs to convert LaTeX to HTML and HTML+MathML, most of them with command line interfaces and some of them with GUI. In fact the information will be too much for an answer here on SX, so I created a page dedicated to LaTeX conversion utilities:

http://binb.droppages.com

Paulo Ney
  • 2,405
  • That's impressive, and would deserved to be bumped up. Good work! – Clément Feb 12 '17 at 03:24
  • It would get bumped up much more if the information were here, instead of behind a link. As it is, the link is dead, and we've lost this resource. – Teepeemm May 13 '19 at 00:46
  • 1
    @PauloNey Dead link not available on wayback can you provide a new link or try embedding as a mwe :-) –  May 13 '19 at 01:31
  • @Teepeemm well spotted suggest we wait to see if an update is forthcoming before flagging –  May 13 '19 at 01:52
  • 2
    Looking again, http://binb.droppages.com/ might be what @PauloNey was talking about (although it's a long list, which is much less useful than a table). – Teepeemm Jul 27 '19 at 02:23
  • Who maintains that list? There's also my project: https://github.com/loopspace/latex-to-internet – Andrew Stacey Oct 03 '20 at 21:01
1

I'll also mention texmath, which provides a Haskell library, a command-line program, and a web server (demo here). This converts between TeX math, MathML, OMML (the equation format used in Microsoft Word), and roff eqn. (The library was designed for use in pandoc, but it can be used independently.)

Example:

% texmath -f tex -t mathml
\mathbf{F}_{2}=k \frac{q_{1} q_{2} \hat{\mathbf{r}}_{21}}{r_{21}^{2}}
<math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow>
    <msub>
      <mstyle mathvariant="bold">
        <mi></mi>
      </mstyle>
      <mn>2</mn>
    </msub>
    <mo>=</mo>
    <mi>k</mi>
    <mfrac>
      <mrow>
        <msub>
          <mi>q</mi>
          <mn>1</mn>
        </msub>
        <msub>
          <mi>q</mi>
          <mn>2</mn>
        </msub>
        <msub>
          <mover>
            <mstyle mathvariant="bold">
              <mi></mi>
            </mstyle>
            <mo accent="true">̂</mo>
          </mover>
          <mn>21</mn>
        </msub>
      </mrow>
      <msubsup>
        <mi>r</mi>
        <mn>21</mn>
        <mn>2</mn>
      </msubsup>
    </mfrac>
  </mrow>
</math>
John MacFarlane
  • 503
  • 2
  • 6