19

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.

9
  • 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. Feb 8, 2015 at 14:45
  • 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... tex.stackexchange.com/questions/57717/… Feb 8, 2015 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 Feb 8, 2015 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 Feb 8, 2015 at 14:56
  • 1
    Yes the code used to detect the browser is all in that script which you may use/adapt as needed (I wrote it:-) Feb 8, 2015 at 15:48

5 Answers 5

8

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>
7

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.

1
  • [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, 2022 at 1:46
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"
1

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

6
  • That's impressive, and would deserved to be bumped up. Good work!
    – Clément
    Feb 12, 2017 at 3: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, 2019 at 0:46
  • 1
    @PauloNey Dead link not available on wayback can you provide a new link or try embedding as a mwe :-)
    – user170109
    May 13, 2019 at 1:31
  • @Teepeemm well spotted suggest we wait to see if an update is forthcoming before flagging
    – user170109
    May 13, 2019 at 1:52
  • 2
    Looking again, 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, 2019 at 2:23
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>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .