1

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}

1 Answer 1

1

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}

enter image description here

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

2
  • 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?
    – mrclng
    Jul 27, 2021 at 15:05
  • @mrclng It's no longer “experimental”. The name expl3 has become the name with no strings attached.
    – egreg
    Jul 27, 2021 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.