Your support for our advertisers helps cover the cost of hosting, research, and maintenance of this document

Formatting Information — An introduction to typesetting with LATEX

Chapter 2: Basic structures

Section 2.8: Table of contents

All auto-numbered headings get entered in the Table of Contents (ToC) automatically. You don’t have to print a ToC, but if you want to, just add the command \tableofcontents at the point where you want it printed (usually after the Abstract or Summary).

Entries for the ToC are recorded each time you typeset your document, and reproduced the next time you typeset it, so you need to re-run LATEX one extra time to ensure that all ToC page-number references are correctly resolved.

The commands \listoffigures and \listoftables work in exactly the same way as \tableofcontents to automatically list all your tables and figures. If you use them, they normally go after the \tableofcontents command.

We’ve already seen in § 2.6 how to use the optional argument to the sectioning commands to add text to the ToC which is slightly different from the one printed in the body of the document. It is also possible to add extra lines to the ToC, to force extra or unnumbered section headings to be included.

Exercise 7: Using a Table of Contents

  1. Add the \tableofcontents command to your document, after the \maketitle command but before the Abstract.

  2. Typeset the document.

    If you are using your editor’s Build to typeset the document, it should re-run LATEX if necessary, to make sure the Table of Contents is updated to reflect any changes you have made (for example in section numbering, or adding, deleting, or moving sections or chapters around.

  3. If the Table of Contents does not reflect your document structure, you need to typeset it one more time, to bring it up to date.

The \tableofcontents command normally shows only numbered section headings, and only down to the level defined by the tocdepth counter (see § 2.6.1), but you can add extra entries with the \addcontentsline command. For example if you use an unnumbered section heading command to start a preliminary piece of text like a Foreword or Preface, you can write:

\subsection*{Preface}
\addcontentsline{toc}{subsection}{Preface}
      

This will format an unnumbered ToC entry for ‘Preface’ in the ‘subsection’ style. You can use the same mechanism to add lines to the List of Figures or List of Tables by substituting lof or lot for toc.

There is also a command \addtocontents which lets you add any LATEX commands to the ToC file. For example, to add a horizontal rule and a 6pt gap, you could say

\addtocontents{toc}{\par\hrule\vspace{6pt}}
      

at the place where you want it to occur. You should probably only use this command once you know what you are doing.

There are several packages to help you restyle these lists of contents; perhaps the best-known is tocloft.