I am looking for a way to take the output of a pageref, iterating through, and assigning a property to each of those pages. Specifically, I want to take this:
\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}
and at the end of the loop assign to each page the \BgThispage flag so the watermark appears on all the relevant pages. Is there a clean way to do this?
Edit: I made some changes to try to merge the first solution with my existing solution, below:
\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}
This guy returns a missing \begin{document} error, which I cannot figure out why. It goes to the text inside the watermarked environment, namely "Inside watermarked on page \thepage."
missing \begin{document}error because the when you copied and pasted my solution the lineInside watermarked on pageshould have been part of the comment on the previous line. – Jul 26 '14 at 14:09