19

I am looking for a way to create a legend in a regular tikzpicture. I found this

Using a pgfplots-style legend in a plain-old tikzpicture

but I don't know

  • how to put the legend at the wanted position and
  • how to add an entry with just a number instead of a symbol, like '1 number of ...'.

I don't need this. I just want to create easily a box where I have colored lines, colored circles/rectangles.. and colored numbers and the description next to.

Can anyone help me?

1
  • 1
    Do you want to use pgfplots tools or TikZ? Do you want to place the legend automatically ? It's relatively simple too create your personal legend with some nodes and where you want. Perhaps you can add the code to get your picture ... Commented Jul 5, 2012 at 10:24

1 Answer 1

26

Here is a solution based on Using a pgfplots-style legend in a plain-old tikzpicture. There are two examples: in the first one, the descriptions in the legend are centered, while in the second one, the descriptions are left aligned.

To define numbers in the legend, I added a key number in legend:

% definition to insert numbers
\pgfkeys{/pgfplots/number in legend/.style={%
        /pgfplots/legend image code/.code={%
            \node at (0.295,0){#1};
        },%
    },
}

Centered descriptions

The example:

\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc,shadings}

\usepackage{pgfplots}

% Code from Christian Feuersänger
% https://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture#54834

% 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}

%%--------------------------------

% definition to insert numbers
\pgfkeys{/pgfplots/number in legend/.style={%
        /pgfplots/legend image code/.code={%
            \node at (0.295,-0.0225){#1};
        },%
    },
}

\begin{document}
\begin{tikzpicture}
\draw (0,0)--(10,0);
\draw(0,-0.2)--(0,0.2)node[above,red]{\small{0}};
\draw(10,-0.2)--(10,0.2)node[above,blue]{\small{1}};
%------------------%
\foreach \m[count=\mi] in {0.909090909,...,9.090909090}
\draw(\m,-0.1)--(\m,0.1) node[anchor=north](a\mi){};
\draw[dotted]($(a2)+(0,0.1)$)--($(a2)+(0,0.4)$);
\draw[dotted]($(a3)+(0,0.1)$)--($(a3)+(0,0.4)$);
\draw[stealth-stealth,red,opacity=0.4]($(a3)+(0,0.4)$)--($(a2)+(0,0.4)$)node[anchor=south west,xshift=0.23cm,black,opacity=1]{\small{$R$}};
%------------------%
\foreach \x/\xtext in {0.7/$u_1$,1.2/$u_2$,4.5/$u_3$,5.2/$u_4$,5.6/$u_5$,7.5/$u_6$}
\shade[ball color=blue]  (\x,0 ) circle (0.1cm) node[below=0.1cm]{\small{\xtext}};
%------------------%
% inf user
\shade[ball color=green]  (0,0 ) circle (0.1cm) node[below=0.1cm]{\small{$u_{inf}$}};

\begin{customlegend}[
legend entries={ % <= in the following there are the entries
$R$: infection radius,
$u_i$: users,
$u_{inf}$: initial infected user, 
social space max dimension,
social space min dimension,
example of rectangle
},
legend style={at={(4.5,3.5)},font=\footnotesize}] % <= to define position and font legend
% the following are the "images" and numbers in the legend
    \addlegendimage{stealth-stealth,red,opacity=0.4}
    \addlegendimage{mark=ball,draw=white}
    \addlegendimage{mark=ball,ball color=green,draw=white}
    \addlegendimage{number in legend=1,blue}
    \addlegendimage{number in legend=0,red}
    \addlegendimage{area legend,orange,fill=orange!20}
\end{customlegend}
\end{tikzpicture}

\end{document}

which gives:

enter image description here

Left aligned descriptions

The code is:

\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc,shadings}

\usepackage{pgfplots}

% Code from Christian Feuersänger
% https://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture#54834

% 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}

%%--------------------------------

% definition to insert numbers
\pgfkeys{/pgfplots/number in legend/.style={%
        /pgfplots/legend image code/.code={%
            \node at (0.125,-0.0225){#1}; % <= changed x value
        },%
    },
}
\pgfplotsset{
every legend to name picture/.style={west}
}

\begin{document}
\begin{tikzpicture}
\draw (0,0)--(10,0);
\draw(0,-0.2)--(0,0.2)node[above,red]{\small{0}};
\draw(10,-0.2)--(10,0.2)node[above,blue]{\small{1}};
%------------------%
\foreach \m[count=\mi] in {0.909090909,...,9.090909090}
\draw(\m,-0.1)--(\m,0.1) node[anchor=north](a\mi){};
\draw[dotted]($(a2)+(0,0.1)$)--($(a2)+(0,0.4)$);
\draw[dotted]($(a3)+(0,0.1)$)--($(a3)+(0,0.4)$);
\draw[stealth-stealth,red,opacity=0.4]($(a3)+(0,0.4)$)--($(a2)+(0,0.4)$)node[anchor=south west,xshift=0.23cm,black,opacity=1]{\small{$R$}};
%------------------%
\foreach \x/\xtext in {0.7/$u_1$,1.2/$u_2$,4.5/$u_3$,5.2/$u_4$,5.6/$u_5$,7.5/$u_6$}
\shade[ball color=blue]  (\x,0 ) circle (0.1cm) node[below=0.1cm]{\small{\xtext}};
%------------------%
% inf user
\shade[ball color=green]  (0,0 ) circle (0.1cm) node[below=0.1cm]{\small{$u_{inf}$}};

\begin{customlegend}[legend cell align=left, %<= to align cells
legend entries={ % <= in the following there are the entries
$R$: infection radius,
$u_i$: users,
$u_{inf}$: initial infected user, 
social space max dimension,
social space min dimension,
example of rectangle
},
legend style={at={(4.5,3.5)},font=\footnotesize}] % <= to define position and font legend
% the following are the "images" and numbers in the legend
    \addlegendimage{stealth-stealth,red,opacity=0.4}
    \addlegendimage{mark=ball,draw=white}
    \addlegendimage{mark=ball,ball color=green,draw=white}
    \addlegendimage{number in legend=1,blue}
    \addlegendimage{number in legend=0,red}
    \addlegendimage{area legend,orange,fill=orange!20}
\end{customlegend}
\end{tikzpicture}

\end{document}

Result:

enter image description here

13
  • This is pretty much what I want. Thank you. Just one question: How do I change the color of the numbers? For example when I want a green 1 and a red 1 in the legend.
    – Jana
    Commented Jul 5, 2012 at 11:41
  • 1
    Very easily, you can say: \addlegendimage{number in legend=1,green} and \addlegendimage{number in legend=1,red}. Commented Jul 5, 2012 at 11:48
  • I updated my answer with this modification. Commented Jul 5, 2012 at 11:54
  • Awesome. Ok one more questions (sorry): Is it possible to align all the description texts?
    – Jana
    Commented Jul 5, 2012 at 11:54
  • Yes, it is possible. Do you want descriptions to be left aligned? Commented Jul 5, 2012 at 12:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .