4

How to align the legend equations at the equal sign knowing that this code shows the following error

Missing } inserted. \end{axis} ?

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{amsmath}

\begin{document}


\begin{tikzpicture}
\begin{axis}[
legend cell align=left,
legend style={legend pos=north west}
]
  \addplot[domain=0:2] {x^2};
  \addplot[domain=0:4] {x};
  \legend{
    \begin{align*}
    $yy&=x^2$\\
    $y&=x$
    \end{align*}
  }
\end{axis}
\end{tikzpicture}
\end{document}

Edit

This code snippet

\legend{
$\begin{aligned}
yy&=x^2\\
y&=x
\end{aligned}$
}

results in

enter image description here

while this one

\legend{
$\begin{aligned}
yy&=x^2\\
y&=x\\ & \rule{0pt}{1ex}
\end{aligned}$
}

results in

enter image description here

Diaa
  • 9,599

2 Answers2

6

It's not really clear to me exactly what you want. Is this it? (I added colours so it's possible to distinguish the lines.)

It appears that even with two lines in the aligned, the node in the legend matrix has the height of a single line. I don't know why this happens. The workaround below is to first use \begin{aligned}[t] which places the first line of the aligned on the baseline. This makes the first line in the aligned align with the first plot in the legend.\

The next key thing to do is to add more legend entries, one for each additional plot. You don't want more text in the legend, but you want something that makes the nodes as high as they would be if there were text there. So add a \strut, this is a zero width box with some height (and depth).

Hence, if you have two plots, then use \legend{$\begin{aligned}[t]...\end{aligned}$, \strut }. If you have three plots, use \legend{$\begin{aligned}[t]...\end{aligned}$, \strut, \strut}. And so on.

enter image description here

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{amsmath}

\begin{document}


\begin{tikzpicture}
\begin{axis}[
legend cell align=left,
legend style={legend pos=north west}
]
  % added colours to plots
  \addplot[blue,domain=0:2] {x^2};
  \addplot[red,domain=0:4] {x};
  \legend{
    $\begin{aligned}[t]
    yy&=x^2\\[-2pt]
    y&=x
    \end{aligned}$,
    \strut % second legend entry
  }
\end{axis}
\end{tikzpicture}
\end{document}

A different approach

A different way altogether could be to do the alignment manually. Here I defined a macro \LHS (left hand side) with an optional and a mandatory argument. You use the optional argument to set the width of the LHS of the equations (default 1.5em), and the mandatory one holds the text/symbols that is the LHS. It makes a box of a fixed width, which makes the = aligned, and is used like this: \legend{$\LHS{yy}=x^2$,$\LHS{y}=x$}.

enter image description here

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{mathtools}
\newcommand{\LHS}[2][1.5em]{\hspace{#1}\mathllap{#2}}

\begin{document}


\begin{tikzpicture}
\begin{axis}[
legend cell align=left,
legend style={legend pos=north west}
]
  % added colours to plots
  \addplot[blue,domain=0:2] {x^2};
  \addplot[red,domain=0:4] {x};
  \legend{
    $\LHS{yy}=x^2$,
    $\LHS{y}=x$
  }
\end{axis}
\end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688
  • (+1) Fine trick, this \strut! Is this documented somewhere in the manual? – Bernard Jul 10 '18 at 20:21
  • @Bernard \struts are mentioned, in this case it's just an easy way of getting an empty legend entry with a full height node. – Torbjørn T. Jul 10 '18 at 20:23
  • I must I don't understand the way TikZ takes into account the environment height. It's so much simpler and direct to do with pstricks. – Bernard Jul 10 '18 at 20:30
  • @Bernard In this case I don't really know what happens either, normally the node in which the legend entry is placed would encompass the entire aligned regardless of [t], so you wouldn't have to do anything special. I don't know (and haven't at this point tried to find out) what pgfplots does exactly that changes it. – Torbjørn T. Jul 10 '18 at 20:40
  • That's exactly what I want to do. IMO, I think the first approach is more appealing in case different vertical alignments are needed. It doesn't really matter, but the entries images are not identical to each other in terms of the vertical alignment with their respective text, so I hope something like this would be fixed in the future. Your help is highly appreciated. – Diaa Jul 10 '18 at 21:29
  • @TorbjørnT. I found something strange when trying to add a third plot. This code results in this legend. So, how can I make the legend robust to the entries numbers without fixing it manually? – Diaa Jul 10 '18 at 21:40
  • @Diaa You need another legend entry, currently there are only two. Add ,\strut after the \strut that is currently there. – Torbjørn T. Jul 10 '18 at 21:42
  • @TorbjørnT. Many thanks, it works. However, if you come up with more robust solution to make the text automatically aligns with its image, it would be great to share it here since for multiple plots, the spacing of entries have to be manually adjusted as I did here in order to get this ouput. – Diaa Jul 10 '18 at 22:05
  • 1
    @Diaa The second option described above does that. I don't have any better ideas. – Torbjørn T. Jul 11 '18 at 07:30
  • @TorbjørnT. I am so grateful for your patience, but if you don't mind, could you give me some ideas for aligning both the equal signs and the text words for this MWE? – Diaa Jul 12 '18 at 06:18
  • 1
    @Diaa https://gist.github.com/TorbjornT/582ba77168ee6c31011fc6c9a6a5b9c6 – Torbjørn T. Jul 12 '18 at 06:24
  • @TorbjørnT. Many thanks! I can't be more grateful. – Diaa Jul 12 '18 at 06:27
3

Replace

\legend{
\begin{align*}
$yy&=x^2$\\
$y&=x$
\end{align*}
}

with

\legend{
$\begin{aligned}
yy&=x^2\\
y&=x
\end{aligned}$
}
Bernard
  • 271,350