48

Is there any tool that can convert Latex documents to HTML documents, so that all math is presented with MathJax?

(Obviously, it is fairly easy to hack together a quick Perl script that is able to do a crude conversion, but I am interested in a bit more robust solutions — at least it should get all cross-references right automatically.)

31

Update 07/2020:

The code bellow is not needed anymore. TeX4ht has now full support for the MathJax output. The equivalent of the code bellow is this:

make4ht filename.tex "mathml,mathjax"

If you want to keep your LaTeX math as plain text, use just:

make4ht filename.tex "mathjax"

make4ht is replacement for htlatex that converts to HTML5 and UTF-8 by default, so no special configurations as in the old answer are necessary.


Original answer:

You can use TeX4ht. It can output math as mathml which can be then displayed with mathjax. You can also configure html header to load mathjax script from mathjax's cdn:

\Preamble{xhtml,mathml}
\Configure{VERSION}{}
\Configure{DOCTYPE}{\HCode{<!DOCTYPE html>\Hnewline}}
\Configure{HTML}{\HCode{<html>\Hnewline}}{\HCode{\Hnewline</html>}}
\Configure{@HEAD}{}
\Configure{@HEAD}{\HCode{<meta charset="UTF-8" />\Hnewline}}
\Configure{@HEAD}{\HCode{<meta name="generator" content="TeX4ht
(http://www.cse.ohio-state.edu/\string~gurari/TeX4ht/)" />\Hnewline}}
\Configure{@HEAD}{\HCode{<link
         rel="stylesheet" type="text/css"
         href="\expandafter\csname aa:CssFile\endcsname" />\Hnewline}}
\Configure{@HEAD}{\HCode{<script type="text/javascript"\Hnewline
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"\Hnewline
></script>\Hnewline}}
\Configure{@HEAD}{\HCode{<style type="text/css">\Hnewline
  .MathJax_MathML {text-indent: 0;}\Hnewline
</style>\Hnewline}}
\begin{document}
\EndPreamble

With this config file, we request to output math as mathml, then configure html header so resulting file is html5. Save it as for example ht5mjlatex.cfg and then call from command line:

htlatex filename.tex "ht5mjlatex.cfg, charset=utf-8" " -cunihtf -utf8"

or you can use William F. Hammond's script (at bottom of the page)

| improve this answer | |
  • 3
    And this has the advantage that browsers that can display MathML (natively or via a plugin) can avoid the necessity of loading MathJaX. – Andrew Stacey Aug 27 '12 at 7:27
  • 9
    This is certainly better than nothing, but I would prefer to keep the HTML source code somewhat readable. Very simple equations seem to produce dozens of lines of mathml markup... – Jukka Suomela Aug 27 '12 at 23:07
7

I would personally prefer using pandoc for this task, It's a one step solution and the output is much better in my opinion.

To convert files from one markup format into another, pandoc is your swiss-army knife.

This command should fit most purposes:

pandoc document.tex --standalone --mathjax --output document.html

It will create a single HTML file with a javascript reference to mathjax.js. Pandoc is extensively documented and other methods for displaying math in HTML are available.

| improve this answer | |
  • 2
    Those sample command lines show how to convert a file with "TeX math" to HTML; note the ".text" and not "tex" extension. The question asks how to convert "LaTeX documents" to HTML. Can pandoc do that? – Liam Dec 30 '18 at 17:45
  • Pandoc can convert complete .tex documents to HTML, but it was not clear from the answer so I improved it. I hope this will help me remove your -1 vote :). – Doron Behar Jul 10 at 13:56

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.