# How can I refer to the page number corresponding to \lastdocpage - 2?

Follow-up question to Hiding watermark for first page?.

I have recently learned about \lastdocpage (from the previous question), but is there a way to refer to two pages prior to \lastdocpage? To understand the context, the watermark from the previous question should last from pages 2 to two pages prior to the end of the document.

MWE:

\documentclass[12pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{tikz}
\usepackage[version=3]{mhchem}
\usepackage [english]{babel}
\usepackage[printwatermark]{xwatermark}
\begin{document}
Stuff
\newpage
More stuff
\end{document}


In this MWE, the watermark is on pages 2 to \lastdocpage. I have tried \pages=2-{\lastdocpage-2}but this did not work.

-

According to the manual of xwatermark you should be able to write

pages=2-{\lastdocpage-2}


Indeed the manual contains the example

pages={\lastdocpage-2}-\lastdocpage


However, while the last version works, the first throws an error. Explicitly forcing it to a number as follows does however work:

pages=2-{\number\numexpr\lastdocpage-2}


So a complete document would be:

\documentclass[12pt]{article}

\usepackage[margin=2cm]{geometry}
\usepackage{tikz}
\usepackage[version=3]{mhchem}
\usepackage [english]{babel}
\usepackage[printwatermark]{xwatermark}

\begin{document}
Stuff
\newpage
More stuff
\newpage
Third page
\newpage
Fourth page

\end{document}


[The above requires you have a standard modern tex engine that includes the etex features.]

-
Your version is definitely much easier ;-) –  Christian Hupfer Jul 15 '14 at 8:03

You have to save the value of \lastdocpage to a counter and decrease the counter by the appropiate value, i.e. in this case by two. Afterwards it is possible to use the counter value as end limit of the watermarks.

To use the counter value of lastwatermarkpage, it is possible either

• saying \thelastwatermarkpage
• \number\value{lastwatermarkpage} which guarantess, that the output is a number, whereas \thelastwatermark is a command, which can basically redefined to do anything else, so usage is discouraged.

I have used a \forloop to generate some dummy pages. In the production run, this can be removed of course, as well as the loopcounter definition.

\documentclass[12pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{tikz}
\usepackage[version=3]{mhchem}
\usepackage [english]{babel}
\usepackage[printwatermark]{xwatermark}

\usepackage{forloop}

\newcounter{loopcounter}

\newcounter{lastwatermarkpage}
\setcounter{lastwatermarkpage}{\lastdocpage}