0

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?

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\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}. 

\end{document} 
2

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.

\documentclass{article}
\usepackage{pgfplots}

\makeatletter
% argument #1: any options
\newenvironment{customlegend}[1][]{%
    \begingroup
    \let\addlegendimage=\pgfplots@addlegendimage
    \let\addlegendentry=\pgfplots@addlegendentry
    % inits/clears the lists (which might be populated from previous axes):
    \pgfplots@init@cleared@structures
    \pgfplotsset{#1}%
}{%
    % draws the legend:
    \pgfplots@createlegend
    \endgroup
}%
\makeatother

\begin{document}

\begin{tikzpicture}
\begin{customlegend}[legend columns=-1]
\addlegendimage{red,mark=*}
\addlegendentry{raw data}
\addlegendimage{blue,mark=triangle*}
\addlegendentry{cooked data}
\end{customlegend}
\end{tikzpicture}

\end{document}

legend

  • 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.