获取环境所在的页面

获取环境所在的页面

我正在尝试创建一个在背景中粘贴水印的新环境。具体来说,我有类似的东西

\usepackage{background}

\newenvironment{\watermarked}{
\backgroundsetup{contents={This section contains a watermark., opacity=0.5}
}

我还需要计算哪些页面具有此环境。我该如何统计页面数量?基本上,我需要创建一个后续环境,它说

水印出现在 XY、ZA、BC 页面上

我该如何做呢?

最终编辑:

这正是我所期望的:

\documentclass{article}
\usepackage[scale=1,opacity=0.5]{background}
\usepackage{hyperref}
\usepackage{forloop}
\usepackage{refcount}

\newcounter{markthis}
\newcounter{endmarkthis}

\newenvironment{watermarked}%
{% at start of watermarked
\SetBgContents{This section contains proprietary information.}
\stepcounter{markthis}\phantomsection\label{marked:\themarkthis}
Inside watermarked on page \thepage.
}
{% at end of watermarked
\stepcounter{endmarkthis}\phantomsection\label{endmarked:\theendmarkthis}
}
\SetBgContents{}

\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}

仍在研究逻辑以确保不会重复页面,但我可以稍后处理。干得好,伙计们!非常感谢!

答案1

这是一个可能的答案的开头。我在水印环境和包的开始和结束处使用计数器hyperref来启用那里的标签。最后,我循环查看标签并查看 s \pageref。当页面需要水印时,atbegshi包会进行修改。shipout

如果您想清理代码,您可以不用这些label/ref东西,只需shipout收集有水印的页面的页码。

编辑:楼主已经解决了我原稿中的几个问题。我为他的改进做了更多工作。以下是更新后的待办事项列表:

  • 列表的终止条件仍然是硬编码的。(其余的是通用的。)

  • pageref禁止显示列表中的链接

  • 折叠页面指示,以便 2 2 4-6 7 读取 2, 4-7

  • 将所需的水印放置在正确标识为需要水印的页面上。


\documentclass{article}
\usepackage{background}
\usepackage{atbegshi}
\usepackage{color}    % for shipout example
\usepackage{picture}  % for shipout example
\usepackage{etoolbox} % for toggle

\usepackage{forloop}
\usepackage{refcount}

\usepackage{hyperref} % should usually be last package loaded

\newcounter{markthis}
\newcounter{endmarkthis}

% true if current page has (part of a) watermarked environment
\newtoggle{markthispage}

% prepare to turn off mark at the end of the page, but not now
\newtoggle{readytoturnoffmark}

\AtBeginShipout{%
\iftoggle{markthispage}{%
   %\backgroundsetup{contents={This section contains a watermark., opacity=0.5}}
   \AtBeginShipoutUpperLeft{%
      \put(0.5\paperwidth,-0.5\paperheight){\circle{200}}}%
   }
   {} % else clause
\iftoggle{readytoturnoffmark}{%
   \global\togglefalse{markthispage}} % for following pages
   {} % else clause
}


\newenvironment{watermarked}%
{% at start of watermarked
%\backgroundsetup{contents={This section contains a watermark.,
%    opacity=0.5}}
\global\toggletrue{markthispage}
\global\togglefalse{readytoturnoffmark}
\stepcounter{markthis}\phantomsection\label{marked:\themarkthis}
Inside watermarked on page \thepage.
}
{% at end of watermarked
\stepcounter{endmarkthis}\phantomsection\label{endmarked:\theendmarkthis}
\global\toggletrue{readytoturnoffmark}
}

\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. 
Loop termination hard coded here.

\noindent
The \arabic{markthis} watermarked environments appear on pages \\

\newcounter{loopcounter}
\forloop[1]{loopcounter}{1}{\value{loopcounter}<5}{
\ifnum \getpagerefnumber{marked:\arabic{loopcounter}} <  \getpagerefnumber{endmarked:\arabic{loopcounter}}
    \pageref{marked:\arabic{loopcounter}}-\pageref{endmarked:\arabic{loopcounter}}
\else
    \pageref{marked:\arabic{loopcounter}}
\fi
}
\end{document}

在此处输入图片描述

相关内容