9

In a figure caption or in the main text I want to reuse the style of the plotmarks or the combination of line and symbol as in the figure legend. As I did not find a command to reuse the legend image I tried to draw the plotmark with the same properties but it seems that the syntax must be changed?

An illustration of what I have in mind: enter image description here

The corresponding code: \documentclass{article}

    \usepackage{tikz,pgfplots}
    \pgfplotsset{compat=1.7} 
    \usetikzlibrary{plotmarks} 

    \begin{document}

    \begin{tikzpicture}
    \begin{axis}[legend style={at={(0.03,0.97)},anchor=north west},] 

    \addplot+[blue,
                        mark options={solid,fill=.!30!white},
                        mark=*,] 
                        coordinates {(1,1) (2,3) (3,5)};
                        \addlegendentry{$H \parallel c$};
    \end{axis}
    \end{tikzpicture} 

    Reusing plot mark style in figure caption does not work:
    \tikz[baseline=-0.5ex]\node[blue,mark options={solid,fill=.!30!white}] at (0,0) {\pgfuseplotmark{*}};
    \end{document}

Is there any way to easily redraw the legend image or the plotmark outside of the pgfplot axis?

Jake
  • 232,450
Alexander
  • 9,163

1 Answers1

8

That's what the \label and \ref functionality of PGFPlots is great for: You add \label{<label name>} after the plot you want to show in your legend, and use \ref{<label name>} at the place where you want to show the legend image:

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.7} 
\usetikzlibrary{plotmarks} 

\begin{document}

\begin{tikzpicture}
\begin{axis}[legend style={at={(0.03,0.97)},anchor=north west},] 

\addplot+[blue,
                    mark options={solid,fill=.!30!white},
                    mark=*,] 
                    coordinates {(1,1) (2,3) (3,5)};
                    \addlegendentry{$H \parallel c$};
                    \label{firstplot}
\end{axis}
\end{tikzpicture} 

Reusing plot mark style in figure caption does not work:
\ref{firstplot}
\end{document}
Jake
  • 232,450
  • Oh, that is very nice. I overlooked this part of the documentation. At first it did not work with externalization but after some fiddling even that runs smoothly. Thank you very much. – Alexander Feb 10 '13 at 18:09
  • @Alexander: Could you please provide your fixes, that \label and \ref works with externalization? – Henri Menke Feb 10 '13 at 19:52
  • 1
    @HenriMenke: Take a look at the comments and the answer to pgfplots: externalization and legend referencing. There are two ways of getting it to work: Either by disabling the externalization locally for the \ref command, or by using the list-and-make externalization mode. – Jake Feb 10 '13 at 22:01
  • 1
    @HenriMenke: I locally disabled the externalization, similar to the comments Jake linked to. This works without further changes to my setup and does not cause any negative side-effects. – Alexander Feb 11 '13 at 01:25
  • Thank you very much! I've been struggling with this some times and then chose to remove all the label-ref-constructs. Your answer is great! – Henri Menke Feb 12 '13 at 22:52