# htlatex does not seems to like $\mathit{}$ in subscripts

When I compilethis MWE:

\documentclass[a4paper,12pt]{article}
\begin{document}
Test $x_\mathit{in}$
\end{document}


...it works perfectly with latex and pdflatex. If I try to use htlatex on it, it chokes:

! Argument of \new:mfont has an extra }.
<inserted text>
\par
l.7 Test $x_\mathit{ in}$


What am I doing wrong?

(BTW: if someone can point me to a comprehensive document about htlatex, please do... my google-fu is quite low today).

EDIT I discovered that if I write:

...$x_{\mathit{in}}$


(with the extra set of braces) it works. But I am still wondering why...

This is standard (distribution-provided) full LaTeX installation in Ubuntu 14.04.

• I guess it's a difference in the implementation of \mathit with tex4ht. While $x_\mathit{in}$ works in LaTeX, it's better to use $x_{\mathit{in}}$ anyway. – egreg May 13 '14 at 23:17
• @egreg so it's basically a case of "doctor, it hurts if..." :-) – Rmano May 13 '14 at 23:33
• the _\mathit{} form only works because of the rather weird parsing of the tex primitive, if you had a macro (or active character) that took an argument #1 then you wouldn't be able to go _\mathit{} as #1 would just be \mathit tex4ht probably makes _ active so it can write the specials it needs in the conversion, so that makes it have the standard argument behaviour. – David Carlisle May 14 '14 at 0:21
• @DavidCarlisle I found that this is documented here: "6.2. Extra braces required. In short, when in doubt enclosed sub- and super- scripts in braces if they are longer than a single character. In this respect the syntax of the TeX language that is accepted by TeX4ht is stricter than that accepted by TeX and LaTeX. " (at stuff.mit.edu/afs/athena/system/i386_deb50/os-ubuntu-9.04/usr/… ) Thank to all for the comments. – Rmano May 14 '14 at 2:38

# Analysis

When tex4ht.sty is loaded, that is, during processing with htlatex, the character _ doesn't have category code 8 as usual in TeX, but category code 12 and it becomes “math active”, so that, when it is scanned in math mode, TeX considers it as if it were an active character, with the definition

_=macro:
->\ifmmode \expandafter \pr:sb \else \expandafter \sys:sb \fi .


So now \pr:sb remains, because we are in math mode (reserved macros of tex4ht.sty have colons in their names). Here's the definition:

\pr:sb ->\ifx \EndPicture \:UnDef \expandafter \Protect \expandafter \S:b \else
\expandafter \s:b \fi


In your case, \EndPicture (of which I ignore the function) is not defined, so what's substituted is \Protect\S:b and it's here where the problem begins. The macro \Protect is irrelevant in this case (it's similar to the usual \protect), and \S:b is

\S:b #1->\def \SuB: {#1}\futurelet \:temp \sub:sup


OK, #1 is \mathit, so we have

\def\SuB:{\mathit}\futurelet\:temp\sub:sup


I won't follow the processing any more: the idea is that what's stored in \SuB is what is to be subscripted, so you get the equivalent of

x_{\mathit}


which is definitely wrong.

If \EndPicture is defined, \s:b would be used, which produces a normal _8 character and the compilation would proceed without errors; however, the output wouldn't be correct: “in” appears in smaller type, but not as a subscript.

# Conclusion

Always use $x_{\mathit{in}}$. As explained in my answer to Why does x_\text y work?, the fact that $x_\mathit{in}$ works relies on the assumption that _ is the usual subscript character.