[Tex/LaTex] Reactivating “\clearpage” in LaTeX after turning it off with “\let\clearpage\relax”

page-breakingtable of contents

My document has a table of content in it. As i found out, after inserting one (\tableofcontents) LaTeX does a \clearpage by default.

A method to ignore \clearpage is the command \let\clearpage\relax, witch ignores it till the end of the document. Although i have no other command in my doc, my last page got erased. So i want to reactive \clearpage right after the toc was generated.

I am searching for something like:

\let\clearpage\relax
\tableofcontents
\*reactive it*

One try to do it with \begingroup failed.

\begingroup
\let\clearpage\relax
\tableofcontents
\endgroup

Any ideas?

Best Answer

At least with the document class book, quick testing suggests that \tableofcontents doesn't insert a \clearpage, but \chapter does. Thus, you have to put your group with \let\clearpage\relax around the first \chapter command (or whatever you have that's similar) after your \tableofcontents. The following example, for instance, typesets on one page:

\documentclass{book}

\begin{document}

\tableofcontents

\begingroup
\let\clearpage\relax
\chapter{Hello, world!}
\endgroup
And goodbye.

\end{document}

The above document rendering on one page (without the page number).


The old answer, which answers the asked question but doesn't solve the problem (and anyway, you should usually use \begingroup\endgroup instead).

You can save and restore \clearpage:

\let\oldclearpage\clearpage
\let\clearpage\relax
\tableofcontents
\let\clearpage\oldclearpage

\let just defines a command to be a duplicate of an existing one, so:

  1. \let\oldclearpage\clearpage makes \oldclearpage equivalent to \clearpage.
  2. \let\clearpage\relax makes \clearpage equivalent to \relax, which is a do-nothing command.
  3. \tableofcontents runs, with its \clearpages doing nothing.
  4. Finally, \let\clearpage\oldclearpage makes \clearpage equivalent to \oldclearpage, which is equivalent to the original \clearpage.