I would like a new command: \myfunc{string}, where string is something like: "a,b,x,y,x" and the effect of the command should be the following: \func{a}\func{b}\func{x}\func{y}\func{x}, where \func{} is another predefined function. How to do this?

2 Answers 2


This is very easy with xparse:


\NewDocumentCommand{\myfunc}{ >{\SplitList{,}} m }{%

  \fbox{#1} % a space follows




enter image description here

Note that \func can be defined in whatever way you like, also with \newcommand. What's important is that it has one argument.

You can also add code before \ProcessList{#1}{\func} and after it.

Note that you're not restricted to commas as separators, because you specify it at definition time.

Another advantage is that \SplitList automatically trims leading and trailing spaces around items, so the output from

\myfunc{ a , b,c ,x,y , z }

will be the same, contrary to other lower level approaches.


LaTeX has a built in command for iterating over comma separated lists:






  • Do you know if it is possible to use for loop on comma separated list if parameter of \myfunc is another function. So: \def \someVar {a, b, c} then \myfunc{\someVar} In this case all variable is considered a one item
    – Dmytro
    Jul 9, 2020 at 18:17
  • @Dmytro \expandafter\myFunc\expandafter{\someVar} Aug 13, 2022 at 2:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .