46

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

28

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)

  • 3
    And this has the advantage that browsers that can display MathML (natively or via a plugin) can avoid the necessity of loading MathJaX. – Loop Space Aug 27 '12 at 7:27
  • 8
    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. As written in their website:

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

In their demos page, under clause 17, they suggest using the command:

pandoc math.text -s --mathjax -o mathMathJax.html

It will creates a single html file with a javascript reference to mathjax.js without any additional css file.

  • 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

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.