我正在尝试调整给出的解决方案这里(这也相关),其中可以通过循环添加图例条目。例如,如果我们有一组 N 条曲线,这些曲线取决于多个参数,那么最好在 groupplot 环境中比较每个集合。以下改编
\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}
\begin{document}
\newcommand{\getLab}[1]{plot_#1}
\newcommand{\getLeg}[1]{curve #1}
\begin{tikzpicture}
\begin{groupplot}[cycle list name=color list,
group style={group name=myplot, group size= 2 by 2}]
\nextgroupplot[ylabel=throughput,
every axis y label/.append style={at=(ticklabel cs:0)}] % Common y label
%\addlegendimage{empty legend}
%\addlegendentryexpanded{param 1} % very convenient
\foreach \n in {1,...,3} {
\addplot {x^(\n)};
\label{\getLab{\n}}
}
\nextgroupplot
\foreach \n in {1,...,3} {
\addplot {x^(\n + 1)};
}
\nextgroupplot
\foreach \n in {1,...,3} {
\addplot {x^(\n + 2)};
}
\nextgroupplot
\foreach \n in {1,...,3} {
\addplot {x^(\n + 3)};
}
\end{groupplot}
\path (myplot c1r1.north west|-current bounding box.north)--
coordinate(legendpos)
(myplot c2r1.north east|-current bounding box.north);
\matrix[
matrix of nodes,
anchor=south,
draw,
inner sep=0.2em,
draw
] at ([yshift=1ex] legendpos) {
% Problem starts:
%\foreach \m in {1,...,3} { % ?? this just looks silly
% \ref{\getLab{\m}} & \getLeg{\m} &
%}
%
% This compiles surprisingly
%\foreach \m in {1,...,3} { \ref{\getLab{\m}} } &
%\foreach \m in {1,...,3} { \ref{\getLeg{\m}} } &
% Working
\ref{plot_2} & curve 2 &
\ref{plot_2} & curve 2 &
\ref{plot_3} & curve 3
\\};
\end{tikzpicture}
\end{document}
给出:
这正是我最终想要的,但我想以类似于生成标签的方式“功能性地”生成最后使用的矩阵结构。这可能吗?
答案1
问题是\foreach
在循环代码周围创建了一个 TeX 组,从而阻止了 Ti钾Z 矩阵将&
s 视为列分隔符。为了解决这个问题,您可以使用expl3
诸如 之类的函数来生成代码\int_step_inline:nnnn
,该函数不会在用户代码周围创建组。
注:Ti钾&
Z 矩阵需要类别代码为 13(活动)的列分隔符。这是不是的标准类别代码为&
,即 4(对齐标签)。这就是为什么我的定义\myAddLegendEntries
使用\pgfmatrixnextcell
而不是(在定义的&
地方,有其标准类别代码 4,当插入 Ti 中时,它不会按预期工作\myAddLegendEntries
&
钾Z 矩阵)。
条目之间的分隔符是可选参数\myAddLegendEntries
。它默认为\pgfmatrixnextcell
,但您可能希望更改它以修改图例的外观。例如,您可以在之前或之后添加内容\pgfmatrixnextcell
,或者您可以使用它来获得多行图例:
\myAddLegendEntries[\\]{1}{1}{3} % initial value, step, final value
{\ref{\getLab{#1}} & \getLeg{#1}}
带有单行图例的完整代码,如 MWE 中所示:
\documentclass[tikz, margin=2mm]{standalone}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.16}
\ExplSyntaxOn
\seq_new:N \l__algae_legend_entries_seq
% Arguments: separator, initial, step, final
\NewDocumentCommand \myAddLegendEntries { O{\pgfmatrixnextcell} m m m m }
{
\seq_clear:N \l__algae_legend_entries_seq
\int_step_inline:nnnn {#2} {#3} {#4}
{ \seq_put_right:Nn \l__algae_legend_entries_seq {#5} }
\seq_use:Nn \l__algae_legend_entries_seq {#1}
}
\ExplSyntaxOff
\newcommand{\getLab}[1]{plot_#1}
\newcommand{\getLeg}[1]{curve #1}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[cycle list name=color list,
group style={group name=myplot, group size= 2 by 2}]
\nextgroupplot[
% Common y label
ylabel=throughput,
every axis y label/.append style={at=(ticklabel cs:0)}]
\foreach \n in {1,...,3} {
\addplot {x^(\n)};
\label{\getLab{\n}}
}
\nextgroupplot
\foreach \n in {1,...,3} {
\addplot {x^(\n + 1)};
}
\nextgroupplot
\foreach \n in {1,...,3} {
\addplot {x^(\n + 2)};
}
\nextgroupplot
\foreach \n in {1,...,3} {
\addplot {x^(\n + 3)};
}
\end{groupplot}
\path (myplot c1r1.north west|-current bounding box.north)--
coordinate(legendpos)
(myplot c2r1.north east|-current bounding box.north);
\matrix[matrix of nodes, anchor=south, draw, inner sep=0.2em]
at ([yshift=1ex] legendpos)
{
\myAddLegendEntries{1}{1}{3} % initial value, step, final value
{\ref{\getLab{#1}} & \getLeg{#1}}
\\
};
\end{tikzpicture}
\end{document}