9

I'm trying to add short section descriptions in the ToC, which has been covered elsewhere for the book documentclass or with the addition of extra packages. I don't want to have to install any new packages, and what I'm curious about here is why this is failing for the article documentclass.

Defining something like \newcommand{\summary}[1]{\addtocontents{toc}{#1} nearly works, but fails mysteriously with Something's wrong--perhaps a missing \item when used just before a new \section (see MWE below).

At the moment, I'm working around this by using the memoir documentclass instead (with some \cftsetindents commands and renewals of \cft[sub]sectionaftersnumb to make the ToC indentation more article-like), but I'd like to know why this isn't working for the article class.

\documentclass{article}

%% Neither of these work:
\newcommand{\summary}[1]{\addtocontents{toc}{#1}}
% \newcommand{\summary}[1]{\addcontentsline{toc}{sectioninfo}{#1}}

%% This one works, but doesn't look right (I don't want page numbers or dotted
%%  spacefilling for \summary entries.), and besides, using 
%%  'subsubsection' rather than 'sectioninfo' feels Wrong.
% \newcommand{\summary}[1]{\addcontentsline{toc}{subsubsection}{#1}}

\begin{document}
\tableofcontents

\section{First Sec}
\summary{This is fine.}
\subsection{A subsec}
\subsubsection{A subsubsec}
\subsection{Another subsec}
\summary{This causes problems}

\section{Second Sec}
\subsection{further subsecs}
\summary{This is fine.}
\subsection{further subsecs}
\summary{This is fine.}

\end{document}
  • 1
    Welcome to TeX.SX! How would you like the information to be printed? – egreg Oct 19 '13 at 21:37
  • Thanks! The look achieved in those other answers is nice, but I'm giving priority to simplicity of the LaTeX and minimization of dependencies, so I'll probably just use \emph{#1} in the command. At the moment I'm more concerned with knowing why this failure occurs with article documentclass, since I'll probably only be using these summaries for my own benefit during drafting, and not in the final product. – tsbertalan Oct 19 '13 at 22:06
9

The problem is that \contentsline{section}{...}{...}, issues \addpenalty which is illegal if you don't end the paragraph.

If you notice, your .toc file will be

\contentsline {section}{\numberline {1}First Sec}{1}
This is fine.
\contentsline {subsection}{\numberline {1.1}A subsec}{1}
\contentsline {subsubsection}{\numberline {1.1.1}A subsubsec}{1}
\contentsline {subsection}{\numberline {1.2}Another subsec}{1}
This causes problems
\contentsline {section}{\numberline {2}Second Sec}{1}
\contentsline {subsection}{\numberline {2.1}further subsecs}{1}
This is fine.
\contentsline {subsection}{\numberline {2.2}further subsecs}{1}
This is fine.

The “This causes problems” line is immediately followed by \contentsline{section} and so \addpenalty will appear in the middle of a paragraph. It does not happen when the text is followed by \contentsline{subsection} entries, because these don't issue \addpenalty.

Simple solution:

\documentclass{article}

\newcommand{\summary}[1]{\addtocontents{toc}{#1\par}}

\begin{document}
\tableofcontents

\section{First Sec}
\summary{This is fine.}
\subsection{A subsec}
\subsubsection{A subsubsec}
\subsection{Another subsec}
\summary{This causes problems}

\section{Second Sec}
\subsection{further subsecs}
\summary{This is fine.}
\subsection{further subsecs}
\summary{This is fine.}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.