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 am using a plain-old tikzpicture to produce a pie-chart-like plot. Now, all other plots in my paper are handled using pgfplots (which doesn't do pie charts) and have their legend typeset by pgfplots.

It would thus be great if it were possible to create, re-using as many keys like /pgfplots/area legend as possible, to create a pgfplots-style legend in TiKZ. Has anyone done anything like this before?

share|improve this question
1  
Would you mind uploading an example of how you create your pie charts? –  Jake May 7 '12 at 15:28
    
I don't use any special package for that; just plain-old tikzpicture \filldraw and arc paths. (No package could easily do what I wanted to out of the box: a pie chart within a ring chart; sort of like a pie with a crust.) –  Andreas May 8 '12 at 7:20
add comment

2 Answers

up vote 24 down vote accepted

Pgfplots has a couple of helper methods which do the job - and these methods are mostly independent of any axis. The only required things are the text labels and the styles for the individual images.

Here is a short draft which might do what you want:

\documentclass{article}

\usepackage{pgfplots}

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

% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

\begin{document}

\thispagestyle{empty}

\begin{tikzpicture}
    \begin{customlegend}[legend entries={$a$,$e^x$,C,$d$}]
    \addlegendimage{red,fill=black!50!red,area legend}
    \addlegendimage{red,fill=black!50!red,sharp plot}
    \addlegendimage{red,fill=black!50!red,mark=*,sharp plot}
    \addlegendimage{red,fill=black!50!red,ybar,ybar legend}
    \end{customlegend}
\end{tikzpicture}

\end{document}

The different \addlegendimage styles are a little bit ... inconsistent, but I wanted to experiment if they work. I guess that mesh legend might produce problems, so stick to the simpler ones...

enter image description here

share|improve this answer
    
Works like a charm. –  Andreas May 8 '12 at 8:17
    
This is really nice. I just don't know how I position the legend in the tikzpicture at the right spot. Tried to put it into a node, but got an error. Is there a general way to position something? –  Jana Jul 4 '12 at 13:52
    
@Jana it is placed using the legend keys, i.e. add legend style={at={(0.5,0.5)},anchor=center} to the option list. Perhaps you can also omit the outer legend style and write at=... immediately. –  Christian Feuersänger Jul 7 '12 at 20:44
    
If I'm not mistaken, this positioning using legend style={at={(0.5,0.5)},anchor=center} is to position the legend as if it was inside an axis environment. How should be done in order to position the legend using global coordinates of the tikz? Lets say that the legend should be a node positioned at (100,100) –  Sik Jun 16 at 6:28
    
at={(100,100)} should work. –  Christian Feuersänger Jun 16 at 18:07
add comment

And here is how to put a non-pgfplots style legend into pgfplots.

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

\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=10,ymin=-5,ymax=5,name=border]
\end{axis}

\node[below left=1mm] at (border.north east) {\fbox{
\begin{tabular}{@{}r@{ }l@{}}
 \raisebox{2pt}{\tikz{\draw[black] (0,0) -- (5mm,0);}}&black\\
 \raisebox{2pt}{\tikz{\draw[red] (0,0) -- (5mm,0);}}&red\\
 \raisebox{2pt}{\tikz{\draw[green] (0,0) -- (5mm,0);}}&green\\
 \raisebox{2pt}{\tikz{\draw[blue] (0,0) -- (5mm,0);}}&blue\\
\end{tabular}}};

\end{tikzpicture}
\end{document}

plot with legend

share|improve this answer
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.