XeLaTeX watermarking environment macro

I’m totally enjoying my switch from LaTeX to XeLaTeX, with all of the added benefits that entails. Over the years I’ve cobbled together and built a number of helpful macros so this will be my first of many posts to share them. Some may not be XeLaTeX specific, but as that’s the environment I use, many of them will not be fully LaTeX compatible.

The first entry is a simple watermarking system, it’s a slight modification of a script from Filox, an environment throughout which each page receives a watermark. just create a file named ‘watermark .png’, and put the macro in a file named ‘watermark.sty’.

\usepackage{everypage}
\newenvironment{water}{\AddEverypageHook{\waterb}}{
\AddThispageHook{\waterb}\AddEverypageHook{\watere}
}
\makeatletter
\newcommand{\waterb}{
\AddToShipoutPicture*{%
\setlength{\@tempdimb}{.3\paperwidth}%
\setlength{\@tempdimc}{.4\paperheight}%
\setlength{\unitlength}{1pt}%
\put(\strip@pt\@tempdimb,\strip@pt\@tempdimc){%
\includegraphics[height=100mm]{watermark.png}
}%
}}
\makeatother
\makeatletter
\newcommand{\watere}{
\AddToShipoutPicture*{%
\setlength{\@tempdimb}{.3\paperwidth}%
\setlength{\@tempdimc}{.4\paperheight}%
\setlength{\unitlength}{1pt}%
\put(\strip@pt\@tempdimb,\strip@pt\@tempdimc){%
\includegraphics[height=100mm]{watermark.png}
}%
}}
\makeatother
view raw watermark.tex hosted with ❤ by GitHub

Now I want another type of draft watermark I can use when circulating incomplete documents, so I alter that to put ‘DRAFT’ diagonally across the background. I put this into a file called ‘draft_watermark.sty’.

\usepackage{everypage}
\newenvironment{water}{\AddEverypageHook{\waterb}}{
\AddThispageHook{\waterb}\AddEverypageHook{\watere}
}
\makeatletter
\newcommand{\waterb}{
\AddToShipoutPicture*{%
\setlength{\@tempdimb}{.5\paperwidth}%
\setlength{\@tempdimc}{.5\paperheight}%
\setlength{\unitlength}{1pt}%
\put(\strip@pt\@tempdimb,\strip@pt\@tempdimc){%
\makebox(0,0){\rotatebox{45}{\textcolor[gray]{0.75}%
{\fontsize{6cm}{6cm}\selectfont{DRAFT}}}}%
}%
}}
\makeatother
\makeatletter
\newcommand{\watere}{
\AddToShipoutPicture*{%
\setlength{\@tempdimb}{.5\paperwidth}%
\setlength{\@tempdimc}{.5\paperheight}%
\setlength{\unitlength}{1pt}%
\put(\strip@pt\@tempdimb,\strip@pt\@tempdimc){%
\makebox(0,0){\rotatebox{45}{\textcolor[gray]{1}%
{\fontsize{6cm}{6cm}\selectfont{DRAFT}}}}%
}%
}}
\makeatother
view raw draft_watermark.tex hosted with ❤ by GitHub

and now I can switch between the two identical environments by simply switching which style file is included, and I open and close the environment simple using the following syntax:

\begin{water}
% watermarked pages here
\end{water}
view raw watermark_usage.tex hosted with ❤ by GitHub

It’s a little hacky in the way it works, but I’ve used it without issue for quite a while. Enjoy.