# Problem: tikz, pgfplots and external coordinates using overlay

I need to draw arrows from text to a pgfplots graph using TikZ. For some reason, the arrow coordinates are not where I want them to be in the pgfplots graph. Here is an example:

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{width=7cm}

\begin{document}

Text \tikz[remember picture] \node[circle,fill=red!50] (n1) {}; text.
\vspace{1in}

\pgfplotsset{domain=-1:1}
\begin{tikzpicture}
\begin{axis}[remember picture,xlabel=A normal sized $x$ label]
\node (c) at (axis cs:0,0.5) {$a$};
\end{axis}
\end{tikzpicture}
\vspace{1in}

Text \tikz[remember picture] \node[fill=blue!50] (n2) {}; text.

\begin{tikzpicture}[remember picture,overlay]
\draw [->,red,very thick] (n1) -- (n2);
\draw [overlay,->,very thick] (c) -- (n2);
\end{tikzpicture}

\end{document}


Letter 'a' seems to be in the correct place at (0,0.5), but the starting point of the black arrow is not. It seems to be too much to the right.

The red arrow, which is not defined using the pgfplots graph, seems to be correct.

Is there some option, which I have missed?

Thank you for any help!

-
You better provide the remember picture option to the tikzpicture env. but I can reproduce this. This can be also be seen within the same TikZ picture without overlay or remember picture. Using \draw (c) -- (0,0); results with the same output. –  percusse Sep 16 '12 at 16:53

This seems to be a bug in pgfplots, I will take care of it.

Although I do not see the mistake, I can say how to AVOID the problem until the bugfix is ready - provided you are running with pgfplots 1.6.1 (the most recent stable):

\pgfplotsset{cell picture=if necessary}

to your preamble (or \begin{axis}[..., cell picture=if necessary] to selected axes) will fix it.

ATTENTION: in this case, you need to move remember picture from \begin{axis} to \begin{tikzpicture}!

You can also say cell picture=false. In this case you will receive error messages if pgfplots really needs the initial setting (which is cell picture=true as you might have guessed).

Technical explanation:

I see that it is somehow related to the fact that pgfplots creates a so-called "cell-picture" and moves it to implement its positioning choices (the anchors). A cell-picture means that it creates an additional \tikzpicture...\endtikzpicture internally just like a pgf matrix.

The initial value cell picture=true is mainly for backwards compatibility. In general, it has advantages to avoid cell pictures. They are necessary if you choose an anchor which is NOT in the list

north, north west, west, south west, south, south east, east, north east, north, center, origin, above origin, left of origin, right of origin, below origin.

For example, the choice anchor=above north requires a cell picture.

-
Thank you for the answer. I have a problem with my Miktex, which has version 1.6.1 of pgfplots. I get an error message "! Package pgfkeys Error: I do not know the key '/pgfplots/cell picture' and I am going to ignore it. Perhaps you misspelled it.". I tried both ways to use the option. What can I do now? –  user18778 Oct 19 '12 at 7:17
Are you sure that you hae pgfplots 1.6.1 ? The .log file of your test should contain the version number... –  Christian Feuersänger Oct 31 '12 at 20:47
@Christian, can you give an update on the state of this bug? I have pgfplots 1.7, but have been running into similar problems. I tried the temporary solution you provide, and it seems to do what it's supposed to. But I can't use it because it breaks alignment when used in a \nextgroupplot. –  jja Mar 13 '13 at 20:30
@jja The bug is unresolved. Do I understand correctly that groupplots and cell picture produce problems? –  Christian Feuersänger Mar 13 '13 at 21:01
@jja update: I have managed to fix the bug right now. At least for pdftex and dvips. will (probably) become part of the next stable, i.e. of pgfplots 1.8 –  Christian Feuersänger Mar 13 '13 at 22:46