Similar questions have been asked—and answered—a number of times (e.g. here, here, and here), but all the answers seem to have the following bug: if the final page of the table of contents (TOC) has just a single item, then the required heading does not appear.
For example, the solution given here uses the atbegshi
package. The minimal working example (MWE) that follows implements this solution, and it will indeed do what it is supposed to, because there will turn out to be more than one item on the final page of the TOC:
\documentclass[letterpaper]{report}
\usepackage{tocloft}
\usepackage{atbegshi}% http://ctan.org/pkg/atbegshi
\makeatletter
\newcommand{\tocheader}{{%
\bfseries% Font selection
\hspace*{\cftchapindent}% Chapter indent
\makebox[\cftchapnumwidth][l]{Chapter no}% Chapter number heading
\hfill%
\contentsname% Title heading
\hfill%
\makebox[\@pnumwidth][r]{Page}% Page heading
\par\kern.5\baselineskip% After heading
}}
\AtBeginDocument{\renewcommand{\tableofcontents}{%
\begingroup%
\parindent\z@\parskip\cftparskip%
\tocheader%
\@starttoc{toc}%
\endgroup%
}}
\renewcommand{\cftaftertoctitle}{\par\kern.5\baselineskip\tocheader}
\newcommand{\AtBeginShipoutClear}{\gdef\AtBegShi@Hook{}}
\makeatother
\begin{document}
\AtBeginShipout{\tocheader}
\tableofcontents
\AtBeginShipoutClear
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\end{document}
Here is the output right at the page break, featuring the desired heading "Chapter no Contents Page" at the beginning of the second page of TOC:
But now just comment out the last \chapter{Something}
in the code above; the result will be this:
The heading (i.e. the "Chapter no Contents Page" line) has disappeared!
The same problem occurs with the solution that uses the afterpage
package (like here); and it also happens if one hacks the \output
routine directly, like in this latex class.
Any ideas how to fix this?
24pt
has no effect and may (should?) be omitted. On the other hand, one should probably provide the document class optionletterpaper
explicitly. This is important for readers whose default paper size isa4paper
.