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?

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@
\setlength\@tempdima{1.5em}%
\begingroup
\samepage %new
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode \bfseries

• Hmm, I remember trying that but probably I forgot to remove the aux and toc files. Thanks. – gerrit Jul 8 '13 at 15:04