Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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}
share|improve this answer
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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