重复宏的参数以进行 cleveref 引用(forcsvlist?)

重复宏的参数以进行 cleveref 引用(forcsvlist?)

这是一个后续问题如何使用自定义项目命令自定义枚举列表并在第二级恢复编号?。现在我想知道如何引用我的工作包列表的条目。

关键部分似乎是重复将\mylist:的每个参数传递给。只需要引用工作包。\crcWP\cref{...}\crcWP{<hook(s)>}

我不确定如何使用例如来实现这一点forcsvlist

\documentclass{article}
\usepackage{enumitem}
\usepackage{etoolbox}
\usepackage[compress]{cleveref}

% ==== List for Workpackages
%
\newlist{workpackages}{enumerate}{3}
\setlist[workpackages,1]{
  label={WP\,\arabic*:},
  ref = {\arabic*},
  resume
}

\setlist[workpackages,2]{
  ref={\theworkpackagesi\alph*},
  label={WP\,\theworkpackagesi\alph*:}
}

\crefname{workpackagesi}{\bfseries WP}{\bfseries WPs}
\creflabelformat{workpackagesi}{#2#1#3}
\crefname{workpackagesii}{\bfseries WP}{\bfseries WPs}
\creflabelformat{workpackagesii}{#2#1#3}



\providecommand{\mylist}{}

\newcommand{\crcWP}[1]{\cref{\mylist:#1}}
%                           ^repeatThis^
%\forcsvlist{\crcWP}{\crcWP}

\newcommand{\crcItem}[1]{\item\label{\mylist:#1}}

\usepackage{cleveref}
\begin{document}
\def\mylist{abc}
\begin{workpackages}
  \crcItem{A} some text
  \crcItem{B} some text
  \begin{workpackages}
  \crcItem{B1} some text
  \crcItem{B2} some text
\end{workpackages}
\crcItem{C} some text
  \crcItem{D} some text
  \begin{workpackages}
  \crcItem{D1} some text
  \crcItem{D2} some text
  \end{workpackages}
\end{workpackages}

\cref{\mylist:A,\mylist:B}
% that should be the same as
% \crcWP{A,B}

\cref{\mylist:C}
% that should be the same as
% \crcWP{C}

% ==== new section
\def\mylist{def} %% <<<< difference!
\restartlist{workpackages}
\begin{workpackages}
  \crcItem{A} some text
  \crcItem{B} some text
  \begin{workpackages}
  \crcItem{B1} some text
  \crcItem{B2} some text
  \end{workpackages}
  \crcItem{C} some text
  \crcItem{D} some text
  \begin{workpackages}
  \crcItem{D1} some text
  \crcItem{D2} some text
  \end{workpackages}
\end{workpackages}
\cref{\mylist:B1,\mylist:C}
% that should be the same as
% \crcWP{B1,C}

\cref{\mylist:D2}
% that should be the same as
% \crcWP{D2}

\end{document}

在此处输入图片描述

答案1

你可以使用expl3它比 更灵活etoolbox。主要部分是

\NewDocumentCommand{\crcWP}{m}
 {
  \clist_clear:N \l__lukascb_wp_refs_clist
  \clist_map_inline:nn { #1 } { \clist_put_right:Nn \l__lukascb_wp_refs_clist { \mylist-##1 } }
  \exp_args:NV \cref \l__lukascb_wp_refs_clist
 }

清除辅助列表,然后使用输入填充,但\mylist-在前面添加。然后将新列表传递给\cref

完整例子。

\documentclass{article}
\usepackage{enumitem}
\usepackage[compress]{cleveref}

% ==== List for Workpackages
%
\newlist{workpackages}{enumerate}{3}
\setlist[workpackages,1]{
  label={WP\,\arabic*:},
  ref = {\arabic*},
  resume
}

\setlist[workpackages,2]{
  ref={\theworkpackagesi\alph*},
  label={WP\,\theworkpackagesi\alph*:}
}

\crefname{workpackagesi}{\bfseries WP}{\bfseries WPs}
\creflabelformat{workpackagesi}{#2#1#3}
\crefname{workpackagesii}{\bfseries WP}{\bfseries WPs}
\creflabelformat{workpackagesii}{#2#1#3}



\newcommand{\mylist}{}

\ExplSyntaxOn
\NewDocumentCommand{\crcWP}{m}
 {
  \clist_clear:N \l__lukascb_wp_refs_clist
  \clist_map_inline:nn { #1 } { \clist_put_right:Nn \l__lukascb_wp_refs_clist { \mylist-##1 } }
  \exp_args:NV \cref \l__lukascb_wp_refs_clist
 }
\ExplSyntaxOff

\newcommand{\crcItem}[1]{\item\label{\mylist-#1}}

\usepackage{cleveref}
\begin{document}
\def\mylist{abc}
\begin{workpackages}
  \crcItem{A} some text
  \crcItem{B} some text
  \begin{workpackages}
  \crcItem{B1} some text
  \crcItem{B2} some text
\end{workpackages}
\crcItem{C} some text
  \crcItem{D} some text
  \begin{workpackages}
  \crcItem{D1} some text
  \crcItem{D2} some text
  \end{workpackages}
\end{workpackages}

\crcWP{A,B}

\crcWP{C}

% ==== new section
\renewcommand\mylist{def} %% <<<< difference!
\restartlist{workpackages}
\begin{workpackages}
  \crcItem{A} some text
  \crcItem{B} some text
  \begin{workpackages}
  \crcItem{B1} some text
  \crcItem{B2} some text
  \end{workpackages}
  \crcItem{C} some text
  \crcItem{D} some text
  \begin{workpackages}
  \crcItem{D1} some text
  \crcItem{D2} some text
  \end{workpackages}
\end{workpackages}

\crcWP{B1,C}

\crcWP{D2}

\end{document}

我必须将冒号改为-,因为:在代码中它是特殊的expl3,但这不应该有问题,因为您没有明确使用标签。

附注:这样做\providecommand{\mylist}{}是错误的:您要确保您使用的命令不存在;否则当您重新定义它时会遇到问题。

在此处输入图片描述

相关内容