3

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:

With two items on the final TOC page, the heading displays correctly

But now just comment out the last \chapter{Something} in the code above; the result will be this:

enter image description here

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?

  • Off-topic: The document class option 24pt has no effect and may (should?) be omitted. On the other hand, one should probably provide the document class option letterpaper explicitly. This is important for readers whose default paper size is a4paper. – Mico Feb 25 '18 at 5:47
  • Thanks for these suggestions; I've now implemented them. – Rettre Feb 25 '18 at 5:52
  • Have you checked with the guideline people that you really need to input that on every page? Usually, those are meta content in an example file to say: title left, page number on the right. – Johannes_B Feb 25 '18 at 12:33
  • @johannes-b First of all, the example of a TOC given in the relevant guidelines definitely has two pages, and on the second page there is the "Chapter" - "Page" heading. Second, even if it turns out the editors are forgiving about this, at this point I am also simply curious to find out how to do it! – Rettre Feb 25 '18 at 12:40
2

Adding \leavevmode to \AtBeginShipoutClear, forcing us back in to horizontal mode, fixes the problem:

Sample output

\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}{\leavevmode\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}

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.