1

I add contents lines containing the chapter numbers in the list of figures of a document, by using the third answer of the following question:

Include chapters in List of Figures with titletoc?

All is good except when I add an appendix, where I have the following error message:

! Missing number, treated as zero.! You can't use \numexpr in horizontal mode. \ifnumcomp ...\ifnum \numexpr #1\relax #2\numexpr...

but it compiles good ! so it gives the desired result despite the error message !

What's the cause of this error message, and how can I avoid it ?

I give the original file, where I only add the command for appendix:

\documentclass{book}
\usepackage{etoolbox}

    \usepackage[toc,page]{appendix}  %% ADDED TO THE ORIGINAL FILE

\makeatletter
\def\thischaptertitle{}
\apptocmd{\@chapter}{\gdef\thischaptertitle{#1}}{}{}

\newcommand{\DeclareDividedList}[1]%
  {\newcounter{#1@chapter}\setcounter{#1@chapter}{0}}

\pretocmd{\addcontentsline}%
  {\ifltxcounter{#1@chapter}%
   {%
     \ifnumgreater{\thechapter}{\value{#1@chapter}}{%
       \setcounter{#1@chapter}{\thechapter}%
       \addtocontents{#1}{\protect\contentsline{chapter}%
         {\protect\numberline {\thechapter} {\thischaptertitle}}{}{} }
     }{}%
   }{}%
  }{}{}
\makeatother

\DeclareDividedList{lof}
\DeclareDividedList{lot}

\usepackage[paperheight=12cm,vscale=0.9]{geometry}
\begin{document}

\tableofcontents
\listoffigures
\listoftables

\mainmatter

\chapter{Introduction with no Figures}

\chapter{Test Chapter with Figures but no Tables}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}

\chapter{Test Chapter with Tables but no Figures}
\begin{table}
\caption{caption text}
\end{table}
\begin{table}
\caption{caption text}
\end{table}

\chapter{Test Chapter with Figures and Tables}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{table}
\caption{caption text}
\end{table}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{table}
\caption{caption text}
\end{table}
\begin{figure}
\caption{caption text}
\end{figure}
%%% ADDED APPENDIX  
    \begin{appendices}
    \renewcommand{\chaptername}{Appendix}
    \chapter{Appendix One}
    \begin{figure}
    \caption{Figure 1 of appendix 1}
    \end{figure}
    \end{appendices}
\end{document}

and the result obtained:

enter image description here

1 Answer 1

2

tl;dr: When you're using a counter in a comparison or to set a counter, you want to use \value{counter}, not \thecounter. And watch out for counters resetting in the appendix.

\thecounter prints a text representation of that counter, but this is not necessarily a number (for example, the chapter becomes A). This means that commands that expect a number see a letter instead ("Missing number..."), which led to your output having a spurious "A¿A" just before the caption.

But once you use \value{counter}, you run into an additional problem: because the chapter numbering starts over in the appendix, your \ifnumgreater no longer sees a larger number, and therefore doesn't write \thischaptertitle. The solution is to reset the divided counter. This leads to:

\documentclass{book}
\usepackage{etoolbox}

    \usepackage[toc,page]{appendix}  %% ADDED TO THE ORIGINAL FILE

\makeatletter
\def\thischaptertitle{}
\apptocmd{\@chapter}{\gdef\thischaptertitle{#1}}{}{}

\newcommand{\DeclareDividedList}[1]{%
  \newcounter{#1@chapter}%
  \setcounter{#1@chapter}{0}%
  \AtBeginEnvironment{appendices}{\setcounter{#1@chapter}{0}}% added
}

\pretocmd{\addcontentsline}%
  {\ifltxcounter{#1@chapter}%
   {%
     \ifnumgreater{\value{chapter}}{\value{#1@chapter}}{% \the -> \value{}
       \setcounter{#1@chapter}{\value{chapter}}% \the -> \value{}
       \addtocontents{#1}{\protect\contentsline{chapter}%
         {\protect\numberline {\thechapter} {\thischaptertitle}}{}{} }
     }{}%
   }{}%
  }{}{}
\makeatother

\DeclareDividedList{lof}
\DeclareDividedList{lot}

\usepackage[paperheight=12cm,vscale=0.9]{geometry}
\begin{document}

\tableofcontents
\listoffigures
\listoftables

\mainmatter

\chapter{Introduction with no Figures}

\chapter{Test Chapter with Figures but no Tables}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}

\chapter{Test Chapter with Tables but no Figures}
\begin{table}
\caption{caption text}
\end{table}
\begin{table}
\caption{caption text}
\end{table}

\chapter{Test Chapter with Figures and Tables}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{table}
\caption{caption text}
\end{table}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{table}
\caption{caption text}
\end{table}
\begin{figure}
\caption{caption text}
\end{figure}
%%% ADDED APPENDIX  
    \begin{appendices}
    \renewcommand{\chaptername}{Appendix}
    \chapter{Appendix One}
    \begin{figure}
    \caption{Figure 1 of appendix 1}
    \end{figure}
    \end{appendices}
\end{document}
1

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.