16

The following code with the comma in the legend entry fails to compile.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[ 
  legend entries =  {$a, b$} , %<- error!
  title = {$a, b$},
  xlabel = {$a, b$}
  ]

  \addplot {x};

  \end{axis}
\end{tikzpicture}

\end{document}

I have added additional curly brackets around the expression as needed by tikz (e.g. here Comma in the formula in tikz).

Weird enough, the error only seems to appear, when using a comma in the legend, as labels and titles work fine.

Is this a bug in the parser or am I missing something?

2 Answers 2

15

Seems that the key handler isn't too careful with dropping outer brace groups, this makes it work:

legend entries =  {}{$a, b$} , %<- error!
2
  • Awesome! How did you solve this so quickly? One minor addition, to have multiple legend entries we now have to write legend entries = {{} {$a, b$},{} {$c, d$}}.
    – Jost
    Commented Aug 22, 2013 at 18:28
  • 2
    @Jost I've used TeX before:-) Commented Aug 22, 2013 at 18:31
11

A different solution is to delimit the list by \\. To quote the PGFPlots manual v1.9 4.9.4 "Legends":

It is also possible to delimit the list by \\. In this case, the last element must be terminated by \\ as well.

Example:

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[ 
  legend entries =  {$a, b$\\$c, d$\\},
  title = {$a, b$},
  xlabel = {$a, b$}
  ]

  \addplot {x};
  \addplot {x - 1};

  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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