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.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}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.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}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}Pitfalls
- After you add, remove or rename a heading, the table of contents shows the old state until you compile twice.
- In
articlethe text continues directly below the table of contents. Write\newpageafter\tableofcontentsto give it a page of its own.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- How-to Sections, subsections and chapters in LaTeX
- How-to Unnumbered sections in LaTeX
- How-to List of figures and list of tables in LaTeX
- Package
tocloft - Package
hyperref - Package
babel