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 add legend symbols from pgfplots in my text. I am aware of the possibility to use the \label and \ref commands but my setup is complicated and I fear that making it work will more time consuming than setting the small drawings manually. I use externalization, my plots are generated with an intricate \pgfplotsinvokeforeach inside a template that is used several times in the document…

What I am looking for is a command like

\legendimageintext{mark=x, draw=blue}

that would draw a small blue dash with a cross on it, just as the legend post of a plot with similar styles would appear.

Note: if you want to put the legend in caption or in text, it is usually simpler to use \ref and \label as described in the pgfplots manual or there. My question related to special cases where the canonical approach is not usable.

share|improve this question
1  
tex.stackexchange.com/questions/54794/… should help with this? –  Jake Sep 7 '12 at 10:17
    
I guess it could be indeed adapted but I fail to see how to use \addlegendimage without the customlegend environment (I do not really understand how the latter works actually). –  Alfred M. Sep 7 '12 at 15:43

1 Answer 1

up vote 9 down vote accepted
+50

You can adapt the approach described by Christian Feuersänger in Using a pgfplots-style legend in a plain-old tikzpicture to provide a command \legendimageintext{mark=x, draw=blue}:

\documentclass{article}

\usepackage{pgfplots}

\newenvironment{customlegend}[1][]{%
    \begingroup
    \csname pgfplots@init@cleared@structures\endcsname
    \pgfplotsset{#1}%
}{%
    \csname pgfplots@createlegend\endcsname
    \endgroup
}%
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

\newcommand{\addlegendimageintext}[1]{%
    \tikz {
        \begin{customlegend}[
            legend entries={\empty},
            legend style={
                draw=none,
                inner sep=0pt,
                column sep=0pt,
                nodes={inner sep=0pt}}]
        \addlegendimage{#1}
        \end{customlegend}
    }%
}

\begin{document}
\addlegendimageintext{mark=x, draw=blue} A plot

\addlegendimageintext{fill=cyan, area legend} An area plot
\end{document}
share|improve this answer

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.