# Getting pages where an environment is located

I am trying to create a new environment that sticks a watermark in the background. Specifically, I have something like

\usepackage{background}

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


I need to also count which pages have this environment. How would I keep count of the pages? Basically, I need to create a follow-on environment that says

The watermark appears on pages X-Y, Z-A, B-C

How would I go about doing that?

Final Edit:

This gets exactly what I would expect:

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


still working on the logic to make sure I don't duplicate pages, but I can cope with that later. Great work, guys! Thanks so much!

• Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Jul 23 '14 at 19:17
• Your definition of the environment is syntactically incomplete (missing }) and the environmentname is not a macro, i.e. \newenvironment{watermarked} – user31729 Jul 23 '14 at 19:20

Here is the beginning of a possible answer. I use counters at the start and end of the watermarked environment and the hyperref package to enable labels there. At the end I loop on the labels and see the \pageref s. The atbegshi package modifies shipout when the page needs a watermark.

If you wanted to clean up the code, you could do without the label/ref stuff and just have shipout collect up the page numbers for watermarked pages.

Edit: The OP has solved several of the problems with my original draft. I've done more work with his improvement. Here's the updated todo list:

• The termination condition for the list is still hard coded. (The rest is generic.)

• Suppress the links in the displayed pageref s

• Collapse the page indications so that 2 2 4-6 7 reads 2, 4-7

• Put the desired watermark on the pages correctly identified as needing it.

\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

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


• That's a great start. I'll figure out the looping, and why "Draft" appears on all the watermarked pages that shouldn't have a watermark (probably a flag with the background package). Thank you so much! – webb Jul 24 '14 at 16:00
• You're welcome. When you've worked out the details you should edit the answer (or write your own) so that someone coming here sees a full solution. – Ethan Bolker Jul 24 '14 at 21:13
• I see that you've finished the solution as part of the question. That might confuse someone looking for it. Perhaps revert the question and post your own answer. And ... is the hardcoded 5 in your answer right? – Ethan Bolker Jul 24 '14 at 21:19
• Will do, I've got one more tweak though before I do that. Trying to figure out the watermark issue – webb Jul 24 '14 at 21:34
• If some of the things on the todo list still puzzle you, consider asking specific new questions, with small specific MWEs. You can link those questions to this one, and vice versa. – Ethan Bolker Jul 25 '14 at 22:51