1

Bug/Issue Report: See here.


  • I want to have an additional multi-line custom entry in the legend (\addlegendentry).
  • Therefore, I follow Add custom entry into legend in pgfplot.
  • I also want the legend to be left-aligned.
  • Somehow, I need both (seemingly redundant) following lines to make it work:

legend style = {
    cells = {align = left}, % When commented out then error occurs
    },          
legend cell align = {left}, 

  • If I remove cells = {align = left}, the I get an error.
  • If I remove legend cell align = {left}, then the alignment is centered.
  • Can you recreate the problem? Is this a bug that I should report?

\documentclass[12pt,letterpaper,landscape]{article}

\usepackage{mathtools}

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

\begin{document}

\begin{tikzpicture} \begin{axis}[ legend style = { cells = {align = left}, % When commented out then error occurs },
legend cell align = {left},
] % Plot A \addplot{x^2 - x + 4}; \addlegendentry{$T_\text{R, I}$} % --- % https://tex.stackexchange.com/questions/204395/ \addlegendimage{empty legend} \addlegendentry{ Line 1\ Line 2 }
\end{axis} \end{tikzpicture}

\end{document}

Error Message

Missing } inserted.
<inserted text> 
                }
l.27  \end{axis}

enter image description here

  • 1
    "left is equivalent to legend style={cells={anchor=west}}" - from the manual. Does it work for you if you do legend style = {cells = {align = left, anchor=west}} and remove legend cell align=left. – Torbjørn T. Mar 07 '22 at 23:21
  • @TorbjørnT. Yes, that works. (1) Do you agree that the behavior is weird? (2) Feel free to add an answer (just the comment) and I will upvote. – Dr. Manuel Kuehner Mar 07 '22 at 23:30
  • 1
    align turns the node into a tabular; that is, the text inside the node is treated like a tabular. You don't see the effect since Line 1 and Line 2 are the same width. anchor=west aligns the entire node (tabular) inside the tikzpicture. – John Kormylo Mar 08 '22 at 18:34
  • Thanks @JohnKormylo! – Dr. Manuel Kuehner Mar 08 '22 at 22:30

2 Answers2

2

I can reproduce the error, might be worth a bug report.


The manual says in the description of legend cell align that

the choice left is equivalent to legend style={cells={anchor=west}}

And if you do

legend style = {cells = {align = left, anchor=west}},

while removing legend cell align=left, it seems to work as desired.

\documentclass[12pt,letterpaper,landscape]{article}

\usepackage{mathtools}

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

\begin{document}

\begin{tikzpicture} \begin{axis}[ legend style = {cells = {align = left, anchor=west}}, ] % Plot A \addplot{x^2 - x + 4}; \addlegendentry{$T_\text{R, I}$} % --- % https://tex.stackexchange.com/questions/204395/ \addlegendimage{empty legend} \addlegendentry{ Line 1\ Line 245 }
% \addlegendimage{empty legend} % \addlegendentry{a}

\end{axis}

\end{tikzpicture}

\end{document}

Torbjørn T.
  • 206,688
1

This is not a bug, but intended behavior. See the comments in the following code for some explanations.

\documentclass[border=5pt]{standalone}
\usepackage{mathtools}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % this key manages how *different* legend entries are aligned to each other
        legend cell align=right,
        % this key manages how *multi-line* text in a *single* legend entry is aligned.
        % You receive an error because it is *required* to set the `align` option
        % when you have multi-line text. ...
        legend style={cells={align=left}}, % When commented out then error occurs
    ]
        \addplot{x^2 - x + 4};
        \addlegendentry{$T_\text{R, I}$}
        \addlegendimage{empty legend}
        \addlegendentry{
            Line 1 \\
            Line number two
        }
        \addlegendimage{empty legend}
        \addlegendentry{just some long text}
    \end{axis}
% ... You'll get the same error if you comment the `align` option here,
% i.e. this is standard (intended) TikZ behavior.
\node at (1,1) [
    align=center,
] {some \\ text};

\end{tikzpicture} \end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535