3

The granularity of the table of contents is generally controlled by the counter tocdepth that can be set globally for the whole document.

Is it possible to change the granularity of the table of contents in the middle of a document, and if so, how?

I have a particular document where I want to have a tocdepth of two for the first part and a tocdepth of one in the second part (nothing more complicate).

  • The word granularity is a little bit confusing, in my point of view – user31729 Oct 12 '17 at 16:11
4

Write the \setcounter{tocdepth}{...} command to the .toc file directly with \addtocontents, as done in the \changelocaltocdepth macro:

This works since the various \l@.... macros that basically generate the contents line entry check for the tocdepth value at the time of generating the .toc via \@dottedtocline.

\documentclass{book}

\newcommand{\changelocaltocdepth}[1]{%
  \addtocontents{toc}{\protect\setcounter{tocdepth}{#1}}%
  \setcounter{tocdepth}{#1}%
}




\setcounter{tocdepth}{2}

\begin{document}

\tableofcontents

\part{One}
\chapter{Foo}

\section{Foo}

\subsection{Foo stuff}

\changelocaltocdepth{1}

\part{Two}
\chapter{Foo}

\section{Foo}

\subsection{Foo stuff}
\end{document}

enter image description here

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.