This question is related to How can I specify a long list of math operators? – but with a twist: I want to allow spaces in in the comma-separated list, like so:

\newacronyms{acm, ams, cpu, nih}

For each of these, the macro \newacronym (note missing suffix “s”) should be invoked, which is defined as follows:


Then I can use the acronyms in a text as follows:

The \nih is the National Institute of Health.
Modern computers have several \cpu[s].

– But how does the \newacronyms macro look like?

Partial solution

I got it working without spaces – that is, it only works as \newacronyms{foo,bar}:


My approach (not working …)

Now I need a way to gobble the leading spaces inside the \@i macro but I’ve come up dry. My initial thought was that something along the following lines should work, but to no avail.



It should be noted that \gobble@spaces works in similar circumstances (although I’m not sure why … shouldn’t there be an \expandafter before the \@gobble?), just not in the above:

% This WORKS:
\def\mylst{1, 2, 3, 4}
\@for\i:=\mylst\do{print ``\expandafter\gobble@spaces\i''\\}

Incidentally, does an equivalent command already exist somewhere in the kernel? I can’t imagine that I’m the first to need it.

5 Answers 5


Here a solution that uses the etoolbox package:

\newcommand*{\newacronym}[1]{\typeout{New acronym: [#1]}}
\newacronyms{acm, ams, cpu, nih}

In ConTeXt, you have a \processcommalist macro for processing comma lists. So your example could be written as



I am pretty sure that there will be a LaTeX package that does the same thing. If not, you can always copy the definition of \processcommalist


To answer your smaller question: to gobble spaces inside a macro or even around a string of tokens, check out the trimspaces package:


A solution with the clist module of LaTeX3, which directly takes care of trimming white space:

\NewDocumentCommand{\newacronyms}{ m }
   \clist_map_inline:nn { #1 }
      \cs_new:cpn { ##1 } { \acroprint {##1} }
\NewDocumentCommand{\acroprint}{ m O{} }
   { \acronymstyle #1 } #2 \xspace


\newacronyms{acm, ams, cpu , nih }

The \nih is the National Institute of Health.
Modern computers have several \cpu[s].

Item foo of the comma separated list defines \foo to expand to \acroprint{foo} and \acroprint has also an optional argument following the mandatory one, so \foo[s] expands to \acroprint{foo}[s] which typesets the optional argument outside the braces delimiting the action of \acronymstyle.

The command \acroprint can be used by itself: \acroprint{bar}[s].


You can put everything together. Your definition of \gobble@spaces leaves much to be desired.

% \docommalist trims leading and trailing spaces in the elements of the list:

\newacronyms{acm , ams , cpu, nih}

\acm, \ams, \acm[s], \ams[s]

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.