addtocontents not working with certain other packages

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

\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

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).

• Probably the answer to question tex.stackexchange.com/questions/65029/… solves your problem. To be more exact: \addtocontents{toc}{\protect\setcounter{tocdepth}{0}}. – Jürgen May 19 '17 at 8:36
• 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 8:44

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).

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

...



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
}
\makeatother


so just doing \appendix is sufficient.

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