1

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.

1

I have devised a solution to the above problem in case anyone is interested. I managed to side-step the problem of having to conditionally add space to the ToC using the afterpage and fancyhdr packages. Here is a minimal working example of the solution:

\documentclass{report}

\usepackage[onehalfspacing]{setspace}
\usepackage{etoolbox}
\usepackage{tocloft}
\usepackage{afterpage}
\usepackage{fancyhdr}

\usepackage{fancyhdr}
\usepackage{afterpage}
\fancypagestyle{FancyToC}{
  \fancyhead{}
  \fancyfoot{}
  \setlength{\headsep}{\dimexpr 12pt}
  \renewcommand{\headrulewidth}{0pt}
  \setlength{\headheight}{\dimexpr25pt+24pt}
  \fancyhead[ER,OR]{Page}
  \fancyfoot[C]{\thepage}
}
\AtBeginDocument{
  \addtocontents{toc}{\protect\setlength{\textheight}{\dimexpr \textheight-36pt}}
}

\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

  \ifnum\value{section}=0
    % remove default skip (pt size) and then add on 1.5 (resp. 2.0) the pt size to get a one and a
    % half spacing (resp. double spacing)
    \addtocontents{toc}{\vskip6pt}
  \fi
  \addtocontents{toc}{\protect\afterpage{\protect\thispagestyle{FancyToC}}\relax}
}

\preto\chapter{
  % add space before chapter lines in the ToC to make it appear 1.5 spacing.
  \addtocontents{toc}{\vskip6pt}
  \addtocontents{toc}{\protect\afterpage{\protect\thispagestyle{FancyToC}}\relax}
}

\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}{12pt}

    % 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
    \tableofcontents
    \pagestyle{plain}
  \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}

Compare the second page of the first MMWE from the question:

Inconsistent spacing

with the second page of the output from this MMWE:

Consistent spacing

I would still be very interested in understanding what went wrong with the first afterpage solution I posted, e.g., why the afterpage space occurred two lines after it was supposed to.

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.