# Using \ref in a ylabel of a pgfplot

When I use \label and \ref to distinguish two y axes the marks drawn by \ref are no longer on the lines after rotating it together with the rest of the label (see example below). Any ideas?

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[axis y line*=left, scale only axis,
ylabel={estimation 2 \ref{pgf:allsamples}}]
\label{pgf:allsamples}
\end{axis}
\begin{axis}[axis y line*=right, scale only axis,
ylabel={estimation 1 \ref{pgf:swingvotes}}, ylabel near ticks]
\end{axis}
\end{tikzpicture}
\end{document}


-
Can you edit your question and elaborate on "something went wrong". –  Peter Grill Dec 2 '11 at 18:34
Yep, that description was a bit weak, I hoped the picture would make clear what I mean. Anyway, Christian's patch did exactly what I wanted. I changed the description a bit so hopefully if anyone else has the same problem they will find this with google. –  Flogo Dec 3 '11 at 12:50

There is, indeed, a bug in pgfplots: the \ref command checks if "it is invoked from within a tikz picture". But this check is buggy because it, well is invoked from within a tikzpicture, but the tikzpicture has been interrupted to display a node.

As a result of the bug, the labels have an unexpected displacement.

Until the bug is fixed, you can use the following work-around:

\documentclass{article}

\usepackage{pgfplots}

\def\fixcheck{%
\def\tikzifinpicture##1##2{##2}%
}%

\begin{document}
\begin{tikzpicture}
\begin{axis}[axis y line*=left, scale only axis,
ylabel={\fixcheck estimation 2 \ref{pgf:allsamples}}]
\label{pgf:allsamples}
\end{axis}
\begin{axis}[axis y line*=right, scale only axis,
ylabel={\fixcheck estimation 1 \ref{pgf:swingvotes}}, ylabel near ticks]

In all your labels, it adds the \fixcheck macro which will simply cause the check to return "false". In that case, \ref will add a temporary tikzpicture.