# Overlaying watermark on \includepdf *at varying defined locations on successive pages*

When using advice elsewhere here to place a watermark over every page of an \includepdf, I'd like to vary the location of the watermarks on each page. One very laborious solution is to include the PDF one page at a time (see "This works" segment). I hoped to use this neat list-as-queue idea to queue up all of the co-ordinates, then pop them from within the pdfpages pagecommand on each page (see "This doesn't" segment; needs commenting-out for "This works" to complete).

Unfortunately my understanding of the underlying mechanisms fails me at this point: the error Missing \endcsname inserted is completely opaque to me. I'm guessing that the \el{} macro to pop co-ordinates can't be used in the context I'm using it in; would be grateful for suggestions for a fix..

Two support documents required; I'd suggest grabbing this for the PNG watermark image, alpha_noset.png:

wget https://www.imagemagick.org/Usage/masking/alpha_noset.png


.. and copying in any two-or-more-page PDF you have handy as inclusion.pdf.

\documentclass[10pt,a4paper]{letter}

\usepackage{tikz}      % Watermark overlay
\usepackage{pdfpages}  % Multi-page PDF inclusions
\usepackage{graphicx}  % Graphical inclusions
\usepackage{xparse}

% Pull values out of a queue, from https://tex.stackexchange.com/questions/230618/iterator-like-command-where-each-use-expands-to-an-item-from-list-defined-earli
\ExplSyntaxOn
\NewDocumentCommand{\deflist}{O{,}m}
{
\seq_set_split:Nnn \l_narebski_list_seq { #1 } { #2 }
}
\NewDocumentCommand{\el}{}
{
\seq_item:Nn \l_narebski_list_seq { 1 }
\seq_pop_left:NN \l_narebski_list_seq \l_narebski_waste_tl
}
\seq_new:N \l_narebski_list_seq
\tl_new:N \l_narebski_waste_tl
\ExplSyntaxOff

\signature{x}
\address{Etc.}

\begin{document}

\begin{letter}{To whom it may concern,}

\opening{Dear sir/madam,}
Really.
\closing{Yours sincerely,}

\newpage
\pagenumbering{gobble}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This works: include PDF one page at a time, repositioning the
% watermark individually for each page.

% Integration of tikzpicture to every \includepdf taken from https://tex.stackexchange.com/questions/12838/can-i-add-tikzpictures-to-pages-included-with-pdfpages

\setkeys{pdfpages}{pagecommand={
\begin{tikzpicture}[remember picture, overlay]
\node[inner sep=0pt] at (\atx,\aty) {%
\includegraphics[width=8cm,angle=\ata]{alpha_noset.png}%
};%
\end{tikzpicture}
}}

% Co-ordinates are x=\atx, y=\aty and rotation angle=\ata.
\includepdf[pages={1}]{inclusion.pdf}
\includepdf[pages={2}]{inclusion.pdf}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This doesn't, but I'd prefer it: queue up list of watermark co-
% ordinates and pop 'em off as needed.

% Here we line up the list of co-ordinates; in normal text, \el{}
% would retrieve them, one at a time (I could say "first: \el{},
% second: \el{}, third: \el{}", etc.).  Within the pdfpages pagecommand,
% \el{} causes an error instead.
\deflist{11,3,15,-2,-18,90} % Same sequence of atx,aty,ata values as above.

\setkeys{pdfpages}{pagecommand={
\begin{tikzpicture}[remember picture, overlay]
\node[inner sep=0pt] at (\el{},\el{}) {%
\includegraphics[width=8cm,angle=\el{}]{alpha_noset.png}%
};%
\end{tikzpicture}
}}
%% COMMENT/UNCOMMENT THE NEXT LINE TO SUPPRESS/GENERATE THE FAIL:
\includepdf[pages={1,2}]{inclusion.pdf}

\end{letter}

\end{document}

• TikZ supports random numbers; Why don't you pass random numbers to your \atx, \aty? See tutorial 4 in the pgf manual at ctan.org/pkg/pgf or code like \foreach \atx, \aty in {1/1,...,10/10} {\pgfmathparse{rnd}\pgfmathresult} (there is better code code than this, look through the manual.) – Huang_d May 24 '18 at 15:34
• Thanks for your quick response; that loop structure looks really useful, but since I need the values in what I'd (in other programming languages) term a callback (pdfpages' pagecommand), retrieving the next three co-ordinates on each call, I can't go through them with a foreach: instead I think need something with "yield"-like behaviour. I did experiment with random numbers, but in this instance I want to choose specific co-ordinates. – C. Hughes May 24 '18 at 15:49
• This won't work. \seq_pop... is not expandable. You need to get the values before executing the \node command. – Ulrike Fischer May 24 '18 at 15:56
• If I try to get the values in the pagecommand but before the \node it still doesn't seem to work though; is there any approach that you think would make this general idea work? – C. Hughes May 24 '18 at 19:48

## 1 Answer

You can't use the non-expandable command \seq_pop_left:NN in this context. Instead you can use \seq_pop_left:NN first to extract the object into a new macro, then use this new macro in the \node command.

This would not give the expected result through, because you defined \l_narebski_list_seq to be local. So LaTeX will always use the first values, because the "pop" is no longer visible after the current group. You can use a global sequence instead:

\documentclass[10pt,a4paper]{letter}

\usepackage{tikz}      % Watermark overlay
\usepackage{pdfpages}  % Multi-page PDF inclusions
\usepackage{graphicx}  % Graphical inclusions
\usepackage{xparse}

% Pull values out of a queue, from https://tex.stackexchange.com/questions/230618/iterator-like-command-where-each-use-expands-to-an-item-from-list-defined-earli
\ExplSyntaxOn
\NewDocumentCommand{\deflist}{O{,}m}
{
\seq_gset_split:Nnn \g_narebski_list_seq { #1 } { #2 }
}
\NewDocumentCommand{\el}{m}
{
\seq_gpop_left:NN \g_narebski_list_seq #1
}
\seq_new:N \g_narebski_list_seq
\ExplSyntaxOff

\signature{x}
\address{Etc.}

\begin{document}

\begin{letter}{To whom it may concern,}

\opening{Dear sir/madam,}
Really.
\closing{Yours sincerely,}

\newpage
\pagenumbering{gobble}

\deflist{11,3,15,-2,-18,90} % Same sequence of atx,aty,ata values as above.

\setkeys{pdfpages}{pagecommand={
\begin{tikzpicture}[remember picture, overlay]
\el\atx \el\aty \el\ata
\node[inner sep=0pt] at (\atx,\aty) {%
\includegraphics[width=8cm,angle=\ata]{alpha_noset.png}%
};%
\end{tikzpicture}
}}
\includepdf[pages={1,2}]{tittoc.pdf}

\end{letter}

\end{document}

• Superb! This works perfectly; thank you very much Marcel :) – C. Hughes May 25 '18 at 15:37