6

When I put \localtableofcontents (which comes from one of these pacakges tocloft or etoc), inside the \if....\else..\fi then I get an error.

The reason I wanted to put it inside an if is because I want the file to compile both with tex4ht and pdflatex, and I do not want this when running in tex4ht mode.

\documentclass[10pt,notitlepage]{report}

\ifdefined\HCode
\else
\usepackage{tocloft}
\usepackage{etoc}
\fi

\begin{document}
\chapter{ my chapter }

\ifdefined\HCode
\else
\localtableofcontents %this gives the error
\fi

\end{document}

now pdflatex foo.tex gives

>pdflatex foo.tex 
This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2014)
....
LaTeX2e <2014/05/01>
Babel <3.9l> and hyphenation patterns for 79 languages loaded.
(/usr/local/texlive/2014/texmf-dist/tex/latex/base/report.cls
Document Class: report 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2014/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2014/texmf-dist/tex/latex/tocloft/tocloft.sty)
(/usr/local/texlive/2014/texmf-dist/tex/latex/etoc/etoc.sty
(/usr/local/texlive/2014/texmf-dist/tex/latex/tools/multicol.sty)) (./foo.aux)
Chapter 1.
! Extra \fi.
<recently read> \fi 

l.15 \fi

How would I prevent \localtableofcontents from being used in tex4ht mode if I can't exclude it using \ifdefined\HCode logic?

>pdflatex foo.tex 
This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2014) (preloaded format=pdflatex)

File list:

*File List*
  report.cls    2014/09/29 v1.4h Standard LaTeX document class
  size10.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
 tocloft.sty    2013/05/02 v2.3f parameterised ToC, etc., typesetting
    etoc.sty    2014/04/22 v1.07l Completely customisable TOCs (jfB)
multicol.sty    2014/10/28 v1.8i multicolumn formatting (FMi)
Nasser
  • 20,220
  • I have added something to my answer to http://tex.stackexchange.com/questions/5153/is-there-an-iftex4ht-command-and-if-not-how-should-it-be-defined – egreg Jan 05 '15 at 11:06
  • @egreg thank you. I looked at it and will study it more. – Nasser Jan 05 '15 at 11:53
  • just for people reading this question in the future: \localtableofcontents indeed comes from etoc, not tocloft. –  Jan 05 '15 at 21:15

1 Answers1

7

I don't know why, but it seems an expansion problem.

Adding \expandafter before \localtableofcontents solves the issue.

MWE:

\documentclass[10pt,notitlepage]{report}

\ifdefined\HCode
\else
\usepackage{tocloft}
\usepackage{etoc}
\fi

\begin{document}
\chapter{ my chapter }

\ifdefined\HCode
\else
\expandafter\localtableofcontents %no error
\fi

\end{document} 

Output (PDFLaTeX)

enter image description here

As an alternative, as suggested by jfbu, you can load the etoolbox package and use \ifdef instead of \ifdefined in the following way

\documentclass[10pt,notitlepage]{report}

\usepackage{etoolbox}

\ifdef{\HCode}{}{%
\usepackage{tocloft}%
\usepackage{etoc}%
}

\begin{document}
\chapter{ my chapter }

\ifdef{\HCode}{}{%
\localtableofcontents%
}

\end{document} 
karlkoeller
  • 124,410
  • thanks, nice workaround. I confirmed it works ok on my end. – Nasser Jan 05 '15 at 06:50
  • 1
    etoc's \tableofcontents and \localtableofcontents both fetch using \futurelet the token coming next, to implement its \label/\ref mechanism. If this token is a \fi then a problem arises due to the peculiarities of TeX's \if..\else..\fimechanism. With \expandafter the problem indeed is solved. A recommended alternative is to load package etoolbox and use (in the context of the OP's question) \ifdef{\HCode}{}{\localtableofcontents} instead. –  Jan 05 '15 at 07:14