Table of Contents in LaTeX: \tableofcontents, Depth, Links and Styling

A table of contents lists the chapters and sections of a document with their page numbers. In LaTeX you never type it 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 a complete table of contents example, then covers depth, unnumbered entries, clickable links, renaming, styling with tocloft, lists of figures and tables, and Beamer outlines. Every example was compiled on TeX Live 2025.

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 reads the source once from top to bottom. When it reaches \tableofcontents it has not seen the headings yet, so it cannot know their titles or page numbers. Instead, every \section writes a line to an auxiliary file called main.toc (named after your .tex file), and \tableofcontents reads that file on the next run. On the very first compile the file does not exist yet, so the log says No file main.toc. and the contents are blank. That message is informational, not an error. The second run prints the entries; if the contents themselves push the body onto a later page, a third run settles the page numbers.

You rarely do this by hand. latexmk -pdf main.tex reruns LaTeX until the auxiliary files stop changing, and online editors, Octree included, do the same on every compile.

Table of Contents Example

Here is a complete, compilable report with a title page, four chapters, sections and subsections. It uses the lipsum package for filler text so the chapters span several pages and the page numbers in the contents are realistic:

\documentclass[11pt]{report}
\usepackage[T1]{fontenc}
\usepackage{lipsum}   % dummy text

\title{Soil Moisture Sensing on a Budget}
\author{A. Student}
\date{October 2026}

\begin{document}
\maketitle
\tableofcontents

\chapter{Introduction}
\lipsum[1-2]
\section{Motivation}
\lipsum[3]
\section{Contributions}
\lipsum[4]

\chapter{Related Work}
\section{Capacitive Sensors}
\lipsum[5-6]
\subsection{Calibration}
\lipsum[7]
\subsection{Temperature Drift}
\lipsum[8]
\section{Resistive Sensors}
\lipsum[9-10]

\chapter{Method}
\section{Hardware}
\lipsum[11-12]
\section{Firmware}
\lipsum[13]
\subsection{Sampling}
\lipsum[14]
\subsubsection{Not in the contents}
Report sets tocdepth to 2, so this level is left out.

\chapter{Conclusion}
\lipsum[15]
\end{document}

The compiled PDF has nine pages. The title page comes first, then a page headed “Contents” in large bold type. It reads, top to bottom: 1 Introduction … 2, with the chapter in bold and its page number flush right but no dots; then 1.1 Motivation and 1.2 Contributions, indented, with dotted leaders to pages 2 and 3; then 2 Related Work … 4 with sections 2.1 and 2.2 and, indented one step further, subsections 2.1.1 Calibration and 2.1.2 Temperature Drift; then 3 Method … 6 with 3.1, 3.2 and 3.2.1 Sampling; and finally 4 Conclusion … 8. The subsubsection “Not in the contents” is missing, and it is also printed without a number, because the report and book classes stop numbering and listing at the subsection level. The next section shows how to change that.

The contents get a page of their own because \chapter always starts a new page. In article the top-level entries are sections, which are bold and have no dots.

Controlling Depth: tocdepth and secnumdepth

Each heading command has a level number. The counter tocdepth decides the deepest level that appears in the contents:

CommandLevelClasses
\part−1 (0 in article)all
\chapter0report, book
\section1all
\subsection2all
\subsubsection3all
\paragraph4all
\subparagraph5all

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}     % chapters and sections only
\setcounter{secnumdepth}{3}  % but number down to subsubsections
\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}}  % appendices: chapters only
\chapter{Raw Data}
\section{Hidden from the contents}
\end{document}

The contents of this document show 1 Main Part, 1.1 A section and A Raw Data, while the body numbers the subsubsection 1.1.1.1. The value that counts is the one in force when \tableofcontents runs, so put \setcounter{tocdepth} in the preamble: a \setcounter{tocdepth}{1} placed after \tableofcontents has no effect on the table.

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}   % bibliography (and index, lists) in the TOC
\usepackage{hyperref}            % load last

\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
  • \phantomsection matters 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 \addcontentsline that is not right after a heading never has one: the entry then links to the previous target, often on the wrong page. Keeping \phantomsection costs nothing.
  • \appendix switches 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.
  • tocbibind adds the bibliography, index, list of figures and list of tables. The nottoc option stops it from also listing the table of contents inside itself, which is almost never wanted. It works with thebibliography and BibTeX but not with biblatex, whose \printbibliography ignores 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,     % colour of TOC entries and \ref
  urlcolor   = cyan,
  linktoc    = all,      % title and page number are both links
  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 Table of Contents

The heading is stored in \contentsname, “Contents” by default. Redefine it in the preamble:

\renewcommand{\contentsname}{Table of Contents}

If you load babel, that line is overwritten at \begin{document}, when babel sets the captions for the language. Hook the change into the language's caption list instead (use \captionsngerman, \captionsfrench and so on for other languages). This complete article also puts the title and the contents on their own unnumbered pages and starts the body at page 1:

\documentclass{article}
\usepackage[english]{babel}
\addto\captionsenglish{\renewcommand{\contentsname}{Table of Contents}}
\begin{document}
\title{Lab Report}\author{A. Student}\maketitle
\thispagestyle{empty}
\clearpage
\tableofcontents
\thispagestyle{empty}
\clearpage
\setcounter{page}{1}
\section{Aim}
Text.
\section{Results}
Text.
\end{document}

In article the contents simply continue on the current page, so a \clearpage (or \newpage) after them moves the body to a fresh page; \clearpage also flushes pending figures, which is usually what you want. \thispagestyle{empty} after \tableofcontents removes the page number from the contents page (if the contents run over several pages, only the last one is affected). In report and book the contents are a chapter of their own and always start on a new page.

