9

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

1
  • The word granularity is a little bit confusing, in my point of view
    – user31729
    Commented Oct 12, 2017 at 16:11

1 Answer 1

13

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.