2

Consider the following example using the amsart document class:

\documentclass{amsart}% http://ctan.org/pkg/amsart
\newcommand{\addtotoc}[1]{\addtocontents{toc}{#1}}

\begin{document}

\tableofcontents
\section{First section}
\addtotoc{abc}
\section{Second section}
\subsection{First subsection}
\addtotoc{def}
\subsection{Second subsection}
\end{document}

The document class amsart seems to exhibit a delay in processing content to the table of contents; here is the .toc file's content:

\contentsline {section}{\tocsection {}{1}{First section}}{1}
abc
\contentsline {section}{\tocsection {}{2}{Second section}}{1}
def
\contentsline {subsection}{\tocsubsection {}{2.1}{First subsection}}{1}
\contentsline {subsection}{\tocsubsection {}{2.2}{Second subsection}}{1}

ToC

Why does the entry abc end up after the first section (as expect), but def does not end up after the first subsection?

1 Answer 1

1

The very same problem arises already with article class, and paragraph:

\documentclass{article}
\newcommand{\addtotoc}[1]{\addtocontents{toc}{#1}}
\begin{document}
\setcounter{tocdepth}{5}
\tableofcontents

\section{First section}
\addtotoc{abc\endgraf}
\section{Second section}
\subsection{First subsection}
\addtotoc{def\endgraf}
\subsection{Second subsection}
\paragraph{A paragraph}% insert \leavevmode to cure the problem
\addtotoc{ghk\endgraf}
\end{document}

Be it with amsart+subsection or article+paragraph the cure is to add \leavevmode before the \addtotoc.

What happens is that the \addcontentsline originating in the subsection will be executed only as part of things put in an \everypar, hence at the start of the next paragraph. Doing \par does not help because we are in vertical mode: immediately after \subsection{First subsection} the subsection title has not been typeset yet.

Thus \addtotoc is encountered first, while still in vertical mode, and thus stuff ends up in the .aux file before the corresponding instruction originating the subsection title itself. This order is the naturally preserved in the .toc file.

image of toc done with article class exhibiting the problem

(one might wonder how the last \addcontentsline ever gets executed, but at the end of the document a \clearpage is done, and \if@noskipsec is \iftrue this does the \leavevmode thing)

1
  • So run-in headings is the key here. Thanks.
    – Werner
    Apr 30, 2014 at 14:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .