3

I'm writing a document that requires very few actual plots and I'm happy with the aesthetic of tikz. However, I'm having trouble finding a way to make a simple legend on tikz. I know there's a way with the data visualization library, but it seems far too convoluted for simple graphs. Some people suggested using matrices, which turned out to be pretty much what I wanted, like this:

\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[->] (-1,0) -- (8,0)  node[right]{$x$};
\draw[->] (0,-2) -- (0,2)  node[above]{$y$};
\draw[green,samples=100,domain=-1:8] plot(\x,{sin(deg(\x))});
\draw[red,samples=100,domain=-1:8] plot(\x,{cos(deg(\x))});
\draw[blue] (0,0)--(pi/2,1)--(3*pi/2,-1)--(5*pi/2,1);
\matrix [draw, above left] at (8,-2) {
  \node[green,font=\tiny] {$\sin x$}; \\
  \node[red,font=\tiny] {$\cos x$}; \\
  \node[blue,font=\tiny] {Lines}; \\
};
\end{tikzpicture}
\end{document}

However, I want the legend to show the draw style of the graphs. Like what we can see on the tikz manual:

Can I do that with matrices? I would also want the legend to be aligned to the left, right now it's to the right, but no idea how to change it.

2

2 Answers 2

2

I also prefer pfgplots but for completeness here is a method using pics.

\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[pics/legend entry/.style={code={%   
        \draw[pic actions] 
        (-0.5,0.25) sin (-0.25,0.4) cos (0,0.25) sin (0.25,0.1) cos (0.5,0.25);}}]
\draw[->] (-1,0) -- (8,0)  node[right]{$x$};
\draw[->] (0,-2) -- (0,2)  node[above]{$y$};
\draw[green!70!black,samples=100,domain=-1:8] plot(\x,{sin(deg(\x))});
\draw[red,samples=100,domain=-1:8] plot(\x,{cos(deg(\x))});
\draw[blue] (0,0)--(pi/2,1)--(3*pi/2,-1)--(5*pi/2,1);
\matrix [draw, above left] at (8,-2) {
 \pic[green!70!black]{legend entry}; &  \node[green!70!black,font=\tiny] {$\sin x$}; \\
 \pic[red]{legend entry}; &  \node[red,font=\tiny] {$\cos x$}; \\
 \pic[blue]{legend entry}; &  \node[blue,font=\tiny] {Lines}; \\
};
\end{tikzpicture}
\end{document}

enter image description here

To align the text nodes you can use these tricks.

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{eqparbox}
\begin{document}
\newbox\eqnodebox
\tikzset{lequal size/.style={execute at begin
    node={\setbox\eqnodebox=\hbox\bgroup},
    execute at end node={\egroup\eqmakebox[#1][l]{\copy\eqnodebox}}},
    lequal size/.default=A,}
\begin{tikzpicture}[pics/legend entry/.style={code={%   
        \draw[pic actions] 
        (-0.5,0.25) sin (-0.25,0.4) cos (0,0.25) sin (0.25,0.1) cos (0.5,0.25);}}]
\draw[->] (-1,0) -- (8,0)  node[right]{$x$};
\draw[->] (0,-2) -- (0,2)  node[above]{$y$};
\draw[green!70!black,samples=100,domain=-1:8] plot(\x,{sin(deg(\x))});
\draw[red,samples=100,domain=-1:8] plot(\x,{cos(deg(\x))});
\draw[blue] (0,0)--(pi/2,1)--(3*pi/2,-1)--(5*pi/2,1);
\matrix [draw, above left] at (8,-2) {
 \pic[green!70!black]{legend entry}; &  \node[lequal size,green!70!black,font=\tiny] {$\sin x$}; \\
 \pic[red]{legend entry}; &  \node[lequal size,red,font=\tiny] {$\cos x$}; \\
 \pic[blue]{legend entry}; &  \node[lequal size,blue,font=\tiny] {Lines}; \\
};
\end{tikzpicture}
\end{document}

enter image description here

6
  • Thank you. You guys are right, I should probably learn to use pgfplots, but this will do the trick for now. Nov 6, 2020 at 17:45
  • This is still aligned to the right though. Is there a way to change that? Nov 6, 2020 at 18:02
  • @Nickesponja What is aligned to the right?
    – user227987
    Nov 6, 2020 at 18:03
  • I mean that the text on the legend entries, "sin x", "cos x", "Lines" is aligned to the right. I'd prefer it aligned to the left, that is, each line of text starting just after the corresponding pic Nov 6, 2020 at 18:06
  • @Nickesponja They are centered, I added a way to align them left. The alignment is controlled by l in \eqmakebox[#1][l]{.
    – user227987
    Nov 6, 2020 at 18:16
2

To get a consistent look, I suggest to never use pure TikZ for plots, but PGFPlot that is build on top of TikZ.

\documentclass[11pt]{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
  samples=100,
  domain=-1:8,
  xmin=-1, xmax=8,
  ymin=-2, ymax=2,
  axis lines=middle,
  ticks=none,
  xlabel={$x$},
  ylabel={$y$},
  legend pos=south east,
  width=\textwidth,
  height=0.5*\textwidth]
\addplot[green] {sin(deg(\x))};
\addplot[red] {cos(deg(\x))};
\addplot[blue] coordinates{(0,0) (pi/2,1) (3*pi/2,-1) (5*pi/2,1)};
\addlegendentry{$sin(x)$}
\addlegendentry{$cos(x)$}
\addlegendentry{Lines}
\end{axis};
\end{tikzpicture}
\end{document}

Graph with legend

You must log in to answer this question.

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