25.1.3 \addtocontents

Synopsis:

\addtocontents{ext}{text}

Add text, which may be text or formatting commands, directly to the auxiliary file with extension ext. This is most commonly used for the table of contents so that is the discussion here, but it also applies to the list of figures and list of tables.

This will put some vertical space in the table of contents after the ‘Contents’ header.

\tableofcontents\newpage
\addtocontents{toc}{\protect\vspace*{3ex}}

This puts the word ‘Page’, in boldface, above the column of page numbers and after the header.

\tableofcontents
\addtocontents{toc}{~\hfill\textbf{Page}\par}
\chapter{...}

This adds a line announcing work by a new author.

\addtocontents{toc}{%
  \protect\vspace{2ex}
  \textbf{Chapters by N. Other Author}\par}

The difference between \addtocontents and \addcontentsline is that the latter is strictly for lines, such as with a line giving the page number for the start of a new subset of the chapters. As the above examples show, \addtocontents is for material such as spacing.

The \addtocontents command has two arguments. Both are required.

ext

Typically one of: toc for the table of contents, lof for the list of figures, or lot for the list of tables. The extension of the file holding the information.

text

The text, and possibly commands, to be written.

The sectioning commands such as \chapter use the \addcontentsline command to store information. This command creates lines in the .toc auxiliary file containing the \contentsline command (see \addcontentsline). In contrast, the command \addtocontents puts material directly in that file.

The \addtocontents command has an interaction with \include (see \include & \includeonly). If you use them at the same level, as with \addtocontents{...}{...}\include{...} then lines in the table of contents can come out in the wrong order. The solution is to move \addtocontents into the file being included.


Unofficial LaTeX2e reference manual