Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 38 ยท Cross-references and Bibliographies

Table of Contents, Index and Glossaries

Generate a table of contents, list of figures, subject index and glossary in LaTeX with \tableofcontents, makeindex and the glossaries package.

Long documents need navigation aids: a table of contents, lists of figures and tables, an index of key terms and a glossary of abbreviations. LaTeX builds all of them from commands you place in the source, so they stay in step with the text. This lesson shows each tool and the extra program runs they sometimes need.

Table of contents and lists

LATEX
\tableofcontents   % sections, subsections ...
\listoffigures     % every \caption inside a figure
\listoftables

Place these after \maketitle. They use the .toc, .lof and .lot files, which are written during one run and read in the next, exactly like labels. That is why a new section shows up only after a second compilation.

Controlling depth and entries

LATEX
\setcounter{secnumdepth}{2}  % numbers up to subsections
\setcounter{tocdepth}{1}     % TOC lists sections only

\section*{Acknowledgements}                       % unnumbered, not in TOC
\addcontentsline{toc}{section}{Acknowledgements}  % add it manually

\section[Short title]{A very long section title that would not fit}
\caption[Short caption for the list]{Full caption with a long explanation}

For a book or report, use \frontmatter, \mainmatter and \appendix to switch numbering styles. With the tocloft package you can change the dot leaders, indentation and fonts of the lists; titletoc is the lower-level alternative.

The stand-in below turns a list of headings into a table of contents with dot leaders, which is the core job of \tableofcontents.

JS Build a table of contents

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Making an index

An index lists terms with the pages where they occur. Load the makeidx package (or the more flexible imakeidx):

LATEX
\usepackage{makeidx}
\makeindex
...
A \emph{matrix}\index{matrix} has rows and columns.
An \index{matrix!inverse|textbf}inverse exists only for square matrices.
See also \index{determinant|see{matrix}}.
...
\printindex

Entry syntax:

  • \index{term}: a plain entry.
  • \index{matrix!inverse}: a sub-entry (use ! for each level).
  • \index{term|textbf}: format the page number (for example bold for the main reference).
  • \index{term|see{other}}: a cross-reference entry.
  • \index{sortkey@\emph{term}}: sort by one string, print another.

The build sequence adds a step: pdflatex main, then makeindex main, then pdflatex main again. With imakeidx and \makeindex, the --shell-escape option lets LaTeX run makeindex automatically; latexmk also knows how.

Put \index after the word, with no space, so the page number points to the right line. Do not put it inside a \section{} argument.

Glossaries and acronyms

The glossaries package defines terms once, then lets you use them consistently, with the expansion appearing on first use.

LATEX
\usepackage[acronym,toc]{glossaries}
\makeglossaries

\newacronym{cpu}{CPU}{central processing unit}
\newglossaryentry{kernel}{
  name=kernel,
  description={the core part of an operating system}
}

The \gls{cpu} runs the \gls{kernel}.   % first use: central processing unit (CPU)
Later the \gls{cpu} appears as CPU.    % subsequent uses: CPU
\Gls{kernel}                           % capitalised
\glspl{cpu}                            % plural CPUs

\printglossary[type=\acronymtype]
\printglossary

Compile with pdflatex main, then makeglossaries main, then pdflatex main. The glossaries-extra package improves many defaults and works with the newer bib2gls tool if you manage large term lists.

Advantages: consistent expansion on first use, automatic links from each use to its definition, and a sorted list with page numbers.

Other navigation helpers

  • \appendix changes section numbering to letters.
  • \thispagestyle{empty} removes headers on a title page.
  • nomencl builds a list of symbols for theses with many mathematical variables, with \nomenclature{$\alpha$}{learning rate} and makeindex -s nomencl.ist.
  • minitoc adds a mini contents at the start of each chapter.
  • hyperref turns all entries in the lists into clickable links and creates PDF bookmarks. Use \texorpdfstring{$\alpha$}{alpha} in headings to keep bookmarks clean.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does a new section not appear in the table of contents after the first compile?

    LaTeX writes headings to the .toc file and reads it during the next compilation, so a second run is needed.
  2. 2. Which command creates a sub-entry for the term inverse under matrix?

    An exclamation mark separates the levels of an index entry.
  3. 3. What does \gls{cpu} print the first time with the glossaries package?

    The first use shows the long form followed by the short form; later uses show only the short form.
  4. 4. How do you keep an unnumbered chapter in the table of contents?

    Starred headings are not listed, so you add the entry yourself with \addcontentsline.

Summary

  • \tableofcontents, \listoffigures and \listoftables read files written in the previous run.
  • Use optional short titles and \addcontentsline to control entries; tocdepth and secnumdepth set depth.
  • \index plus makeindex and \printindex build an index; !, |see{} and @ give sub-entries and sort keys.
  • The glossaries package expands acronyms on first use and needs makeglossaries.
  • Each helper adds a program run, and latexmk automates them.

Next: Using Packages