# How can I prevent or reduce multiply defined labels?

I have a format which repeatedly uses TikZ pictures in the background of certain pages. Because these pictures require remember picture, overlay, they cannot be externalised. To control compilation time, I therefore save pictures I need repeatedly into boxes at the beginning of the document and then just use those boxes on the pages where I need them.

This works fine. The only problem is that I get multiply defined labels. The problem is not so much that LaTeX warns me about these, as that there are many of them and they drown out warnings I care about. In particular, I want to know if other labels get multiply defined, because that would screw up my cross-references.

So I'm trying to reduce the number of spurious warnings I get. I've made a little progress in the case of externalised images (by redefining a macro from pageslts only when externalising), but I need to do something different (if possible) for the pictures I'm boxing.

Here's a minimal example demonstrating the problem.

\documentclass{article}
\usepackage{tikz,everypage,kantlipsum}
\newsavebox\mybox

\begin{document}
\sbox\mybox{%
\begin{tikzpicture}[remember picture, overlay]
\shade [top color=blue!50!black, bottom color=blue!50!black, middle color=blue!50!cyan] (current page.north west) rectangle (current page.south east);
\end{tikzpicture}%
}%
\kant[1-8]
\kant[9-10]
\end{document}


So the point here is that the picture is generated once and saved to \mybox. This box is then put into the background on page 1. It is then put into the background on page 3. The output is fine.

The problem is that it generates a multiply-defined label warning

LaTeX Warning: Label pgfid1' multiply defined.


because the .aux file contains

\pgfsyspdfmark {pgfid1}{4736286}{47362867}
\pgfsyspdfmark {pgfid1}{4736286}{47362867}


Is there any help for this? Obviously, the warning is spurious in the sense that it doesn't hurt anything. But my list of warnings is being swamped by spurious warnings I can't get rid of and it is getting well nigh impossible to find the ones I do need to identify, especially when I'm in a hurry.

# EDIT

Just to clarify, I know that remember picture is responsible for writing the labels to the .aux file which trigger the warnings. But removing this is not an option because overlay requires it:

• Just wondering if this is related. If you switch off the remember option directive, the complaints will disappear. – marmot Feb 17 '18 at 4:48
• @marmot Yes, but I can't switch remember picture off because the whole point of doing it this way is that I need overlay. If I could turn remember off, I wouldn't save them to boxes in the first place: I'd externalise them instead. So, yes, this question is related in the sense that the issue I know is causing the warnings is the one which Symbol1 suggested was causing yours. But diagnosis contained the solution in your case - the symbols don't need to be remember. Unfortunately, diagnosis is not sufficient here. – cfr Feb 17 '18 at 4:58
• @marmot See edit above for clarification. The image at the bottom shows the effect of attempting to use overlay without remember picture. This is why overlay, remember picture always needs 2 runs to be placed correctly: the coordinates written to the .aux on the first run are used to place the picture correctly on the second. – cfr Feb 17 '18 at 5:04
• I understood that, and apologize for my not-so-clear comment above. I wanted to say that the fact that switching off the remember picture option cures it is further evidence that this is the same problem. – marmot Feb 17 '18 at 5:05
• @marmot Oh, I see. Sorry. Yes. It is definitely the same cause. And I wish that I could find an equally satisfactory solution! I just meant that this question isn't a duplicate because when you say 'related' people get trigger-happy with the close button ;). – cfr Feb 17 '18 at 5:06

As I mentioned in comments I think a real solution would be to not use the tikz remember mechanism as here you do not need to save any current coordinates as you are placing the tikz at a fixed position on the page.

However starting from here the easiest thing is just to ignore all the settings after the first by writing a command to the aux file that discards the setting.

\documentclass{article}
\usepackage{tikz,everypage,kantlipsum}
\newsavebox\mybox
\makeatletter
\def\eatpgfmark{\write\@auxout{\string\@gobblefour}}
\makeatother
\begin{document}
\sbox\mybox{%
\begin{tikzpicture}[remember picture, overlay]
\shade [top color=blue!50!black, bottom color=blue!50!black, middle color=blue!50!cyan] (current page.north west) rectangle (current page.south east);
\end{tikzpicture}%
}%
\kant[1-8]
\kant[9-10]
\end{document}


which produces an aux file

\relax
\pgfsyspdfmark {pgfid1}{4736286}{47362867}
\@gobblefour
\pgfsyspdfmark {pgfid1}{4736286}{47362867}


so the second \pgfsysmark is discarded.

• Thanks. What would a 'real' solution look like? (My actual picture is a bit more complicated, though, so I'm not certain a real solution would work there.) – cfr Feb 17 '18 at 19:50
• @cfr as far as I can see you are just using the overlay mechanism to get a zero sized box that you can then put at a fixed position on the page, so you could use any tikz and make it zero sized with tikz or \smash or whatever and then position it on the page using a standard pagestyle, or the addthispage hook that you use above or anything else – David Carlisle Feb 17 '18 at 23:28
• I guess I'm being dumb. But won't that insert the picture at whatever point in the text? That is, it won't make sure that the bottom left corner of the picture is at the bottom left of the paper and the top right corner at the top right, will it? – cfr Feb 18 '18 at 0:32
• @cfr there are many ways to add something at a specific point, that is I assume the hook you are using is doing, the classic way is to use a page style, if you use a page style with \begin{picture}(0,0)\put(a,b){anything}\end{picture} then anything will be overlaid at position a,b over the page contents – David Carlisle Feb 18 '18 at 0:35
• I tried \documentclass{article} \usepackage{tikz,everypage,geometry,kantlipsum} \newsavebox\mybox \begin{document} \sbox\mybox{% \begin{picture}(0,0)% \unitlength=1mm% \put(-25.4,25.4){% \begin{tikzpicture}[baseline=(current page.north west)] \shade [top color=blue!50!black, bottom color=blue!50!black, middle color=blue!50!cyan] (current page.north west) rectangle (current page.south east); \end{tikzpicture}% }% \end{picture}% }% \AddThispageHook{\usebox\mybox} \kant[1-8] \AddThispageHook{\usebox\mybox} \kant[9-10] \end{document}` :(. – cfr Feb 18 '18 at 1:08