Sorry in advance for this being such a specific question, but I'm at my wits end with this problem.
My university has odd formatting requirements for dissertations (I know, don't they all). In particular, in the table of contents they want the spacing between chapter/chapter, chapter/section, and section/chapter to be one half spacing, but they want all other spacing to be single spaced. This isn't so hard to sort out (there are some excellent stackexchange solutions). However, they also want the word "Page" to appear above the page number column on ever page of the ToC and they want it to be separated by one half spacing, this is the problematic part.
In an attempt to solve this problem I used utilized some stackexchange solutions based on tocloft
, etoolbox
, and setspace
as well as some \preto
commands for formating the ToC and then used the atbegshi
package to put "Page" at the top of the page column on each page. The problem is that, due to the way spacing is handled, if a section is at the top of a page, then "Page" is only separated by a single space.
Here is a minimal working example illustrating the problem:
\documentclass{report}
\usepackage[onehalfspacing]{setspace}
\usepackage{etoolbox}
\usepackage{tocloft}
\RequirePackage{atbegshi}
\preto\section{
% if this is the first section in a chapter, then add extra space to the ToC
% to make it appear 1.5 spacing.
\ifnum\value{section}=0
\addtocontents{toc}{\vskip6pt}
\fi
}
\preto\chapter{
% add space before chapter lines in the ToC to make it appear 1.5 spacing.
\addtocontents{toc}{\vskip6pt}
}
\newcommand{\newtoc}{
\begin{singlespace}
% zero out the space tocloft puts before chapters
\setlength{\cftbeforechapskip}{0pt}
% add leader dots for chapters
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}}
% make chapter and corresponding page appear in normal font
\renewcommand{\cftchapfont}{\normalfont}
\renewcommand{\cftchappagefont}{\normalfont}
% prepend ``CHAPTER'' to the chapter name in the ToC
\renewcommand{\cftchappresnum}{CHAPTER\space}
\settowidth{\cftchapnumwidth}{\cftchappresnum\qquad}
% add some space between ``Page'' and the ToC line in the ToC (make it appear
% onehalfspacing). Then add the ToC line to the ToC
\addtocontents{toc}{\vskip 6pt}
\addcontentsline{toc}{chapter}{TABLE OF CONTENTS}
% generate a name for the ToC
\renewcommand{\cfttoctitlefont}{\normalfont}
\renewcommand\contentsname{\normalfont} {\centerline{TABLE OF CONTENTS}}
\setlength{\cftaftertoctitleskip}{0pt}
% put ``Page'' above the page column on the first page of the ToC
\renewcommand{\cftaftertoctitle}{\hfill{\normalfont { Page}}}
% put ``Page'' above the page column on all subsequent pages of the ToC
% the build the ToC
\AtBeginShipout{\cftaftertoctitle}
\tableofcontents
\AtBeginShipout{}
\end{singlespace}
}
\begin{document}
\newtoc
\chapter{A Chapter}
\chapter{A Chapter}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\end{document}
Evidently I need 10 reputation to post images of the output, but the problem can be seen by examining the top of page 2 as compared to either page 1 or page 3 of the output.
I attempted to use the afterpage
package to set a bool in the ToC. The idea being the subsequent \preto commands could use the bool to determine if they need to insert space at the top of the page. This very nearly worked, except that the inserted space always appears two lines down from where it should. Here is the modified code:
\documentclass{report}
\usepackage[onehalfspacing]{setspace}
\usepackage{etoolbox}
\usepackage{tocloft}
\usepackage{atbegshi}
\usepackage{afterpage}
% generate a bool for tracking when a newpage occures in the ToC
\providebool{NewPage}
\global\boolfalse{NewPage}
\preto\section{
\addtocontents{toc}{
\protect\ifbool{NewPage}{
% If NewPage is true, then set it to false and insert spacing
\protect\global\boolfalse{NewPage}\vskip6pt
}{
% turn on the NewPage bool after the end of a page
\protect\afterpage{\global\booltrue{NewPage}}
}
}
% if this is the first section in a chapter, then add extra space to the ToC
% to make it appear 1.5 spacing.
\ifnum\value{section}=0
\addtocontents{toc}{\vskip6pt}
\fi
}
\preto\chapter{
% add space before chapter lines in the ToC to make it appear 1.5 spacing.
\addtocontents{toc}{
\protect\ifbool{NewPage}{
% unflag NewPage
\protect\global\boolfalse{NewPage}
}{
% turn on the NewPage bool after the end of a page
\protect\afterpage{\global\booltrue{NewPage}}
}
}
\addtocontents{toc}{\vskip6pt}
}
\newcommand{\newtoc}{
\begin{singlespace}
% zero out the space tocloft puts before chapters
\setlength{\cftbeforechapskip}{0pt}
% add leader dots for chapters
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}}
% make chapter and corresponding page appear in normal font
\renewcommand{\cftchapfont}{\normalfont}
\renewcommand{\cftchappagefont}{\normalfont}
% prepend ``CHAPTER'' to the chapter name in the ToC
\renewcommand{\cftchappresnum}{CHAPTER\space}
\settowidth{\cftchapnumwidth}{\cftchappresnum\qquad}
% add some space between ``Page'' and the ToC line in the ToC (make it appear
% onehalfspacing). Then add the ToC line to the ToC
\addtocontents{toc}{\vskip 6pt}
\addcontentsline{toc}{chapter}{TABLE OF CONTENTS}
% generate a name for the ToC
\renewcommand{\cfttoctitlefont}{\normalfont}
\renewcommand\contentsname{\normalfont} {\centerline{TABLE OF CONTENTS}}
\setlength{\cftaftertoctitleskip}{0pt}
% put ``Page'' above the page column on the first page of the ToC
\renewcommand{\cftaftertoctitle}{\hfill{\normalfont { Page}}}
% put ``Page'' above the page column on all subsequent pages of the ToC
% the build the ToC
\AtBeginShipout{\cftaftertoctitle}
\tableofcontents
\AtBeginShipout{}
\end{singlespace}
}
\begin{document}
\newtoc
\chapter{A Chapter}
\chapter{A Chapter}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\section{A Section}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\chapter{A Chapter}
\end{document}
The new spacing issue arises on page 2.
I think the problem here is that afterpage
is buffering some of the output (in this case the first two lines on the next page), and then executing the afterpage commands. The result is that the conditional expressions for the buffered sections are expanded and evaluated before afterpage sets the bool. I took a look at afterpage.sty
hoping to expand the package to allow one to manually dump this buffer, but I was unable to make any progress there.
Any help would be greatly appreciated.