ToC 列标题中的条件间距

ToC 列标题中的条件间距

对于这个问题如此具体,我深感抱歉,但我对这个问题已经束手无策了。

我的大学对论文的格式要求很奇怪(我知道,不是吗?)。特别是,在目录中,他们希望章节/章节、章节/节和节/章节之间的间距为半倍行距,但他们希望所有其他间距为单倍行距。这并不难解决(有一些出色的 stackexchange 解决方案)。但是,他们还希望“Page”一词出现在目录的每一页的页码列上方,并且他们希望它以半倍行距分隔,这是有问题的部分。

为了解决这个问题,我利用了一些基于tocloft、和的 stackexchange 解决方案etoolbox以及setspace一些\preto用于格式化目录的命令,然后使用该atbegshi包将“页面”放在每页的页面列顶部。问题是,由于间距处理方式,如果某个部分位于页面顶部,则“页面”仅由一个空格分隔。

这是一个说明该问题的最小工作示例:

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

显然,我需要 10 点声誉才能发布输出的图像,但通过检查第 2 页的顶部与输出的第 1 页或第 3 页的比较就可以看到问题。

我尝试使用该afterpage包在目录中设置一个布尔值。这个想法是后续的 \preto 命令可以使用布尔值来确定是否需要在页面顶部插入空格。这几乎成功了,只是插入的空格总是出现在它应该出现的位置的两行以下。以下是修改后的代码:

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

新的间距问题出现在第 2 页。

我认为这里的问题是afterpage缓冲部分输出(在本例中是下一页的前两行),然后执行 afterpage 命令。结果是在 afterpage 设置布尔值之前,缓冲部分的条件表达式被扩展和评估。我看了看,afterpage.sty希望扩展包以允许手动转储此缓冲区,但我无法在那里取得任何进展。

任何帮助将不胜感激。

答案1

如果有人感兴趣的话,我已经设计了一个解决上述问题的方法。我设法避开了必须使用afterpagefancyhdr包有条件地向 ToC 添加空间的问题。以下是该解决方案的一个最小工作示例:

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

比较问题中第一个 MMWE 的第二页:

间距不一致

使用来自此 MMWE 的输出的第二页:

一致的间距

我仍然非常有兴趣了解afterpage我发布的第一个解决方案出了什么问题,例如,为什么 afterpage 空格出现在应该出现的两行之后。

相关内容