遍历页面并设置变量

遍历页面并设置变量

我正在寻找一种方法来获取 pageref 的输出,进行迭代,并为每个页面分配一个属性。具体来说,我想这样做:

\documentclass{article}
\usepackage[contents={Watermarked!},pages=some]{background}
\usepackage{hyperref}
\usepackage{forloop}
\usepackage{refcount}

\newcounter{markthis}
\newcounter{endmarkthis}
\newenvironment{watermarked}%
{% at start of watermarked
    \stepcounter{markthis}\phantomsection\label{marked:\themarkthis}
Inside watermarked on page \thepage.
}
{% at end of watermarked
\stepcounter{endmarkthis}\phantomsection\label{endmarked:\theendmarkthis}
}
\begin{document}

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}

Loop to find pages containing the watermarked environment. Hard coded
here.

\noindent
The \arabic{markthis} watermarked environments appear on pages \\
\newcounter{thenumber}
\forloop[1]{thenumber}{1}{\value{thenumber}<5}{
\ifnum \getpagerefnumber{marked:\arabic{thenumber}} < \getpagerefnumber{endmarked:\arabic{thenumber}}
    \pageref{marked:\arabic{thenumber}}-\pageref{endmarked:\arabic{thenumber}}
\else
    \pageref{marked:\arabic{thenumber}}
\fi
}

\end{document}

并在循环结束时为每个页面分配 \BgThispage 标志,以便水印出现在所有相关页面上。有没有简洁的方法可以做到这一点?

编辑:我做了一些更改,尝试将第一个解决方案与我现有的解决方案合并,如下所示:

\documentclass{article}
\usepackage[content={Watermarked!},pages=some]{background}
\usepackage{hyperref}
\usepackage{forloop}
\usepackage{refcount}

\makeatletter
\newif\ifWatermarking\Watermarkingfalse
\newif\ifLastWatermark
\AddEverypageHook{%
   \ifWatermarking\bg@material%
     \ifLastWatermark\global\Watermarkingfalse\fi%
   \fi}

   \newcounter{markthis}
\newcounter{endmarkthis}

\newenvironment{watermarked}%
    {\global\Watermarkingtrue\global\LastWatermarkfalse
       \stepcounter{markthis}\phantomsection\label{marked:\themarkthis}
}%
Inside watermarked on page \thepage.
    {\global\LastWatermarktrue
    \stepcounter{endmarkthis}\phantomsection\label{endmarked:\theendmarkthis}}
\makeatother

\begin{document}

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}

Loop to find pages containing the watermarked environment. Hard coded
here.

\noindent
The \arabic{markthis} watermarked environments appear on pages \\
\newcounter{thenumber}
\forloop[1]{thenumber}{1}{\value{thenumber}<5}{
\ifnum \getpagerefnumber{marked:\arabic{thenumber}} < \getpagerefnumber{endmarked:\arabic{thenumber}}
    \pageref{marked:\arabic{thenumber}}-\pageref{endmarked:\arabic{thenumber}}
\else
    \pageref{marked:\arabic{thenumber}}
\fi
}

\end{document}

这个家伙返回了一个缺失 \begin{document} 错误,我不明白为什么。它转到水印环境内的文本,即“页面 \thepage 上的水印内”。

答案1

编辑二

这是一个功能齐全的解决方案。MWE 示例是您的。我的代码将水印放在第 2、4、5、6 和 7 页上,并在文档末尾添加指向它们的超链接。我将在下面做一些评论。

\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\relax%
  \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,@}
\makeatother

\begin{document}
% watermarks on pages 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 \WatermarkedPages.

\end{document}

一些评论:

  1. 解释一下,background 包使用 everypage 包通过\bg@material使用 PGF/TikZ 定义来装饰页面。所以这就是为什么上面的代码\bg@material里面有\AddEverypageHook。(特别是,您不需要再向文件中添加任何包,因为 background 已经加载了我使用的所有内容。)

  2. 大约一半的代码用于定义\WatermarkedPages从中提取引用列表\@watermarkedPages并将其传递给哪个,\pageref以便它可以创建指向相应页面的超链接。很可能有更有效的方法来实现这一点。

  3. 我之前的解决方案的问题是,超链接都指向文档的第一页,而不是指向相应的页面,尽管 PDF 文件中显示的页码和参考本身都是正确的。[编辑:此问题在超引用产生的标签链接目标不正确]。解决方案很简单,就是\refstepcounter对带有水印的页面数量进行计数器(当然,因为我自己写了辅助文件条目,所以我可以伪造这个)。

  4. 我在将标签写入aux文件时遇到了问题,因为并非所有\label执行的命令都会导致将条目写入辅助文件。我认为使用\labelinside可能会出现问题\shipout。为了解决这个问题,我\newlabel使用 将命令明确写入辅助文件\immediate\write,这可能有点夸张。

相关内容