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

4
  • 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. Mar 7, 2022 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. Mar 7, 2022 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. Mar 8, 2022 at 18:34
  • Thanks @JohnKormylo! Mar 8, 2022 at 22:30

2 Answers 2

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

2
  • Thanks a lot. This was not clear to me. The commands sound very similar and I assumed they are synonyms. Mar 9, 2022 at 19:44
  • 1
    This can happen. But luckily we places like TeX.SX where you can be brought back on track ;) Mar 9, 2022 at 19:46
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}
1
  • Hey, I accepted the new answer. I will give +1 to some of your other answers as a thank you. Mar 9, 2022 at 18:26

You must log in to answer this question.

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