2

This question is essentially the same as How can I fit my table of contents into a single page?

"When I use \tableofcontents I get all my contents in a single page except for a line that goes on the other page, how can I make all in the same page?"

However, the solution given there does not help me, for the following reason. The page next to the ToC is text only (plus one line of ToC), the page after that starts with a section title. The last line of ToC seems to be forced here by LaTeX in order to avoid getting the section title at the bottom: it prefers to put a space between the ToC and the text rather than between paragraphs.

Is there an elegant solution to this?

Benoît Kloeckner
  • 524
  • 1
  • 4
  • 14
  • 4
    following the solution to that question, I managed to achieve what you're expecting by adding \addtocontents{toc}{\protect\enlargethispage{\baselineskip}} immediately after the first section heading – henrique Aug 28 '12 at 12:58

2 Answers2

3

Henrique's solution is hardwired to a position in the document and assumes that the toc will be only one page. Here is another solution.

\documentclass[12pt,a6paper]{article}
\usepackage{blindtext}
\makeatletter
\def\enlargetocpage{%
  \begingroup
  \@ifstar{\def\x{*}\@enlargetocpage}{\def\x{}\@enlargetocpage}%
}
\def\@enlargetocpage#1{%
  \toks@{#1}%
  \protected@edef\x{%
    \endgroup\noexpand\AtEndDocument{\noexpand\addtocontents{toc}%
    {\protect\enlargethispage\x{\the\toks@}}}%
  }\x
}
\makeatother

\enlargetocpage*{\baselineskip}

\begin{document}
\tableofcontents
\blindtext

\section{A section}

\subsection{And a subsection}

\blindtext

\blinddocument

\blinddocument

\blinddocument

\end{document}
Ahmed Musa
  • 11,742
  • Unfortunately, in my document this has no effect on my problem.Maybe my analysis is wrong, or there are some issue with the cls file.

    Note that in your example, a \enlargethispage, while ad hoc, makes the deal. It does nothing in my document.

    – Benoît Kloeckner Sep 14 '12 at 12:08
  • It works for the samples I have generated. Without having your document, I wouldn't know why it failed in your case. – Ahmed Musa Sep 15 '12 at 00:38
1

Following @lockstep's first answer to How can I fit my table of contents into a single page?, I managed to achieve what you're expecting by adding \addtocontents{toc}{\protect\enlargethispage{\baselineskip}} immediately after the first section heading. Here's a MWE:

\documentclass[12pt,a6paper]{article}
\usepackage{blindtext}
\begin{document}
\tableofcontents

\blindtext

\section{A section}
\addtocontents{toc}{\protect\enlargethispage{\baselineskip}}

\subsection{And a subsection}

\blindtext

\blinddocument

\blinddocument

\blinddocument
\end{document}
henrique
  • 6,616