2

This is a follow up question on this

How do I change the toc dept mid document while using packages (in this case only mathtools).

An example:

\documentclass[a4paper,10pt,oneside]{book}

\usepackage{mathtools}

\begin{document}

 \frontmatter          

 \chapter{hello}
 \section{hello}
 \subsection{hello}
 \subsubsection{hello}

 \setcounter{tocdepth}{3}
 \tableofcontents

 \mainmatter

 \chapter{hello}
 \section{hello}
 \subsection{hello}
 \subsubsection{hello}

 \chapter{hello}
 \section{hello}
 \subsection{hello}
 \subsubsection{hello}

 \appendix

 \addtocontents{toc}{\setcounter{tocdepth}{0}}

 \chapter{hello}
 \section{hello}
 \subsection{hello}
 \subsubsection{hello}

 \chapter{hello}
 \section{hello}
 \subsection{hello}
 \subsubsection{hello}

\end{document}

This example generates an error and very strangely puts an exclamation mark in the ToC as you can see here example

however when I comment out the \usepackage{mathtools} it suddenly works again or replacing it with a different package such as \usepackage[T1]{fontenc} also generates no errors.

But this is not limited to just this specific package, certain other packages also introduce the same behavior (I just took this package to keep the example small).

LinG
  • 355
  • 1
    Probably the answer to question https://tex.stackexchange.com/questions/65029/problems-with-mathtools-package/65031 solves your problem. To be more exact: \addtocontents{toc}{\protect\setcounter{tocdepth}{0}}. – Jürgen May 19 '17 at 08:36
  • 1
    Yup that solves everything, thank you. Also, with regards to my comment in my previous question. I was unable to produce an error with splitting the document file into multiple tex files which I thought caused the error but after attempting to reproduce the error this turned out to be the cause. – LinG May 19 '17 at 08:44

3 Answers3

5

When you do

\addtocontents{toc}{<whatever>}

the tokens in <whatever> undergo full expansion when the writing in the .aux file takes place. So, with

\addtocontents{toc}{\setcounter{tocdepth}{0}}

the .aux file will contain

\@writefile{toc}{\global \c@tocdepth 0\relax }

(without mathtools). On the other hand, mathtools requires calc, which changes how \setcounter works and upon running LaTeX you get

! Undefined control sequence.
\@calc@post@scan ...st@scan \else \def \calc@next 
                                                  {\calc@error #1}\fi \fi \f...
l.31 ...dtocontents{toc}{\setcounter{tocdepth}{0}}

The problem is that \setcounter is turned into a fragile command. Simply going past the error will essentially produce arbitrary rubbish.

In any case, you don't want low level commands such as

\global \c@tocdepth 0\relax 

in the .toc file, so \protect should be used in front of \setcounter independently on the loading of calc.

Suppose you define a counter for deciding at run time what contents levels you want to show in the appendix.

\newcounter{appendixtocdepth}
\setcounter{appendixtocdepth}{0} % choose a value

...

\addtocontents{toc}{\protect\setcounter{tocdepth}{\arabic{appendixtocdepth}}}

Note that \arabic should not be protected against expansion, because you want the number to be evaluated.

If you're sure the level to be used is 0, then just

\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}

is sufficient. Anyway, it would be even better to incorporate the code into \appendix. In the preamble do

\makeatletter
\g@addto@macro\appendix{%
  \addtocontents{toc}{\protect\setcounter{tocdepth}{0}}%
}
\makeatother

so just doing \appendix is sufficient.

egreg
  • 1,121,712
  • Somehow, this seems to break bookmark structure in PDFs. I have a long document. If the line \addtocontents{toc}{\protect\setcounter{tocdepth}{0}} is included in appendix, the bookmark structure in PDF viewer is Chapter -> Section (but document has more substructure). Without the line, it is correctly Chapter -> Section -> Subsection -> Subsubsection. Are you aware of this issue? Something similar is this answer by Fischer but not exactly the same. Thank you. – Linear Christmas Nov 24 '22 at 20:32
  • 1
    @LinearChristmas Please, open a new question with some details and an example document showing the issue. – egreg Nov 24 '22 at 20:36
  • Yes, will try to do this tomorrow :). My apologies, I suspected you might be aware of the issue straight away, so amending this answer would have sufficed. – Linear Christmas Nov 24 '22 at 20:38
  • I asked and on a very basic level answered it a moment ago: https://tex.stackexchange.com/q/666348/116907. – Linear Christmas Nov 24 '22 at 22:03
4

In addition to my comment above: You have to add a \protect so that the line looks like \addtocontents{toc}{\protect\setcounter{tocdepth}{0}}. For an explanation you may refer to Problems with mathtools package (or to egreg's elaborated answer below).

Jürgen
  • 2,160
2

You should protect the counter:

\documentclass[a4paper,10pt,oneside]{book}

\usepackage{mathtools}

\begin{document}

 \frontmatter          

 \chapter{hello}
 \section{hello}
 \subsection{hello}
 \subsubsection{hello}

 \setcounter{tocdepth}{3}
 \tableofcontents

 \mainmatter

 \chapter{hello}
 \section{hello}
 \subsection{hello}
 \subsubsection{hello}

 \chapter{hello}
 \section{hello}
 \subsection{hello}
 \subsubsection{hello}

 \appendix

 \addtocontents{toc}{\protect\setcounter{tocdepth}{0}}

 \chapter{hello}
 \section{hello}
 \subsection{hello}
 \subsubsection{hello}

 \chapter{hello}
 \section{hello}
 \subsection{hello}
 \subsubsection{hello}

\end{document}
Ulrike Fischer
  • 327,261