[tex4ht] How I used tex4ht to produce HTML from LaTeX

Michal Hoftich michal.h21 at gmail.com
Thu Jun 4 16:21:04 CEST 2015

Hi Bob,

I have some notes :)

> With these goals in mind, and knowing almost nothing about eBooks, I began my
> learning process. I am a big fan of open standards, so I decided that epub
> would be the best format for my book. Further research taught me that epub is
> based on xhtml. So what I needed was a process to convert LaTeX to xhtml. The
> next thing I learned is that this conversion is not straightforward. The real
> problem is that the features that make LaTeX great for authoring technical
> works — rendering equations, drawing figures, formatting pages, etc. — are not
> implemented very well in xhtml; they are "under construction."

You may have a look at tex4ebook [1], which converts LaTeX to epub, mobi
and epub3. From these formats, epub3 supports vector figures in svg
format and equations in mathml. The only problem is that there are only
few ebook readers which can read it, but it can't change if nobody
publishes books in epub3.

> Another result of my not using the latest HTML technology is that the
> online book should be more widely available. Some mobile devices may
> not handle HTML5, MathJax, or MathML. It seems appropriate to use the
> "lowest common denominator" for the free online version.

yes, there can be a problem with math on mobile devices, as they have
usually Webkit or Chrome based browsers, which don't support mathml and
mathjax can be quite slow on their hardware. On the other hand, using
images for math has also many drawbacks (using different fonts than rest
of document, poor support for different screen resolutions (unless you
use svg))

>   • The first thing I did was to create a mechanism to select between
>   the three versions —paper, PDF, HTML — within the existing LaTeX
>   source code. At the beginning of my main file I used the following
>   code:
>          \def\web{0}
>          \def\pdf{1}
>          \def\paper{2}
>          \include{bookType}

You may also detect tex4ht with construct like

      tex4ht stuff
      pdf and print stuff

>   • Following Prof. Fox's example, I decided to use tex4ht for my conversion.
>     The font I was using did not render correctly in the HTML version. Rather
>     than attempt to figure this out, I simply went with another font for the
>     HTML version:
>          \ifx\doctype\web
>             \usepackage{lmodern}
>          \else
>             \usepackage{dejavu}
>          \fi

I recently posted a huge patch with support for lot of fonts in T1 font
encoding, dejavu was among them. But using default fonts with tex4ht is
generally a good idea when font handling fails.

>          \Preamble{html}
>          \begin{document}
>            \Css {.ec-lmtk-10{font-weight: bold;}}
>          \EndPreamble

if you want to create xhtml files, replace \Preamble{html} with
\Preamble{xhtml}. You can also add other options after xhtml, like
\Preamble{xhtml,info,2} in your case

>            \Css {.ec-lmtk-10{ font-family: monospace; font-weight: bold;}}
>     but that did not work. Although it's not clear (to me) in the tex4ht
>     documentation, it seems that the \Css entries will be added to the default
>     entries, not replace them.

yes, it is true, css parser would be needed to implement replacing
instead of adding :)

>   • I wanted to use MathML or MathJax for the equations. However, this
>   rendered the equations in SVG. After the first encounter with SVG,
>   the remaining HTML code switched from span markup to tspan, which
>   caused all the font styles, weight, etc. (italic, boldface, etc.) to
>   disappear. I settled for the default, which is to render equations
>   in png.

I presume this was the issue with Tikz from your other email. If you
didn't configure anything, equations are converted to png by default.
You can convert them to mathml with `mathml` option (you can add it to
the \Preamble command). To enable mathjax, all you need to do is to add


to the .cfg file. See also [2] for complex cfg file, [3] for some
discussion about it and [4] for example conversion of publication with
multiple chapters.

> I'm sure that I've missed something important here. And I've probably
> made some mistakes or could have done some things a better way. But
> this meets my needs for now. I hope it helps anyone who is dealing
> with some of these problems.  And, as always, I welcome any
> corrections or suggestions about how to improve things.

I hope that this will help you, thank you for sharing your setup.

Best regards,

[1] https://github.com/michal-h21/tex4ebook
[2] https://github.com/michal-h21/tex4ht-enhanced-web/blob/master/enhanced.cfg
[3] http://tex.stackexchange.com/a/239944/2891
[4] http://michal-h21.github.io/reyman/thesis.html

More information about the tex4ht mailing list