I'm trying to create a legend for use in the caption of a plot (to match a plot created in matlab, so sadly, I can't just link to the figure.). I know that tikz can create values for use in a legend, as in the MWE included below. But what I really want is to also have a marker symbol: a dashed line with a circle -o-

I think maybe I would want to add a node (e.g., draw a short line, draw a symbol, draw another short line). So then this would give me something like

\newcommand{\hwplotA}{\raisebox{2pt}{\tikz{\draw[black,dashed,mark=o,line width=0.9pt](0,0) -- (5mm,0);\node at (2.5mm,0) {$o$}}}}

although this doesn't compile. The solution listed by the OP at the link below doesn't work for me (putting a zero-sized tikz plot) because it seems to not play nicely with other packages I'm using.

Any ideas?

MWE to create a legend line from How to draw legend lines in caption/text without including original plot's TikZ picture?


\newcommand{\hwplotA}{\raisebox{2pt}{\tikz{\draw[red,solid,line width=0.9pt](0,0) -- (5mm,0);}}}
\newcommand{\hwplotB}{\raisebox{2pt}{\tikz{\draw[black,dashed,line width=1.2pt](0,0) -- (5mm,0);}}}

This is how I reference to {\hwplotA} and {\hwplotB}. 


To illustrate Christian Feuersänger's solution from Using a pgfplots-style legend in a plain-old tikzpicture :

Do you really want the legend in the caption? Between the picture and the caption should do.


% argument #1: any options
    % inits/clears the lists (which might be populated from previous axes):
    % draws the legend:


\begin{customlegend}[legend columns=-1]
\addlegendentry{raw data}
\addlegendentry{cooked data}



  • Hmm, that's a nice workaround. Ideally, I would like to find a solution that also works in the caption. – user2441511 Jun 6 '15 at 0:24
  • You can put it in the caption, but it will also show up in the list of figures. – John Kormylo Jun 6 '15 at 12:36

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.