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

And*_*ann 1 conditional latex tex

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

\AddEverypageHook{
  \if "New chapter starts at current page." - "Do stuff." 
  \else "Do other stuff."
  \fi
}
Run Code Online (Sandbox Code Playgroud)

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

Wer*_*ner 5

在典型的文档中,发出\chapter命令后会出现自动分页符。例如,看看\chapterin 做了什么report.cls

\newcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
                    \thispagestyle{plain}%
                    \global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@chapter\@schapter}
Run Code Online (Sandbox Code Playgroud)

它发出\clearpage(或\cleardoublepage),刷新任何挂起的内容并在新页面上开始。

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

\let\oldchapter\chapter % Store \chapter
\renewcommand{\chapter}{% Redefine \chapter to...
  \afterpage{\customcommand}% ...execute \customcommand after this page
  \oldchapter}
Run Code Online (Sandbox Code Playgroud)

当然,这只有在您不在非章节页面上执行任何其他操作时才有意义,因为条件与\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}
Run Code Online (Sandbox Code Playgroud)

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