7

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}}]
      \addplot[blue,mark=+] table {data2.csv};
      \label{pgf:allsamples}
    \end{axis}
    \begin{axis}[axis y line*=right, scale only axis,
             ylabel={estimation 1 \ref{pgf:swingvotes}}, ylabel near ticks]
      \addplot[red,mark=x] table {data1.csv};
      \label{pgf:swingvotes}
    \end{axis}
  \end{tikzpicture}
\end{document}

ref does not work as expected

2
  • Can you edit your question and elaborate on "something went wrong". Dec 2, 2011 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, 2011 at 12:50

1 Answer 1

8

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}}]
      \addplot[blue,mark=+] {x+5};
      \label{pgf:allsamples}
    \end{axis}
    \begin{axis}[axis y line*=right, scale only axis,
             ylabel={\fixcheck estimation 1 \ref{pgf:swingvotes}}, ylabel near ticks]
      \addplot[red,mark=x] {x};
      \label{pgf:swingvotes}
    \end{axis}
  \end{tikzpicture}
\end{document}

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .