pgfplots:使用带换行符的 `\addlegendentry` 时出现奇怪的问题

pgfplots:使用带换行符的 `\addlegendentry` 时出现奇怪的问题

错误/问题报告:看这里


  • 我想要在图例中添加一个多行自定义条目 ( \addlegendentry)。
  • 因此,我遵循在 pgfplot 中的图例中添加自定义条目
  • 我还希望图例左对齐。
  • 无论如何,我需要两个都(看似多余的)按照以下几行操作即可使其工作:

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

  • 如果我删除cells = {align = left},它,我会得到一个错误。
  • 如果我删除legend cell align = {left},,则对齐将居中。
  • 你能重现这个问题吗?这是我应该报告的错误吗?

\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}

错误信息

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

在此处输入图片描述

答案1

这不是一个错误,但是故意的行为。请参阅以下代码中的注释以获得一些解释。

\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}

该图显示了上述代码的结果

答案2

我可以重现该错误,可能值得报告错误。


手册中对此进行了legend cell align描述

选择left相当于legend style={cells={anchor=west}}

如果你这样做

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

删除时legend cell align=left,它似乎按预期工作。

\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}

相关内容