Books and theses usually number the front matter in roman numerals. The book class does that with \frontmatter and \mainmatter:

\documentclass{book}
\renewcommand{\contentsname}{Table of Contents}
\begin{document}
\frontmatter            % roman page numbers, unnumbered chapters
\begin{titlepage}
  \centering\Huge My Thesis
\end{titlepage}
\tableofcontents
\chapter{Acknowledgements}
Thanks.

\mainmatter             % arabic numbers restart at 1
\chapter{Introduction}
\section{Background}
Text.
\chapter{Results}
Text.
\end{document}

The contents land on page iii, list Acknowledgements … v without a number, and then 1 Introduction … 1. In the front matter \chapter behaves like \chapter* for numbering but still writes a contents entry. Report and article do not have these commands; use \pagenumbering{roman} before the contents and \pagenumbering{arabic} after them, as shown in the page numbering guide. Nothing forces the contents to the front, either: \tableofcontents works anywhere, including at the end of the document.

Styling with tocloft

The tocloft package exposes the look of every contents entry as a set of macros and lengths whose names follow one pattern: \cft + the level (part, chap, sec, subsec, subsubsec, also fig and tab for the lists) + the property. The ones you will use most:

  • \cftsecfont and \cftsecpagefont: the font of the title and of the page number
  • \cftsecleader: what fills the gap between them (dots or blank space)
  • \cftbeforesecskip: vertical space above each entry
  • \cftsecindent and \cftsecnumwidth: the indent of the entry and the room reserved for its number
  • \cftdotsep: the spacing of the dots, 4.5 by default; larger is sparser
\documentclass{report}
\usepackage{tocloft}

% Chapter entries: sans-serif bold, no dots, a little more space above
\renewcommand{\cftchapfont}{\sffamily\bfseries}
\renewcommand{\cftchappagefont}{\sffamily\bfseries}
\setlength{\cftbeforechapskip}{1.2em}

% Section entries: dotted leaders (the default for sections in report),
% wider dot spacing and a deeper indent
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}}
\renewcommand{\cftdotsep}{3}
\renewcommand{\cftsecfont}{\itshape}
\renewcommand{\cftsecpagefont}{\itshape}
\setlength{\cftsecindent}{2em}
\setlength{\cftbeforesecskip}{0.3em}

% Title of the contents
\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}}  % dots for sections

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.

If you already format headings with titlesec, its companion titletoc is the alternative. Its \titlecontents command describes a whole entry at once (indent, label, title, filler and page) and \dottedcontents is a shortcut for the usual dotted line, for example \dottedcontents{section}[3.8em]{}{2.3em}{1pc}. It is more flexible than tocloft for unusual layouts, such as entries that run into one paragraph, but for changing fonts, spacing and dots tocloft is simpler.

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}   % adds the lists to the TOC
\begin{document}
\tableofcontents
\listoffigures
\listoftables
\clearpage

\section{Data}
\begin{figure}[ht]
  \centering
  \rule{4cm}{2cm}   % stand-in for \includegraphics
  \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.

Table of Contents in Beamer

Beamer slides use the same \tableofcontents, placed inside a frame. Its optional argument filters and highlights the entries, which is how most talks get an outline slide at the start of every section:

\documentclass{beamer}
\usetheme{Madrid}
\title{Soil Moisture Sensing}
\author{A. Student}

\AtBeginSection[]{
  \begin{frame}{Outline}
    \tableofcontents[currentsection, hideallsubsections]
  \end{frame}
}

\begin{document}
\frame{\titlepage}

\begin{frame}{Outline}
  \tableofcontents
\end{frame}

\section{Introduction}
\subsection{Motivation}
\begin{frame}{Why measure moisture?}
  Irrigation scheduling.
\end{frame}

\section{Method}
\subsection{Hardware}
\begin{frame}{The sensor}
  A capacitive probe.
\end{frame}

\section{Results}
\begin{frame}{Findings}
  It works.
\end{frame}
\end{document}

The result is eight slides: the title, a full outline with sections and subsections, and before each of the three sections an “Outline” slide that shows the section titles only, with the current one highlighted and the others shaded. The options that matter most:

  • currentsection: highlight the current section and shade the rest
  • hideallsubsections / hideothersubsections: drop all subsections, or all except those of the current section
  • sections={2-3}: show only some sections, useful to split a long outline over two slides
  • pausesections: reveal the outline one section at a time

The empty [] after \AtBeginSection is the code used for starred sections, so \section* gets no outline slide. Frame titles do not appear in the outline; only \section and \subsection do, and they must sit outside frames. Beamer also needs two runs before the outline appears.

Common Errors

Symptom or messageCauseFix
Table of contents is empty or shows old titlesThe contents come from the .toc file of the previous runCompile again (three times with hyperref if pages shift), or use latexmk
No file main.toc. in the logNormal on the first run: the file has not been written yetNothing; it goes away on the second run
A section is missingStarred 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 editsThe contents grew or shrank and moved the body, but only one run happened sinceRun 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 ignoredSet after \tableofcontents, which is too late for that tableMove \setcounter to the preamble, or use \addtocontents as shown above
Renamed or deleted headings still listedA stale .toc file from an earlier run, often after a failed compileDelete the auxiliary files (latexmk -c, or .aux, .toc, .out) and compile again
\renewcommand{\contentsname} has no effectbabel 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 and online editors such as Octree repeat the run automatically.

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?

Sections, subsections and lower levels already have dotted leaders in the standard classes; only the top level (parts, chapters, and sections in article) has none. Load tocloft and redefine the leader, for example \renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}} for sections in article, or \cftchapleader for chapters in report and book.

Next Steps

The guides that usually come next when you structure a long document:

Free LaTeX Tools

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 →