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

  .... latex code which tex4ht is not supposed to use

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

%thanks to solution from
%this is meant only for pdf, not HTML.
     \expandafter\newwrite\csname tf@#1\endcsname
     \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax

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) 
 (preloaded format=latex)
 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
! You can't use `\spacefactor' in vertical mode.
                 \@m {}
l.19     \@

TL 2015, on Linux.

  • 1
    You could try moving \newif\ifshowlistof out of the \ifdefined\HCode condition. May 13, 2016 at 5:26

1 Answer 1


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


the code will work.

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.