使用 LaTeX3 复制类似结构化的宏定义

使用 LaTeX3 复制类似结构化的宏定义

是否有可能创建类似结构的宏?例如,在 MWE 中我有一个\creator,我想创建相同定义的宏\lecturer,因此可以通过将单词替换creator为来实现lecturer。如果我有许多相同的宏,那就太无聊了。我需要以某种方式在程序级别定义列表,然后creator, lecturer,practicer乘以用户级宏\creator\lecturer,\practicer`...

\documentclass{article}

\ExplSyntaxOn


\int_new:N \l_creator_int
\NewDocumentCommand\creator{ O{} m}
{
    \int_incr:N \l_creator_int
    \cs_new_protected:cpn {creator\int_use:N\l_creator_int}{#2}
    \tl_if_empty:nTF {#1} {} {\cs_new_protected:cpn {creator\int_use:N\l_creator_int email}{#1}}
    
}

%\int_new:N \l_lecturer_int
%\NewDocumentCommand\creator{ O{} m}
%{
%    \int_incr:N \l_lecturer_int
%    \cs_new_protected:cpn {lecturer\int_use:N\l_lecturer_int}{#2}
%    \tl_if_empty:nTF {#1} {} {\cs_new_protected:cpn {lecturer\int_use:N\l_lecturer_int email}{#1}}
%    
%}

%...

% same for \practicer and other
\ExplSyntaxOff


\begin{document}

\creator[]{First}
\creator{Second}
\creator[[email protected]]{Third}

\end{document}

答案1

你用错了方法。首先,你不应该使用功能(又名) 用于存储数据,而是标记列表变量或在本例中为序列。

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\creator}{O{}m}
 {
  \sk_class_data_add:nnn { creator } { #1 } { #2 }
 }
\NewDocumentCommand{\lecturer}{O{}m}
 {
  \sk_class_data_add:nnn { lecturer } { #1 } { #2 }
 }
\NewDocumentCommand{\retrieve}{mm}
 {% #1 is the type, creator or lecturer
  % #2 is the index
  \seq_item:cn { g_sk_class_#1_name_seq } { #2 }
  \tl_if_blank:eF { \seq_item:cn { g_sk_class_#1_email_seq } { #2 } }
   { ~ \texttt{\seq_item:cn { g_sk_class_#1_email_seq } { #2 }} }
 }


\seq_new:N \g_sk_class_creator_name_seq
\seq_new:N \g_sk_class_creator_email_seq
\seq_new:N \g_sk_class_lecturer_name_seq
\seq_new:N \g_sk_class_lecturer_email_seq

\cs_new_protected:Nn \sk_class_data_add:nnn
 {
  % add the name to the proper sequence variable
  \seq_gput_right:cn { g_sk_class_#1_name_seq } { #3 }
  % add the email to the proper sequence variable
  \seq_gput_right:cn { g_sk_class_#1_email_seq } { #2 }
 }

\ExplSyntaxOff


\begin{document}

\creator[]{First}
\creator{Second}
\creator[[email protected]]{Third}

\lecturer{Me}
\lecturer[]{You}
\lecturer[[email protected]]{Count Dracula}

\retrieve{creator}{3}

\retrieve{lecturer}{2}

\retrieve{lecturer}{3}

\end{document}

在此处输入图片描述

相关内容