[Tex/LaTex] How to hide/show section levels in the table of contents

table of contents

I have a table of contents (\tableofcontents) in my document. How can I hide subsections in it? I want that only names of sections are displayed there.

Best Answer

The tocdepth counter decides to which depth down the entries appear in the ToC.

(Code slightly edited, thanks to https://tex.stackexchange.com/users/1357/jdh for pointing me to a typo!)

Usually, following values hold

  • \setcounter{tocdepth}{-1} -- only parts
  • \setcounter{tocdepth}{0} -- only parts and chapters
  • \setcounter{tocdepth}{1} -- part,chapters,sections
  • \setcounter{tocdepth}{2} -- part,chapters,sections, subsections
  • \setcounter{tocdepth}{3} -- part,chapters,sections, subsections,subsubsections
  • \setcounter{tocdepth}{4} -- part,chapters,sections, subsections,subsubsections and paragraphs
  • \setcounter{tocdepth}{5} -- part,chapters,sections, subsections, subsubsections, paragraphs and subparagraphs.

The memoir class adds the ToC depth -2 for the \book structure level at all, being higher leveled than \part.

This means, the larger the number for the counter, the greater the number of structuring in the ToC.

The toclevels can be found, for example in article.cls (and since book.cls loads article.cls) looking at the \@startsection{...} commands.

If for example, some parts of the structuring should not appear in the ToC at all, there's a trick

\addtocontents{toc}{\setcounter{tocdepth}{-10}}

before the 'hidden parts' and later reenable them with

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

See Switching tocdepth in the middle of a document for an example about changing the tocdepth in between.

Please do not confuse tocdepth with the secnumdepth counter!


Just for completeness: Both tocdepth and secnumdepth are counters that aren't defined with\newcounter, but have the internal names \c@tocdepth and \c@secnumdepth in order to allow \setcounter etc. usage. You won't find them in the internal counter list and are no members of counter reset lists by default, which would be no of importance anyway.

The value of the tocdepth counter controls in the same manner which entries enter the regular bookmarks provided by hyperref.