我有一个命令,可以在文件顶部创建一些预设文本,该文件需要有关我创建的环境位置的信息。我已经将所有代码组合在一起,它在文档末尾运行良好,但我需要将其放在文档的开头。
具体来说,我有一个命令
\newcommand\Pages{\expandafter\@printWatermarkedPage\@watermarkedPages,@}
\makeatother
\newcommand{\makeparagraph}{
Pages \Pages of this document has watermarks
}
其中,一堆逻辑填充了 watermarkedPages 列表(有关详细信息,请参阅此处遍历页面并设置变量),我需要将结果短语打印在顶部。如果我只是将当前实现移动到文档的头部,就会出现“缺少 \endcsname 插入。”错误,我认为这是因为列表尚未填充。
如何创建一个知道需要多次传递才能获得所需信息的命令?
答案1
我已经接受了遍历页面并设置变量并添加了必要的组件,以便使用\label
-\ref
方法提取带有水印的页面。
我已经添加
\usepackage{atveryend}
\newcommand\printWatermarkedPages{%
\@ifundefined{r@watermark-pages}{??}{%
\edef\@thewatermarkedPages{\expandafter\expandafter\expandafter\@firstoffive\csname r@watermark-pages\endcsname}%
\expandafter\@printWatermarkedPage\@thewatermarkedPages,@%
}%
}
\AfterLastShipout{
\immediate\write\@auxout{\string\newlabel{watermark-pages}{{\@watermarkedPages}{}{}{watermark.end}{}}}
}
它做了两件事:
.aux
在参考\label
文献末尾处写入“水印标签”atveryend
的\AfterLastShipout
。您可能不需要这个,只需使用\AtEndDocument
)。创建插入从上述参考中获得的
\printWatermarkedPages
带水印页面的超链接序列(类似于)。\WatermarkedPages
这是一个完整的最小示例:
\documentclass{article}
\usepackage[contents={Watermarked!},pages=some]{background}
\usepackage[colorlinks]{hyperref}
\pagenumbering{arabic}
\newcounter{watermark}
\makeatletter
\newif\ifWatermarkPage\WatermarkPagefalse% true if page should be watermarked
\newif\ifStillWatermarking\StillWatermarkingfalse% true if we want a watermark on the next page
\AddEverypageHook{%
\ifWatermarkPage%
\bg@material%
\else%
\ifStillWatermarking%
\global\WatermarkPagefalse%
\AddWatermarkLabels\bg@material%
\fi%
\fi%
\global\WatermarkPagefalse%
}
% will hold a comma separated list of labels for the watermarked pages
\def\@watermarkedPages{\@gobble} % gobble will eat an initial comma
% add a watermark and create a label - unless we've already done this for this page
\newcommand\AddWatermarkLabels{%
\ifWatermarkPage% already have label, so do nothing
\else%
\global\WatermarkPagetrue
\refstepcounter{watermark}% increment number of watermarked pages
% NB. Using \label{watermark:\thepage} doesn't write to the aux file
% when this is called via \AddEverypageHook, so we do it ourselves:
\immediate\write\@auxout{\string\newlabel{watermark:\thepage}{{\thewatermark}{\thepage}{}{watermark.\thewatermark}{}}}
\global\edef\@watermarkedPages{\@watermarkedPages,watermark:\thepage}% add label
\fi
}
\newenvironment{watermarked}%
{\global\StillWatermarkingtrue\AddWatermarkLabels}
{\global\StillWatermarkingfalse\AddWatermarkLabels} % this page still needs a watermark
% The next macros pass the labels in \@watermarkedPages to \pageref taking
% care to add commas between references and an "and" between the last two.
\def\@printWatermarkedPage#1,#2{\pageref{#1}%
\ifx#2@\let\@nextWatermark\@gobble%
\else\let\@nextWatermark\@printWatermarkedPages%
\fi%
\@nextWatermark#2%
}
\def\@printWatermarkedPages#1,#2{%
\ifx#2@ and \let\@nextWatermark\@gobble\else ,\fi%
\pageref{#1}%
\@nextWatermark#2%
}
\newcommand\WatermarkedPages{\expandafter\@printWatermarkedPage\@watermarkedPages,@}
\usepackage{atveryend}
\newcommand\printWatermarkedPages{%
\@ifundefined{r@watermark-pages}{??}{%
\edef\@thewatermarkedPages{\expandafter\expandafter\expandafter\@firstoffive\csname r@watermark-pages\endcsname}%
\expandafter\@printWatermarkedPage\@thewatermarkedPages,@%
}%
}
\AfterLastShipout{
\immediate\write\@auxout{\string\newlabel{watermark-pages}{{\@watermarkedPages}{}{}{watermark.end}{}}}
}
\makeatother
\begin{document}
Watermarks on pages \printWatermarkedPages.% 2,4,5,6,7
Some text here on page 1.
\newpage
\begin{watermarked}
some text
\end{watermarked}
\begin{watermarked}
more text
\end{watermarked}
More text here.
\newpage
Page with no watermarked stuff.
\newpage
\begin{watermarked}
more text,
\newpage
on several pages
\newpage
\end{watermarked}
More text here. Start a new page.
\newpage
\begin{watermarked}
more text
\end{watermarked}
\newpage
Loop to find pages containing the watermarked environment. Hard coded
here.
The \arabic{watermark} watermarked environments appear on the following
pages \printWatermarkedPages.
\end{document}
\printWatermarkedPages
您可以按照通常使用的方式在文档中的任何位置使用\ref
。