Create a table of contents in LaTeX (tableofcontents)

Write \tableofcontents where the table of contents should appear. LaTeX builds it from the \section and \chapter commands of the document; it is complete after the second run.

Insert the table of contents

The first run writes the headings and page numbers to a file with the ending .toc; the second run reads that file and prints the list.

\tableofcontents

\section{Introduction}
Text of the section.

\subsection{Background}
Text of the subsection.

\section{Method}
Text of the section.
Result
The compiled example

Open in the editor

Limit the depth

The counter tocdepth holds the lowest level that is listed: 0 for chapters, 1 for sections, 2 for subsections, 3 for subsubsections. The default is 3 in article and 2 in report and book.

This goes into the preamble.

\setcounter{tocdepth}{1}

Open in the editor

Add an entry by hand

Headings with a star, such as \section*, are not listed. \addcontentsline{toc}{section}{Preface} adds an entry: the arguments are the list (toc), the level whose format the entry takes, and the text.

\tableofcontents

\section*{Preface}
\addcontentsline{toc}{section}{Preface}
Text of the preface.

\section{Introduction}
Text of the section.
Result
The compiled example

Open in the editor

Change the heading

The heading is stored in \contentsname. Without babel, \renewcommand{\contentsname}{Overview} in the preamble changes it. With babel that line has no effect, because babel sets the name again at \begin{document}; use \setlocalecaption with the language, the name without "name" and the new text.

This goes into the preamble.

\usepackage[ngerman]{babel}
\setlocalecaption{ngerman}{contents}{Inhalt}

Open in the editor

Make the entries clickable

With hyperref loaded, every entry is a link to its heading. hidelinks removes the coloured frames around the links.

This goes into the preamble.

\usepackage[hidelinks]{hyperref}

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages