自动注册命令所使用的参数

自动注册命令所使用的参数

我想知道是否可以创建某种集合来跟踪 latex 中的元素。一种预期用途是自动生成已在整个文档中传递给特定命令的参数列表。这可能有助于跟踪大量使​​用缩写或自定义命令的文档。一个最小的不起作用的示例可能如下

\documentclass{article}
\begin{document}
\newcommand{\coeff}[1]{
    % TODO: Register argument in a set
    $c_{#1}$
}
\newcommand{\printcoeffs}{
    % TODO: Print list/table/etc of all arguments
}
The coefficients \coeff{A}, \coeff{B}, and \coeff{C} take the values 1, 2, and 3, respectively.
\coeff{A} is particularly interesting, \coeff{B} not so much
...
\printcoeffs  % should give, e.g., A, B, C
\end{document}

答案1

很简单,使用expl3

\documentclass{article}

\ExplSyntaxOn

\seq_new:N \g_mrclng_coeffs_seq

\NewDocumentCommand{\coeff}{m}
 {
  \seq_if_in:NnF \g_mrclng_coeffs_seq { $#1$ }
   {
    \seq_gput_right:Nn \g_mrclng_coeffs_seq { $#1$ }
   }
  c\sb{#1}
 }
\NewDocumentCommand{\printcoeffs}{}
 {
  \seq_use:Nn \g_mrclng_coeffs_seq {,~}
 }

\ExplSyntaxOff

\begin{document}

The coefficients $\coeff{A}$, $\coeff{B}$, and $\coeff{C}$ take the values 1, 2, and 3, 
respectively. $\coeff{A}$ is particularly interesting, $\coeff{B}$ not so much.

\printcoeffs  % should give, e.g., A, B, C

\end{document}

在此处输入图片描述

注意。由于您可能会使用\coeff{A}在数学公式中使用,所以我选择了不是硬连线$...$在其中。

相关内容