3

I have a long ToC provided by the tocloft package. It is long enough that it runs on to a second page. What I have then is that (for example) section 2 is at the end of the first page and section 2.1 is at the start of the second page.

Ideally I'd like to group sections and subsections in the ToC such that if they get split by a page like that, then it'll just get pushed to a new page. The end result would be that the first page would end with section 1.X and the second page would start with section 2 (and so on...).

Is there a way to configure this?


MWE that illustrates the issue.

\documentclass{article}
\usepackage{tocloft}

\begin{document}

\tableofcontents
\clearpage

\section{Section 1}
\subsection{A subsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsection{A subsection}
\subsection{A subsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsection{A subsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}

\section{A Second Section}
\subsection{A subsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}
\subsubsection{A subsubsection}

\section{A Third Section}
\subsection{A subsection}
\subsubsection{A subsubsection}

\end{document}

MWE Illustration

  • 3
    Something like \enlargethispage{-1cm} just before the \tableofcontents? – marmot Feb 22 at 21:13
  • One approach is to automatically issue a \needspace with each \l@section. See tex.stackexchange.com/questions/375439/… – John Kormylo Feb 23 at 3:34
  • @marmot Ultimately went with this solution as it was simple, yet effective. – zephyr Feb 25 at 14:48
2

Here's an excerpt from page 6 of the user guide of the tocloft package:

You may get undesired page breaks in the ToC. For example you may have a long multiline section title and in the ToC there is a page break between the lines. After your document is stable you can use \addtocontents at appropriate places in the body of the document to adjust the page breaking in the ToC. As examples:

  • \addtocontents{toc}{\protect\newpage} to force a page break.

  • \addtocontents{toc}{\protect\enlargethispage{2\baselineskip}} to make the page longer.

  • \addtocontents{toc}{\protect\needspace{2\baselineskip}} to specify that if there is not a vertical space of two baselines left on the page then start a new page. (The \needspace macro is defined in the needspace package.)

Note, incidentally, that these piece of advice are not dependent on the tocloft package being loaded.

For your document, then, the optimal action would be insert the instruction

\addtocontents{toc}{\protect\newpage}

immediately before

\section{A Second Section}

in the body of the document.

Finally, do take note of the qualifier "after your document is stable". For sure, don't engage in these fine-tuning operations unless and until your document is otherwise pretty much finished.

  • 1
    Much better solution than mine! Nice feature to know as well. Thanks! – zephyr Feb 25 at 15:30
  • 1
    +1: A case of RTFM :). – Dr. Manuel Kuehner Feb 25 at 15:32
1

Another l̶e̶s̶s̶ ̶h̶a̶c̶k̶y version that does not require your document setup to be finalized would be to add a \nopagebreak after section-titles. With tocloft this could be done via

\renewcommand{\cftsecafterpnum}{\nopagebreak}

(if you want to add it manually to a single section you'll need to add \protect\nopagebreak)

In your example this works as intended:

toc with break at the right place

  • Note that this prevents breaks after section entries, but allows them after subsections. If your toc consists mostly of sections with no subsections, this could lead to some page breaks that are too early. – sheß Feb 25 at 16:00
  • 1
    I disagree with your view that \renewcommand{\cftsecafterpnum}{\nopagebreak} is "less hacky" than \addtocontents{toc}{\protect\newpage}. Moreover, I actually fail to see what's supposed to be "hacky" about the latter command. Central to fine typography is the balancing of various competing, and often conflicting, visual and layout goals. Sometimes it may well "make sense" to assure that a page break gets inserted in some locations of the ToC. At other times, though, it may be even more important not to allow those same page breaks. The derogatory term "hacky" is out of place here. – Mico Feb 25 at 17:23
  • Sorry, didn't mean it like that. You're right of course that depending on the context my suggestion is actually less useful. What I like about it though is that, provided that your toc is not predominantly made up of sections, this version will not require any additional adjustments if you decide to change something. e.g. your margins, or add a section, or whatever. I.e. your layout does not have to be finalized. While the \enlargethispage works only if the problem appears on the first page and the \addtocontents{toc}{\protect\newpage} will need to be moved/dropped if the document changes. – sheß Feb 26 at 12:58
  • Your solution could also be quite unsatisfactory if it turns out that the ToC would span exactly 2 pages with no redefinition of \cftsecafterpnum, and exactly 2 pages and 1 line if a redefinition of the type you're advocating is applied. In such a case, allowing a bad page break is less problematic than having a third ToC page with exactly 1 line. I think the admonition to hold off with tweaking the ToC until the document is essentially done is very wise indeed. – Mico Feb 26 at 13:38
  • 1
    Fine. Probably good advice. I've retract "my less hacky", but keep the answer because it answers the literal question ("Keep section and subsection together in table of content") and I guess will be an appropriate approach in many cases. – sheß Feb 26 at 13:46
0

With the help of marmot, I was able to push my title to the next page with a simple \enlargethispage{-0.5cm} command. It's not the most ideal solution because it is very manual and specific to the problem, but at least it does work.

From the MWE above, I exchanged

\tableofcontents

with

{%
    \enlargethispage{-0.5cm}
    \tableofcontents
}

that produced the output below.

Example of fixed ToC

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.