# Pgfplots label get repeated by tikz spy library

My question is very similar to this question. I label a certain curve in my plot and magnify a part of that. The \spy command repeats the \label which causes multiply defined label warnings which are annoying.

The solution presented in the linked question does not apply to my example as I have no way of naming a node I can refer to later.

How can I stop \spy from repeating the label in a pgfplot?

 \documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{spy}

\begin{document}

\begin{tikzpicture}[spy using outlines={circle, magnification=2, size=1cm,
connect spies}]
\begin{axis}
\end{axis}
\begin{scope}
\spy[green!30!black,size=1cm] on (3,.85) in node [fill=white] at (4,2);
\end{scope}
\end{tikzpicture}

\ref{plot:curve}
\end{document}

-
Hm, that's annoying. Maybe there's a way to undefine the \label command within the spy scope? –  Jake Jun 20 '12 at 15:53
@Jake I think that has been tried in the linked topic with no luck (\def\label#1{}). I guess we need to dive deeper into the pgfplots label handeling mechanism... –  Martin H Jun 20 '12 at 17:11

The problem happens whenever a \label depends on the context of the item which is to be labelled. In this case, pgfplots provides its own label functionality. This, in turn, boils down to the standard LaTeX \label command eventually.

And the standard LaTeX \label results in \write{....} (to write something to the .aux file).

This \write is the "problem", combined with the spy libary: the spy library collects all its stuff into a TeX box and typesets that twice. A \write is also part of that box, and will be executed everytime the box is used (from what I know and see). A solution would be to use \immediate\write instead of \write in this context.

** EDIT: ** this answer was RASH; its suggested solution below might BREAK links: while my analysis above seems to be correct, the proposed solution below is probably of less use than I claimed. When it was later yesterday night, I did not think about the most important question: why has someone deferred the write!? Of course, he did so because the label mechanism is used to establish a connection between the labelled item and the reference. Typically, this requires the page number where the labelled item is typeset. And this, in turn, can only be retrieved using deferred write.

Perhaps you can still use my hack for pgfplots because there, you do not want to know the page number - you want to have the small legend image. That should do. However, my hack might (?) break hyperrefs.

But the solution is clearly wrong for any other \label because it will destroy page number information for larger documents.

Use it on your own risk.

You can replace the \write functionality temporarily as in the following example - and it will work (for every \label, not just for pgfplots):

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{spy}

\newenvironment{writereplace}{%
\begingroup
\let\writeold=\write
\def\write{\immediate\writeold}%
}{%
\endgroup
}%

\begin{document}

\begin{writereplace}
\begin{tikzpicture}[spy using outlines={circle, magnification=2, size=1cm,
connect spies}]
\begin{axis}
\end{axis}
\begin{scope}
\spy[green!30!black,size=1cm] on (3,.85) in node [fill=white] at (4,2);
\end{scope}
\end{tikzpicture}
\end{writereplace}

\ref{plot:curve}
\end{document}

-
Please see my edit for side-effects of my suggested solution. –  Christian Feuersänger Jun 21 '12 at 5:25
It seems that the warnings are still there in my large project with tikz external and so on. I use the labels in the picture caption and they are correct so I choose to ignore them for now. For the MWE your solution seems to work –  Martin H Jun 21 '12 at 10:55
Ok... my guess was that it'll work for larger projects as well. Sorry about that. –  Christian Feuersänger Jun 23 '12 at 7:54
No need to apologize, it just means that my MWE is not good enough. I will try to squeeze in some more testing once my current report is out of the way... –  Martin H Jun 23 '12 at 16:59