7

How do I change the ToC depth mid document?

so in this example

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

\begin{document}

 \chapter{hello}

 \frontmatter          

 \chapter{hello}

 \setcounter{tocdepth}{3}
 \tableofcontents

 \mainmatter

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

 \chapter{hello}

 \appendix

 \setcounter{tocdepth}{1}

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

\end{document}

example

But I want the ToC depth in the appendix to only be of depth 1 (while the rest of the document stays in tocdepth 3). I tried the \setcounter{tocdepth}{1} right after \appendix but that didn't do anything as you can see...

(my guess would be that the ToC is build right after I call \tableofcontents thus I can't change it afterwards this way)

Jürgen
  • 2,160
LinG
  • 355

1 Answers1

12

Write the change in tocdepth into the toc like shown in this example:

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

\begin{document}

 \chapter{hello}

 \frontmatter

 \chapter{hello}

 \setcounter{tocdepth}{3}
 \tableofcontents

 \mainmatter

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

 \chapter{hello}

 \appendix

 \addtocontents{toc}{\setcounter{tocdepth}{1}}

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

\end{document}
Jürgen
  • 2,160
  • Thank you, this solution does work for this example but it doesn't work in my real project which uses \input{} to put in all the different chapters (I use a different .tex file for every chapter).

    If I then add your line I get the error "undefined control sequence" and if I compile again that error changes to "missing } inserted. \tableofcontents".

    Any idea how to resolve this? (Else I'll just make a follow up question if this is not clear enough)

    – LinG May 18 '17 at 14:02
  • 2
    Yes, so do I (partition the text into multiple tex-files). Generally it should work, so I would propose you show another fine example (the one above is really well written). You may of course add it to this question! – Jürgen May 18 '17 at 14:07
  • 1
    I ran into the same problem as LinG, this helped: https://tex.stackexchange.com/questions/395856/switching-tocdepth-in-the-middle-of-a-document – sheß Dec 18 '19 at 20:50