4

I would like to build a legend entry that refers to two simultaneous plots, because it is more intuitive to do so. I use \label and \ref to refer to the plot symbols, but they are shifted in the legend, while they are not shifted in the text. What am I doing wrong?

\documentclass[10pt]{article}
\usepackage{color}
\usepackage{amssymb}
\usepackage{amsmath} 
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{plotmarks}

\begin{document}

\begin{tikzpicture}
 \begin{axis}
  \addlegendimage{empty legend}
  \addlegendentry{\ref{pgf:x} $\le$ \ref{pgf:x2}}
  \addplot+ function {x};
   \label{pgf:x}
  \addplot+ function{x^2};
   \label{pgf:x2}
 \end{axis}
\end{tikzpicture}

The legend is placed \ref{pgf:x2} correctly here

\end{document}

enter image description here

4

Not sure it is the best way but it appears to work if you reset the tikzpicture locally:

enter image description here

\documentclass[10pt]{article}
\usepackage{color}
\usepackage{amssymb}
\usepackage{amsmath} 
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{plotmarks}

\begin{document}

\begin{tikzpicture}
 \begin{axis}
  \addlegendimage{empty legend}
  \addlegendentry{\begin{tikzpicture}\ref{pgf:x}\end{tikzpicture} $\le$ \begin{tikzpicture}\ref{pgf:x2}\end{tikzpicture}}
  \addplot+ function {x};
   \label{pgf:x}
  \addplot+ function{x^2};
   \label{pgf:x2}
 \end{axis}
\end{tikzpicture}

The legend is placed \ref{pgf:x2} correctly here

\end{document}
3
  • 1
    Using the shorthand \tikz{...} for \begin{tikzpicture} ... \end{tikzpicture} might be a good idea here (so \tikz{\ref{pgf:x}}. – Jake Sep 17 '12 at 11:47
  • a bit ugly for sure, but it answers my question, thanks! – yannick Sep 18 '12 at 12:49
  • 1
    This answer is correct ... and the uglyness is due to a bug in TikZ which is used by pgfplots. I introduced that bug, sorry about it. It should have detected that it has to generate a picture environment on its own. – Christian Feuersänger Sep 18 '12 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.