Table of Contents in LaTeX: \tableofcontents, Depth and Styling
Quick answer
- Command:
\tableofcontentsafter\maketitle, then compile twice (the first run only writes the.tocfile). - Depth:
\setcounter{tocdepth}{1}lists sections only;2adds subsections. - Unnumbered entries:
\section*{Preface}then\addcontentsline{toc}{section}{Preface}. - Lists and links:
\listoffigures,\listoftables;\usepackage{hyperref}makes every entry clickable. - Styling and title:
tocloftortitletocfor fonts, dots and spacing;\renewcommand{\contentsname}{Table of Contents}renames “Contents”.
\documentclass{article}
\usepackage{hyperref}
\setcounter{tocdepth}{2}
\renewcommand{\contentsname}{Table of Contents}
\begin{document}
\tableofcontents
\listoffigures
\section*{Preface}
\addcontentsline{toc}{section}{Preface}
\section{Introduction}
\subsection{Background}
\begin{figure}[h]
\centering
\rule{3cm}{1cm}
\caption{A placeholder figure}
\end{figure}
\end{document}A table of contents (TOC) lists the chapters and sections of a document with their page numbers. In LaTeX you never type the TOC by hand: one command, \tableofcontents, builds it from your headings and keeps the page numbers right as the document grows. This guide starts with a three-line version and complete examples, then covers depth, unnumbered entries, clickable links, renaming, styling with tocloft, lists of figures and tables, and troubleshooting.
A Table of Contents in Three Lines
The smallest working document needs only the command itself and a few headings:
\documentclass{article}
\begin{document}
\tableofcontents
\section{Introduction}
The table of contents above lists this section.
\subsection{Background}
And this subsection.
\section{Method}
And this one.
\end{document}The result is a “Contents” heading followed by three entries: section 1 in bold with its page number, subsection 1.1 indented with a dotted leader, and section 2. There is one catch: you have to compile twice.
LaTeX stores headings and page numbers in auxiliary files. The contents read the previous run's .toc file, so an empty first pass is normal. Compile again before diagnosing missing entries.
Use latexmk -pdf main.tex to rerun LaTeX until the auxiliary files stabilize. Two runs usually populate a small contents page; a larger contents page may need another pass because its new height changes later page numbers.
Controlling Depth: tocdepth and secnumdepth
Each heading command has a level number. The counter tocdepth decides the deepest level that appears in the contents:
| Command | Level | Classes |
|---|---|---|
\part | −1 (0 in article) | all |
\chapter | 0 | report, book |
\section | 1 | all |
\subsection | 2 | all |
\subsubsection | 3 | all |
\paragraph | 4 | all |
\subparagraph | 5 | all |
The defaults are tocdepth 3 in article (down to subsubsections) and 2 in report and book (down to subsections). A second counter, secnumdepth, decides the deepest level that gets a number, with the same defaults. The two are independent: you can number subsubsections but leave them out of the contents, or list headings that have no number.
\documentclass{report}
\setcounter{tocdepth}{1}
\setcounter{secnumdepth}{3}
\begin{document}
\tableofcontents
\chapter{Main Part}
\section{A section}
\subsection{Numbered, not listed}
\subsubsection{Numbered as 1.1.1.1}
\appendix
\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}
\chapter{Raw Data}
\section{Hidden from the contents}
\end{document}The contents show Main Part, A section and Raw Data. Set the overall depth before printing the contents; changing it afterward cannot alter entries already read.
To change the depth for one part of the document only, the last trick in the example writes the counter change into the .toc file with \addtocontents. It takes effect at that point in the contents, so every entry after it (here the appendix) is filtered at the new depth. To restore the depth for a later chapter, add another \addtocontents line with the old value. The \protect is harmless with the current LaTeX kernel and keeps the line working on older installations.
Adding Unnumbered Sections, the Bibliography and Appendices
Starred headings such as \section* and \chapter* are unnumbered and never written to the contents. To list one anyway, add the entry yourself with \addcontentsline{toc}{level}{text} right after the heading. The bibliography, index and lists of figures are unnumbered chapters or sections too, and the tocbibind package adds them for you:
\documentclass{report}
\usepackage[nottoc]{tocbibind}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\chapter*{Preface}
\phantomsection
\addcontentsline{toc}{chapter}{Preface}
This chapter has no number but still appears in the contents.
\chapter{Introduction}
See \cite{knuth84}.
\section*{A note on notation}
\phantomsection
\addcontentsline{toc}{section}{A note on notation}
Unnumbered, listed, and the link lands here.
\appendix
\chapter{Proofs}
Appendix chapters are lettered A, B, \ldots{} in the contents.
\begin{thebibliography}{9}
\bibitem{knuth84} D. E. Knuth, \emph{The \TeX book}, Addison-Wesley, 1984.
\end{thebibliography}
\end{document}The contents of this document read:
Contents
Preface 2
1 Introduction 3
A note on notation . . . . . . . . 3
A Proofs 4
Bibliography 5\phantomsectionmatters only with hyperref. It drops an invisible link target at the current spot. Current hyperref already gives\section*and\chapter*a target of their own, but older installations do not, and an\addcontentslinethat is not right after a heading never has one: the entry then links to the previous target, often on the wrong page. Keeping\phantomsectioncosts nothing.\appendixswitches chapter numbering to letters (or section numbering in article) from that point on, so appendix entries show up as A, B, C with no extra work.tocbibindadds the bibliography, index, list of figures and list of tables. Thenottocoption stops it from also listing the table of contents inside itself, which is almost never wanted. It works withthebibliographyand BibTeX but not with biblatex, whose\printbibliographyignores it; there, write\printbibliography[heading=bibintoc]instead.
For a document with no section numbers at all, starring every heading is the wrong tool. Keep the normal commands and lower secnumdepth instead: 0 removes the section numbers in article, -1 also removes the chapter numbers in report and book, and \setcounter{secnumdepth}{-2} switches off every number, parts included. The numbers disappear but every heading stays in the contents. The sections guide covers the heading commands themselves in more detail.
Clickable Table of Contents with hyperref
Loading hyperref is all it takes to turn every contents entry into a link and to fill the bookmark panel of the PDF viewer. Load it after the other packages; it redefines many commands and most packages expect to come before it.
\documentclass{article}
\usepackage{hyperref}
\hypersetup{
colorlinks = true,
linkcolor = blue,
urlcolor = cyan,
linktoc = all,
bookmarksnumbered = true,
pdftitle = {Clickable contents}
}
\begin{document}
\tableofcontents
\section{Results for \texorpdfstring{$x^2$}{x squared}}
Text.
\section{Discussion}
Text.
\end{document}By default hyperref draws a red box around each link, visible on screen and not in print. colorlinks colours the text instead; linkcolor is the option that controls the contents and cross-references. linktoc chooses what is clickable: section (the default, the title only), page, all or none. For a version that will be printed, or a thesis whose rules forbid coloured text, keep the links but hide them:
\usepackage[hidelinks]{hyperref}The \texorpdfstring in the first heading is there because a PDF bookmark can only hold plain text. Math or formatting commands in a heading make hyperref print Package hyperref Warning: Token not allowed in a PDF string (Unicode) and drop the offending part from the bookmark; \texorpdfstring{TeX version}{plain version} gives it a plain alternative. More link options, including URL styling and \autoref, are in the hyperlinks guide.
Renaming and placing the contents
Redefine \contentsname in the preamble to change the title. With babel, put the redefinition inside the active language caption hook, such as\addto\captionsenglish, so language selection does not overwrite it. In article, the contents continue on the current page; use \clearpageafterward if the body should begin on a fresh page. Report and book start the contents with a chapter-style heading on a new page.
\documentclass{book}
\renewcommand{\contentsname}{Table of Contents}
\begin{document}
\frontmatter
\tableofcontents
\chapter{Acknowledgements}
Thanks to the laboratory team.
\mainmatter
\chapter{Introduction}
\section{Background}
The main text starts here.
\end{document}In book, \frontmatter uses Roman page numbers and unnumbered chapters that still appear in the contents. \mainmatter restarts Arabic page numbers and chapter numbering. Article and report do not provide these commands; use \pagenumbering with roman and arabic instead, placing a page break before each switch. Hiding a printed page number does not change its underlying value or prevent duplicate PDF destinations when numbering is reset.
Styling with tocloft
Use tocloft to adjust entry fonts, indents and dotted leaders. This report gives chapters bold sans-serif text and sections italic text with tighter dots.
\documentclass{report}
\usepackage{tocloft}
\renewcommand{\cftchapfont}{\sffamily\bfseries}
\renewcommand{\cftchappagefont}{\sffamily\bfseries}
\setlength{\cftbeforechapskip}{1.2em}
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}}
\renewcommand{\cftdotsep}{3}
\renewcommand{\cftsecfont}{\itshape}
\renewcommand{\cftsecpagefont}{\itshape}
\setlength{\cftsecindent}{2em}
\setlength{\cftbeforesecskip}{0.3em}
\renewcommand{\cfttoctitlefont}{\hfill\Large\bfseries}
\renewcommand{\cftaftertoctitle}{\hfill}
\begin{document}
\tableofcontents
\chapter{Introduction}
\section{Scope}
\section{Outline}
\chapter{Theory}
\section{Model}
\subsection{Assumptions}
\end{document}The most common request is dots after the top-level entries, which the standard classes leave out: chapters in report and book, sections in article. One line adds them. In article:
\usepackage{tocloft}
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}}and \cftchapleader in the same way for chapters. To remove dots everywhere, set \renewcommand{\cftdotsep}{\cftnodots}. Do not combine tocloft with memoir, which has the same commands built in, or with the KOMA-Script classes, which style their contents through tocbasic.
List of Figures and Tables
\listoffigures and \listoftables work exactly like \tableofcontents, only their entries come from the \caption of each figure and table and are stored in the .lof and .lot files. Long captions make poor list entries, so give a short version in the optional argument:
\documentclass{article}
\usepackage{graphicx}
\usepackage[nottoc]{tocbibind}
\begin{document}
\tableofcontents
\listoffigures
\listoftables
\clearpage
\section{Data}
\begin{figure}[ht]
\centering
\rule{4cm}{2cm}
\caption[Sensor layout]{Sensor layout in the test field, with the three
depth levels marked and the reference probe at the centre.}
\end{figure}
\begin{table}[ht]
\centering
\caption{Mean moisture by depth}
\begin{tabular}{lr}
Depth & Moisture \\ \hline
10 cm & 21\,\% \\
30 cm & 27\,\% \\
\end{tabular}
\end{table}
\end{document}The list of figures shows only “1 Sensor layout”, while the figure itself keeps the full caption. Thanks to tocbibind the table of contents now starts with List of Figures and List of Tables entries. Without that package, add them by hand with \addcontentsline{toc}{section}{\listfigurename} directly before \listoffigures (use chapter as the level in report and book, after a \cleardoublepage so the page number is right). See the guide to figures and images and the tables guide for the floats themselves.
Common Errors
| Symptom or message | Cause | Fix |
|---|---|---|
| Table of contents is empty or shows old titles | The contents come from the .toc file of the previous run | Compile again until page numbers settle, or use latexmk |
| No file main.toc. in the log | Normal on the first run: the file has not been written yet | Nothing; it goes away on the second run |
| A section is missing | Starred heading (\section*), or a level deeper than tocdepth | \addcontentsline after the heading, or raise tocdepth |
| Page numbers in the contents are off by one or two after edits | The contents grew or shrank and moved the body, but only one run happened since | Run LaTeX once more; latexmk detects this and reruns by itself |
| Package hyperref Warning: Token not allowed in a PDF string (Unicode) | Math or a formatting command in a heading that goes into a PDF bookmark | \texorpdfstring{tex}{plain text} in the heading |
| Contents link jumps to the wrong page | \addcontentsline with no link target before it (older hyperref, or an entry not tied to a heading) | \phantomsection right before \addcontentsline |
tocdepth seems to be ignored | Set after \tableofcontents, which is too late for that table | Move \setcounter to the preamble, or use \addtocontents as shown above |
| Renamed or deleted headings still listed | A stale .toc file from an earlier run, often after a failed compile | Delete the auxiliary files (latexmk -c, or .aux, .toc, .out) and compile again |
\renewcommand{\contentsname} has no effect | babel resets the caption at \begin{document} | \addto\captionsenglish{...} as shown above |
Frequently Asked Questions
How do I make a table of contents in LaTeX?
Put \tableofcontents where the contents should appear, usually right after \maketitle, and use \chapter, \section and \subsection for your headings. Compile twice: the first run writes the headings to a .toc file and the second run prints them. latexmk repeats runs until auxiliary files stabilize; check the build log if your editor still shows stale entries.
Why is my LaTeX table of contents empty?
The contents are read from the .toc file written by the previous run, so after a single compile the table is empty or out of date. Compile again, or use latexmk. If it stays empty, check that your headings are not starred (\section* never goes to the contents) and that tocdepth is not set too low.
How do I add an unnumbered section to the table of contents?
Follow the starred heading with \addcontentsline: \section*{Preface} then \addcontentsline{toc}{section}{Preface}. With hyperref, put \phantomsection before \addcontentsline so the link jumps to the right page.
How do I limit the table of contents to chapters and sections?
Set the tocdepth counter in the preamble: \setcounter{tocdepth}{1} lists parts, chapters and sections only. The levels are part -1 (0 in article), chapter 0, section 1, subsection 2, subsubsection 3, paragraph 4 and subparagraph 5. The setting must come before \tableofcontents.
How do I make the table of contents clickable?
Load hyperref, normally as the last package: \usepackage{hyperref}. Every entry becomes a link and the PDF gets bookmarks. Add linktoc=all to make the page numbers clickable too, colorlinks to colour the text instead of drawing boxes, or hidelinks for a print version with no visible link styling.
How do I add dots to the table of contents in LaTeX?
In article, subsections and lower levels have dotted leaders by default, but sections and parts do not. In report and book, sections have dots but chapters and parts do not. Load tocloft and redefine the leader, for example \renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}} for sections in article, or \cftchapleader for chapters in report and book.
Related guides and a place to test
Paste a complete example into the LaTeX preview tool to inspect the PDF before adding it to your project.
A table of contents that is never out of date
Octree is an AI-powered LaTeX editor with TeX Live already set up and every compile run to completion, so the contents, page numbers and links are always current. Paste any example above, or ask for "dots after the chapters" in plain English.
Try Octree Free →