在 LaTeX 中我声明\NewDocumentCommand
像这样:
\NewDocumentCommand{\DeclareListOfValues}{ m m }
{
\clist_gclear_new:c {g_list_of_values_#1_clist}
\clist_gset:cn {g_list_of_values_#1_clist} {#2}
}
下面是我如何使用它来创建嵌套值列表的示例:
\DeclareListOfValues{MyList}
{
{14-1-11,73.6,26.4,0,0,0},
{15-1-4,72.7,27.3,0,0,0},
{15-1-6,67.7, 32.3,0,0,0},
{16-1-10,75.6, 24.4,0,0,0},
{1562-3-6,68.7,31.3,0,0,0},
{1562-3-16,71.6,28.4,0,0,0},
{1563-3-8,85.9,14.1,0,0,0},
{1563-3-13,71.8,28.2,0,0,0},
{1564-2-7,58.1,41.9,0,0,0},
{1564-3-14,56.2,43.8,0,0,0}
}
根据嵌套列表中的这些值,我自动创建了直方图,并且运行良好。
我现在需要完成的是计算列表内的子列表以及子列表中的元素,因为这些数字可能不同。
有人可以帮忙吗?
麦克风
答案1
这提供了一个完全可扩展的命令,用于计数外部列表的项目或其子列表中的项目。
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\DeclareListOfValues}{ m m }
{
\clist_gclear_new:c {g_list_of_values_#1_clist}
\clist_gset:cn {g_list_of_values_#1_clist} {#2}
}
\NewExpandableDocumentCommand{\CountList}{om}
{
\IfNoValueTF { #1 }
{% no optional argument, count the outer list
\clist_count:c {g_list_of_values_#2_clist}
}
{% optional argument, count the i-th item in the outer list
\clist_count:e { \clist_item:cn {g_list_of_values_#2_clist} { #1 } }
}
}
\cs_generate_variant:Nn \clist_count:n { e }
\ExplSyntaxOff
\DeclareListOfValues{MyList}{
{14-1-11,73.6,26.4,0,0,0},
{15-1-4,72.7,27.3,0,0,0},
{15-1-6,67.7, 32.3,0,0,0},
{16-1-10,75.6, 24.4,0,0,0},
{1562-3-6,68.7,31.3,0,0,0},
{1562-3-16,71.6,28.4,0,0,0},
{1563-3-8,85.9,14.1,0,0,0},
{1563-3-13,71.8,28.2,0,0,0},
{1564-2-7,58.1,41.9,0,0,0},
{1564-3-14,56.2,43.8,0,0,0}
}
\begin{document}
\CountList{MyList} (should be 10)
\CountList[1]{MyList} (should be 6)
\end{document}
如果需要,可以添加检查来判断可选参数是否超出了可接受的范围。