4

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)

6

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}
  • 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

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.