Typevia
Document Structure

Introduction to the Table of Contents

A Table of Contents (ToC) is an essential element of any long document, providing a roadmap for your readers and helping them navigate your content efficiently. In many word processors, creating a ToC is a manual or semi-manual process that can break easily when you move text around.

In LaTeX, the Table of Contents is handled automatically. LaTeX tracks your document’s structure (sections, subsections, and chapters) and generates a perfectly formatted ToC based on your actual content. If you move a section or change a heading, LaTeX updates the ToC for you.


The Basic Syntax

To generate a Table of Contents, you only need one simple command. Usually, this command is placed right after your \maketitle command or your abstract.

Example 1: A Basic Document with a ToC

\documentclass{article}

\begin{document}

\title{My First Structured Document}
\author{Jane Doe}
\date{\today}
\maketitle

\tableofcontents
\newpage

\section{Introduction}
This is where the introduction goes.

\section{Main Research}
\subsection{Methodology}
Detailing how the research was conducted.
\subsection{Results}
What we found during the study.

\section{Conclusion}
Final thoughts.

\end{document}

Note: When you run this code for the first time, you might notice that the Table of Contents area is blank or incomplete. Don't panic! This is a normal part of how LaTeX works.

The "Two-Run" Rule

LaTeX generates the ToC by reading the document and writing section titles and page numbers into a secondary file (with the extension .toc).

  1. First Run: LaTeX records where the sections are.
  2. Second Run: LaTeX reads that recorded information and prints the ToC.

If you ever see a "???" in your ToC or notice page numbers are wrong, simply compile your document one more time.


Controlling the Depth of the ToC

By default, LaTeX includes headings down to a certain level. In the article class, it typically shows Sections, Subsections, and Subsubsections. However, you might want to simplify your ToC to show only major sections.

You can control this using the tocdepth counter in your preamble (the area before \begin{document}).

Level Numbering Reference:

  • -1: Part
  • 0: Chapter (only in report or book classes)
  • 1: Section
  • 2: Subsection
  • 3: Subsubsection

Example 2: Limiting ToC Depth

If you want your ToC to only show major Sections (Level 1) and ignore subsections, add this to your preamble:

\documentclass{article}

% Set ToC depth to 1 (Sections only)
\setcounter{tocdepth}{1}

\begin{document}
\tableofcontents

\section{Visible Section}
\subsection{Hidden Subsection} % This will not appear in the ToC
\end{document}

Adding Unnumbered Sections Manually

By default, LaTeX only adds numbered sections to the ToC. If you use a "starred" section command (like \section*{Introduction}), it will appear in the text but not in the Table of Contents.

If you want a starred section to appear in the ToC, you must manually tell LaTeX to add it using the \addcontentsline command.

Example 3: Adding Starred Sections

\section*{Preface}
\addcontentsline{toc}{section}{Preface}

This section is unnumbered in the document text but will now appear in the Table of Contents.

Syntax breakdown:

  • toc: Tells LaTeX which list to add to (Table of Contents).
  • section: Tells LaTeX the formatting level to use.
  • Preface: The text that will actually appear in the ToC.

Handling Long Titles

Sometimes, a section title is very long and looks great in the body of your document but becomes cluttered or spans too many lines in the Table of Contents.

LaTeX allows you to provide an optional argument in square brackets [] to specify a shorter version of the title specifically for the ToC.

Example 4: Short Titles for ToC

\section[Brief Overview]{An In-depth and Extremely Detailed Overview of Global Economics in the 21st Century}

In this example, the long title appears in the document body, but "Brief Overview" appears in the Table of Contents.


Making the ToC Interactive

In the digital age, a Table of Contents should be clickable. By default, LaTeX ToCs are static text. To turn your ToC into a set of hyperlinks, you should use the hyperref package.

Example 5: Clickable ToC

\documentclass{article}
\usepackage[colorlinks=true, linkcolor=blue]{hyperref}

\begin{document}
\tableofcontents
\newpage

\section{The First Section}
Clicking "The First Section" in the ToC above will jump you directly here!
\end{document}

Best Practice: Always load the hyperref package last in your preamble, as it needs to override many internal LaTeX commands to create the links correctly.


Lists of Figures and Tables

If your document has many images or data tables, you might want to include a "List of Figures" or "List of Tables." These function exactly like the Table of Contents.

  • \listoffigures: Generates a list based on your \begin{figure} environments.
  • \listoftables: Generates a list based on your \begin{table} environments.

Place these commands wherever you want the lists to appear (usually right after the \tableofcontents).


Common Mistakes and Best Practices

1. Forgetting to Recompile

If you change a heading and it doesn't update in the ToC, compile again. Most modern editors (like Typevia) do this automatically, but if you are working locally, you may need to hit "Run" twice.

2. Putting the ToC at the End

While some books put the ToC at the back, most academic and professional documents expect it at the beginning. Ensure it follows your title page and abstract.

3. Overwhelming the Reader

If your ToC is three pages long, it’s no longer a "quick reference." Use \setcounter{tocdepth}{2} or 1 to keep it concise.

4. Spacing Issues

If your ToC looks cramped, you can add a \newpage or \clearpage after the \tableofcontents command to ensure your actual content starts on a fresh page.

5. Manual Entry Placement

When using \addcontentsline, make sure to place the command immediately after the section heading. If you place it at the end of the section, the page number in the ToC might point to the end of the section instead of the beginning.