# tex4ht gives an error for code it is NOT supposed to look at! \spacefactor' in vertical mode error

I have never seen anything like this before. I use some Latex code which works in pdf only. The code is surrounded by

\ifdefined\HCode
\else
.... latex code which tex4ht is not supposed to use
\fi


I just found out that tex4ht gives an error due to the code it is not supposed to look at. How could this be possible? The code is only supposed to run in pdf mode. When I delete the code below the \else, tex4ht gives no error.

Here is the MWE

\documentclass[11pt]{report}%
\ifdefined\HCode
\else
%thanks to solution from
%http://tex.stackexchange.com/questions/309244/how-to-show-list-of-figures-and-list-of-tables-inside-chapters-only-and-not-also#309245
%this is meant only for pdf, not HTML.
\newif\ifshowlistof
\makeatletter
\def\@starttocbutdonotshowtoc#1{%
\begingroup
\makeatletter
\setcounter{tocdepth}{\mytocdepth}
\ifshowlistof
\@input{\jobname.#1}%
\fi
\if@filesw
\expandafter\newwrite\csname tf@#1\endcsname
\immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
\fi
\@nobreakfalse
\endgroup}
\renewcommand{\listoftables}{%
\@starttocbutdonotshowtoc{lot}%
}
\makeatother
\fi
\begin{document}
test
\end{document}


now pdflatex gives no error. Since I know the code works in pdf. But compiling with tex4ht gives very strange error:

htlatex  foo.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015)
restricted \write18 enabled.
entering extended mode
LaTeX2e <2015/10/01> patch level 2
Babel <3.9m> and hyphenation patterns for 79 languages loaded.
(./foo.tex (/usr/local/texlive/2015/texmf-dist/tex/latex/base/report.cls
Document Class: report 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/size11.clo))
(/usr/local/texlive/2015/texmf-dist/tex/generic/tex4ht/tex4ht.sty)
! You can't use \spacefactor' in vertical mode.
\@->\spacefactor
\@m {}
l.19     \@
nobreakfalse
?


TL 2015, on Linux.

• You could try moving \newif\ifshowlistof out of the \ifdefined\HCode condition. – Sunilkumar KS May 13 '16 at 5:26

When you're compiling with tex4ht, the test \ifdefined\HCode returns true, so TeX wants to discard the false branch. So it goes from \else up to the matching \fi without expanding macros or executing commands, but matching conditionals.

Since \ifshowlistof is not a conditional when \else is scanned, it never becomes one, so the matching \fi is the one in the body of \def\@starttocbutdonotshowtoc. Note that neither \makeatletter is executed, so @ is still an “other character”. The test \if@filesw returns false (because \if compares @ and f) and finally \@ where \@nobreakspace was intended raises the error.

Solution: never put \newif in conditional text. If you do

\newif\ifshowlistof
\ifdefined\HCode
\else
...
\fi


the code will work.