Friday, January 21, 2011

TeX4ht and XeLaTeX

Tex4ht is without doubt the best solution for (La)tex to HTML conversion, but kind of hard to figure out sometimes. Here are two problems I found particularly frustrating, and some solutions.

1. Tex4ht will convert your plain tex or eplain file, only if you include this line in your source file (or at the end of an input file) after all other inputs and configurations:

\csname tex4ht\endcsname

Note there’s no space before \endcsname.

2. You may have found that Tex4ht’s htlatex command does not work when you use the fontspec package. I tried htxelatex as mentioned in the docs, but it didn’t work for me (some character encoding errors), but it wouldn’t hurt to try it anyway, just in case the problem was peculiar to my system.

If not, a nice workaround is to use the ifxetex package to hide the fontspec-related declarations from tex4ht,  like so:
 
\usepackage{ifxetex}
\ifxetex
\usepackage{fontspec}
...
\else
\fi

Conversely, you can hide commands from XeTeX by putting them between the else and fi. For example, including \usepackage{parskip} would produce unindented paragraphs only in the HTML;  Tex4ht understands common package declarations, and commands relevant to HTML output, like secnumdepth and parindent.

Hopefully this post will save a fellow novice tex-er some search time.