# pgfplots axis cs nodes inside a scaled tikzpicture

I would like to join a node inside a pgfplots axis to a node belonging to a different tikzpicture. I can do this with an overlay from inside the axis. However, if I scale the tikzpicture that encloses the axis, the overlay breaks.

For example, the following works fine:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[remember picture]
\node (n1) {n1};
\end{tikzpicture}

\begin{tikzpicture}[remember picture]
%\begin{tikzpicture}[remember picture, scale=0.85, transform shape]
\node (n2) {n2};
\draw[overlay, green] (n1) -- (n2);
\begin{axis}[right of=n2, name=plot, clip=false]
\draw[overlay, red] (n1.east) -- (axis cs:2,3);
\end{axis}
\end{tikzpicture}
\end{document}


When I add a scale parameter (e.g. scale=0.85 with transform shape) to the tikzpicture outside the axis (and run pdflatex twice), the red line no longer starts from the node (n1):

Everything works fine if the scale is a parameter of the axis, but I would like to scale the whole tikzpicture. Is there a way to make this work correctly?

-
When you supply the scale parameter to the axis, only the plot dimensions, but not the text labels will be scaled, and the number of tick labels may be different. The behaviour is basically as if you had supplied different values for width and height. Is that what you want, or do you actually want to scale the plot, text and all? –  Jake May 22 '12 at 11:45
@Jake Sorry for not being clear enough, I would like to scale the whole picture, including the text. I usually scale down my diagrams in beamer a bit and would like to use this to annotate them from the text in the enclosing frame. –  anton May 22 '12 at 14:45

As seen in the picture on the right, the vector magnitude is shrunk down by the scaling factor 0.85 compared to the original length. Hence we can deduce that the scaling is applied after the required calculations. It's not the best choice but one can supply the scaling to the required keys manually as follows

\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\def\myscale{0.4}
\tikzset{myscalar/.style={
node distance=\myscale cm and \myscale cm,
every node/.style={scale=\myscale},
every axis/.style={
major tick length=0.15*\myscale cm,
minor tick length=0.1*\myscale cm,
mark options={scale=\myscale},
scale=\myscale
}
}
}
\begin{document}
\begin{frame}{Title}
\begin{tikzpicture}[remember picture]
\node (n1) {n1};
\end{tikzpicture}

\begin{tikzpicture}[remember picture,transform shape,myscalar]
\node (n2) {n2};
\draw[overlay, green] (n1) -- (n2);
\begin{axis}[right of=n2, name=plot, clip=false]

One can change \myscale by re\defining it after some point.