如何检查乳胶是否在当前页面开始新章节?

How can I check in latex whether a new chapter starts on current page?

我正在使用 everypage 包。使用 \AddEverypageHook 命令,我可以在文档每一页的开头重复操作。现在我想做这样的事情:

\AddEverypageHook{
  \if "New chapter starts at current page." - "Do stuff." 
  \else "Do other stuff."
  \fi
}

如何在latex中查看新章节是否从当前页开始?

我有办法。这个想法是检查章节编号计数器是否已更改。这是由自定义计数器完成的:

\newcounter{CurrentChapNum}
  • 如果章节计数器没有改变,我们仍在当前章节中,\customcommand1 内容也是如此。
  • 如果计数器发生变化,我们似乎正处于新章节的开始,所以执行 \customcommand2 并将 CurrentChapNum-counter 重置为当前章节的值。

这是通过这段代码完成的。

\AddEverypageHook{ 
   \ifnum\value{chapter}=\value{CurrentChapNum} \customcommand1 
   \else \customcommand2 \setcounter{CurrentChapNum}{\value{chapter}} 
   \fi  
}

因为我对 latex-markup-stuff 很陌生,我希望这不会太笨拙。

在典型的文档中,发出 \chapter 命令后会自动分页。例如,查看 \chapterreport.cls 中的作用:

\newcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
                    \thispagestyle{plain}%
                    \global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@chapter\@schapter}

它发出一个 \clearpage(或 \cleardoublepage),刷新任何未决的并在新页面上开始。

因此,根据您的设置,使用 afterpage package\afterpage{<stuff>} 宏在 之后执行 <stuff> 可能就足够了当前页面。例如,在您的序言中,您会

\let\oldchapter\chapter % Store \chapter
\renewcommand{\chapter}{% Redefine \chapter to...
  \afterpage{\customcommand}% ...execute \customcommand after this page
  \oldchapter}

当然,这只有在您不在非章节页面上执行任何其他操作时才有意义,因为条件与 \chapter 相关联。因此,在 每个 页面做出文档范围的决定可能需要稍微不同的方法。

我仍然建议使用 \chapter 宏,但使用条件。这是一个例子(点击放大图片):

\documentclass{report}
\usepackage{lipsum,afterpage,everypage}

\newcounter{chapterpage}% For this example, a chapterpage counter
\newcounter{regularpage}% For this example, a regularpage counter
\newif\ifchapterpage% Conditional used for a \chapter page
% Just for this example, print page number using:
\renewcommand{\thepage}{\LARGE\thechapterpage--\theregularpage}

\AddEverypageHook{
  \ifchapterpage % If on a \chapter page...
    \stepcounter{chapterpage}% Increase chapterpage counter
    \global\chapterpagefalse% Remove conditional
  \else % ...otherwise
    \stepcounter{regularpage}% Increase regularpage counter
  \fi
}
\let\oldchapter\chapter % Store \chapter
\renewcommand{\chapter}{% Redefine \chapter to...
  \afterpage{\global\chapterpagetrue}% ... set \ifchapterpage to TRUE _after_ this page
  \oldchapter}

\begin{document}

\chapter{First chapter}\lipsum[1-50]
\chapter{Second chapter}\lipsum[1-50]
\chapter*{Third chapter}\lipsum[1-50]
\chapter{Final chapter}\lipsum[1-50]

\end{document}

上述方法的优点是对\chapter\chapter*都有效。 \chapter* 不会增加 chapter 计数器,因此依赖基于这种比较的条件是不够的。