2

For a sandwich thesis, I'm typesetting a List of Papers using the tocloft package. Each entry contains the full citation (using biblatex's \fullcite), so it can be rather long. I've currently typeset it as follows (where \makeanypaperBiblio is an argument to a custom function):

   \addcontentsline{pap}{papers}{%
    {\bfseries Paper~\numberline{\Roman{paper}}}\\[.25\baselineskip]%
    \fullcite{\makeanypaperBiblio}\vspace{2ex}}%

I want to ensure that the entire contents-line will be on a single page in my List of Papers, but the usual way of preventing pagebreaks is not working. To illustrate the issue with a MWE:

\documentclass[a5paper]{article}
\begin{document}%
\tableofcontents
\section{Test}
\addcontentsline{toc}{section}{Very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very long}
\addcontentsline{toc}{section}{Very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very long}
\addcontentsline{toc}{section}{\begin{samepage}Very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very very very very very very very very very very
very very very very very very long\end{samepage}}
\end{document}

Compiling this example fails with:

! Argument of \@sect has an extra }.
<inserted text> 
                \par 
l.31 ...ry very very very very long\end{samepage}}

? 
Runaway argument?

Clearly, I cannot use the samepage environment inside addcontentsline, or at least not without trickery. How can I prevent a table of contents entry from spanning more than one page?

4

You need to protect the command:

\addcontentsline{toc}{section}{\protect\samepage Very very very very very very ...long}

If you want to insert it for all sections title you can redefine \l@section e.g.

\makeatletter
\renewcommand*\l@section[2]{%
  \ifnum \c@tocdepth >\z@
    \addpenalty\@secpenalty
    \addvspace{1.0em \@plus\p@}%
    \setlength\@tempdima{1.5em}%
    \begingroup
      \samepage %new
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
    \endgroup
  \fi}
\makeatother  
| improve this answer | |
  • Hmm, I remember trying that but probably I forgot to remove the aux and toc files. Thanks. – gerrit Jul 8 '13 at 15:04

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.