# Automatically register arguments a command is used with

I am wondering whether it is possible to create some sort of set to keep track of elements in latex. One intended usage would be to automatically generate a list of arguments that have been passed to a particular command throughout the document. This could be helpful to keep track of abbreviations or custom commands in documents that make heavy use of them. A minimal not-working example could be as follows

\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}


It's easy with 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}


Note. Since you're likely to use \coeff{A} in math formulas, I chose not hardwire $...$ in it.

• Excellent, thank you egreg! Searching for expl3 revealed that this is for "experimental LaTeX3" support. Do you know how good the support for these constructs is? E.g., while the above works on my institute's sharelatex instance, could the same be expected for submissions to arXive or the like? Jul 27, 2021 at 15:05
• @mrclng It's no longer “experimental”. The name expl3 has become the name with no strings attached. Jul 27, 2021 at 15:21