这个想法源于以下问题:如何使用 xparser 获取可选参数的数量?
问题
我无法让 & 符号在 内充当分隔符(用于制表位)\DeclareDocumentCommand{\mygroup}
。我想也许我应该在这里看看:我如何使用 expl3 为活动角色赋予定义?。我很确定问题是什么,因为 & 符号在 中有效DeclareDocumentEnvironment{mytab}{O{}}
。
工作(不是真的,只是排版时不会崩溃): \doargs:n{#1} & \\ % interestingly, this ampersand does not crash typesetting.
不工作: {\clist_item:Nn \arglist {##1}\&} % added backslash to & for debugging
例子
\documentclass{article}
\usepackage{fontspec}
\usepackage{xparse,expl3}
\ExplSyntaxOn % based on https://tex.stackexchange.com/a/243442/13552
\DeclareDocumentEnvironment{mytab}{O{}}%
{\noindent\begin{tabular}{*{50}{l}}}
{\end{tabular}}
\DeclareDocumentCommand{\mygroup}{O{}m}{
\doargs:n{#1} & \\ % interestingly, this ampersand does not crash typesetting.
\multicolumn{\clist_count:N \arglist}{l}{\indent\parbox{\dimexpr\textwidth-\parindent\relax}{{\small\textit{#2}}}} \\%[\baselineskip]
}%
\clist_new:N\arglist
\cs_new_protected:Nn\doargs:n{{%
\clist_set:Nn\arglist{#1}%
\int_step_inline:nnnn {1}{1}{\clist_count:N \arglist}
{\clist_item:Nn \arglist {##1}\&} % added backslash to & for debugging
}}%
\ExplSyntaxOff
\begin{document}
\begin{mytab}
\mygroup[
Special 1,
ID 1,
A,
B]
{Some nice long description.}
\mygroup[
Special 2,
ID 2,
A,
B]
{Some nice long description.}
\mygroup[
Special 3,
ID 3,
A,
B]
{Some nice long description.}
\end{mytab}
\end{document}
答案1
您尝试在一个单元格中开始循环,在另一个单元格中结束循环。这行不通,因为对齐单元格会形成组。
你想使用\clist_use:Nn
\documentclass[draft]{article}
\usepackage{fontspec}
\usepackage{xparse,expl3}
\ExplSyntaxOn
\DeclareDocumentEnvironment{mytab}{O{}}
{\begin{tabular}{*{50}{l}}}
{\end{tabular}}
\clist_new:N \g_macmadness_mygroup_items_clist
\DeclareDocumentCommand{\mygroup}{O{}m}
{
\clist_gset:Nn \g_macmadness_mygroup_items_clist { #1 }
\clist_use:Nn \g_macmadness_mygroup_items_clist { & } \\
\multicolumn{ 50 } {@{}l@{}}
{
\hspace{\parindent}
\parbox{\dim_eval:n { \textwidth-\parindent } } {\small\itshape #2}
}
\\[\normalbaselineskip]
}
\ExplSyntaxOff
\begin{document}
\noindent
\begin{mytab}
\mygroup[
Special 1,
ID 1,
A,
B]
{Some nice long description.}
\mygroup[
Special 2,
ID 2,
A,
B]
{Some nice long description.}
\mygroup[
Special 3,
ID 3,
A,
B]
{Some nice long description.}
\end{mytab}
\end{document}
这将使用指定的分隔符扩展整个列表,因此 TeX&
在该过程中不会看到,只会在它结束时看到。