TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


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.

share|improve this question
up vote 10 down vote accepted

Here a solution that uses the etoolbox package:

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

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

share|improve this answer

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

share|improve this answer

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].

share|improve this answer

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]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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