在 Tikz 中,Row sep 表示单行图例

在 Tikz 中,Row sep 表示单行图例

我正在使用来自的仅图例解决方案在普通的 tikzpicture 中使用 pgfplots 样式的图例

如何获取单行样式?row sep = 5pt例如,我尝试为所有行设置样式,但第二行和第三行之间的值不同。我试过

legend style={row sep = 5pt,
    /tikz/row 2/.style={row sep = 10pt}
}

这可以很好地编译并5pt为所有行添加分隔,但不会改变第二行之后的分隔。

谢谢!

编辑

这是 MWE。我简单地复制了链接中的答案并添加了上面的代码:

\documentclass{article}

\usepackage{pgfplots}

% argument #1: any options
\newenvironment{customlegend}[1][]{%
    \begingroup
    % inits/clears the lists (which might be populated from previous
    % axes):
    \csname pgfplots@init@cleared@structures\endcsname
    \pgfplotsset{#1}%
}{%
    % draws the legend:
    \csname pgfplots@createlegend\endcsname
    \endgroup
}%

% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

\begin{document}

\thispagestyle{empty}

\begin{tikzpicture}
    \begin{customlegend}[legend style={row sep = 5pt,
        /tikz/row 2/.style={row sep = 10pt}
    },
    legend entries={$a$,$e^x$,C,$d$}]
    \addlegendimage{red,fill=black!50!red,area legend}
    \addlegendimage{red,fill=black!50!red,sharp plot}
    \addlegendimage{red,fill=black!50!red,mark=*,sharp plot}
    \addlegendimage{red,fill=black!50!red,ybar,ybar legend}
    \end{customlegend}
\end{tikzpicture}

\end{document}

答案1

row sep作为选项传递给pgfplots用于创建图例的 TikZ 矩阵。我认为这很好用。然而,这是一个适用于而不是细胞,而row <number>影响的风格细胞。因此,row sep在样式中设置row 2不会产生任何效果,因为个人细胞完全不受此选项的影响。

你可以通过移动所有细胞yshift例如,连续使用。因为yshift适用于细胞row 2,在样式选项中设置工作。

例如(为说明目的有所夸张):

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{pgfplots}
% code from Christian Feuersänger's answer at http://tex.stackexchange.com/a/54834/
% argument #1: any options
\newenvironment{customlegend}[1][]{%
    \begingroup
    % inits/clears the lists (which might be populated from previous
    % axes):
    \csname pgfplots@init@cleared@structures\endcsname
    \pgfplotsset{#1}%
}{%
    % draws the legend:
    \csname pgfplots@createlegend\endcsname
    \endgroup
}%
% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}
\begin{document}
\begin{tikzpicture}
  \begin{customlegend}
    [
      legend style={%
        row sep = 5pt,
        /tikz/row 2/.style={yshift = 15pt},
      },
      legend entries={$a$,$e^x$,C,$d$},
    ]
    \addlegendimage{red,fill=black!50!red,area legend}
    \addlegendimage{red,fill=black!50!red,sharp plot}
    \addlegendimage{red,fill=black!50!red,mark=*,sharp plot}
    \addlegendimage{red,fill=black!50!red,ybar,ybar legend}
    \end{customlegend}
\end{tikzpicture}
\end{document}

一行中移动单元格

答案2

据我所知,更改row sep单行对普通 TikZ 不起作用matrix。在这种普通矩阵中,您必须使用\\行末尾的可选参数来增加或减少row sep

为了放大,row sep您可以在图例中插入一条空线:

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\newenvironment{customlegend}[1][]{%
    \begingroup
    \csname pgfplots@init@cleared@structures\endcsname
    \pgfplotsset{#1}%
}{%
    \csname pgfplots@createlegend\endcsname
    \endgroup
}%
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

\begin{document}
\begin{tikzpicture}
    \begin{customlegend}[legend style={row sep = 5pt,
        %nodes=draw
    },
    legend entries={$a$,$e^x$,{[inner sep=0pt]},C,$d$}]
    \addlegendimage{red,fill=black!50!red,area legend}
    \addlegendimage{red,fill=black!50!red,sharp plot}
    \addlegendimage{empty legend}
    \addlegendimage{red,fill=black!50!red,mark=*,sharp plot}
    \addlegendimage{red,fill=black!50!red,ybar,ybar legend}
    \end{customlegend}
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者您可以在图例条目的单元格中插入一个带有不可见行的不可见节点:

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\newenvironment{customlegend}[1][]{%
    \begingroup
    \csname pgfplots@init@cleared@structures\endcsname
    \pgfplotsset{#1}%
}{%
    \csname pgfplots@createlegend\endcsname
    \endgroup
}%
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

\begin{document}
\begin{tikzpicture}
    \begin{customlegend}[legend style={row sep = 5pt,
        row 2 column 2/.style={
            execute at end cell={
                \node[anchor=north,yshift=-\dp\strutbox,inner sep=0pt,draw=none,fill=none]{\rule{0pt}{5pt}};
        }},
        %nodes=draw
    },
    legend entries={$a$,$e^x$,C,$d$}]
    \addlegendimage{red,fill=black!50!red,area legend}
    \addlegendimage{red,fill=black!50!red,sharp plot}
    \addlegendimage{red,fill=black!50!red,mark=*,sharp plot}
    \addlegendimage{red,fill=black!50!red,ybar,ybar legend}
    \end{customlegend}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容