11

In some of my plots the curve description in the legend is too long, making the legend going over the plot, even if the legend is placed as far away as possible. Therefore I want to have multiple lines in the legend, but

\addlegendentry{a\\ b} 

results in an error. Is there another way to do that?

  • Could you provide a MWE, please ? – flav Nov 26 '15 at 10:22
  • @flav: Is it still needed? – arc_lupus Nov 26 '15 at 10:36
15

By default, TikZ nodes do not allow line breaks, but if you add the align=<left/right/center> key you can use \\. Hence, you can modify the legend style as below.

\documentclass[border=4pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.12
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
      legend style={cells={align=left}}
   ]  
    \addplot {rnd};
\addlegendentry{a\\b};
\end{axis}
\end{tikzpicture}
\end{document}
  • Yes, that solves the problem! – arc_lupus Nov 26 '15 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.