11

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.

  • 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 8 '15 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/… – David Carlisle Feb 8 '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 8 '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 8 '15 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:-) – David Carlisle Feb 8 '15 at 15:48
4

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. You need to use simple configuration file for tex4ht, as it doesn't support Mathjax out of the box:

The configuration file mathjax.cfg:

\Preamble{xhtml,mathml}
\Configure{@HEAD}{%
\HCode{<script type="text/javascript"
   src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=MML_CHTML">
 </script>\Hnewline}}
\begin{document}
\EndPreamble

The xhtml,mathml options in \Preamble command requests XHTML and MathML support, \Configure{@HEAD} will insert the JavaScript code needed for MathJax loading.

The document can be compiled using:

make4ht  -c mathjax.cfg filename.tex
2

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 --latexmathml -o mathLaTeXMathML.html => http://pandoc.org/demo/mathLaTeXMathML.html

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

2

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>
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 prepared a little table with most of the relevant information here:

http://binb.droppages.com/latexto.html

and some information on each one of them.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.