检测前一页(已发货)是否为空?

检测前一页(已发货)是否为空?

我使用 lualatex。TeXlive2023,Linux。

问题:是否可以检测上一页(刚发货)是否为空?我不想添加或删除空白页。我不想移动到正面或反面。所有这些情况都有众所周知的解决方案。我寻求的只是信息,因为第 N 页有一个自定义宏,它取决于第 N-1 页是否为空。它不需要知道“为什么”该页面为空。MWE:

\documentclass{article}
\usepackage(fontspec} % I compile with lualatex. Remove line if pdflatex.
\pagestyle{empty}
% Begin pseudo-code:
\IfPrevPageEmpty{\typeout{FOO}}{\typeout{BAR}}
% End pseudo-code.
\begin{document}
Hello
\clearpage
\IfPrevPageEmpty% Expect BAR.
\null
\clearpage
\IfPrevPageEmpty% Expect FOO.
World
\end{document}

上述 MWE 不使用页眉或页脚,因此正文中没有任何内容的页面将真正为空。如果检测仅查看主文本块,效果会更好,这样具有非空页眉或页脚的上一页将为“空”,且文本块为空。

我为什么问:最多 N-1 页可能由一个人编辑,而从 N 开始的页面可能由另一个人编辑。合并的文档可能由第三个人编辑。如果 N-1 和 N 没有按预期交互,我想发出一个标记。这不是简单的正面/反面问题。如果第三个人恰好是我,我会注意到;对其他人来说不一定如此。

答案1

如果您使用nextpage命令清除到下一个奇数/偶数页而不是手动插入空白页,则可以使用可选参数来切换您可以测试的布尔值。

以下提供了\rallgclearpage*[]。无星号版本清除到奇数页;带星号版本清除到偶数页。可选参数可以包含要包含到任何跳过的页面上的内容。\IfPrevPageEmptyTF{}{}提供条件,以及\IfPrevPageEmptyT{}\IfPrevPageEmptyF{}并且所有这三个都应该以通常的方式工作。

\documentclass{article}
\usepackage{nextpage}
\ExplSyntaxOn
\bool_new:N \g_rallg_empty_bool
\NewDocumentCommand \rallgclearpage { s O {} }
{
  \bool_gset_false:N \g_rallg_empty_bool
  \IfBooleanTF { #1 }
  {
    \cleartoevenpage[\bool_gset_true:N \g_rallg_empty_bool]
    #1
  }{
    \cleartooddpage[\bool_gset_true:N \g_rallg_empty_bool]
    #1
  }
  \AddToHookNext {shipout/after} { \bool_gset_false:N \g_rallg_empty_bool }
}
\prg_new_protected_conditional:Nnn \rallg_if_prevpage_empty: { T, F, TF }
{
  \bool_if:NTF \g_rallg_empty_bool 
  {
    \prg_return_true:
  }{
    \prg_return_false:
  }
}
\cs_new_eq:NN \IfPrevPageEmptyT \rallg_if_prevpage_empty:T
\cs_new_eq:NN \IfPrevPageEmptyF \rallg_if_prevpage_empty:F
\cs_new_eq:NN \IfPrevPageEmptyTF \rallg_if_prevpage_empty:TF
\ExplSyntaxOff
\begin{document}
Hello
\rallgclearpage
\IfPrevPageEmptyTF{empty}{non}% Expect empty.
\rallgclearpage*
World
\IfPrevPageEmptyTF{empty}{non}% Expect non.
\end{document}

相关内容