I need to store a csv list for later use in the end code part of an environment. Can somebody explain me what's going wrong in the following example?

  \ifthenelse{\equal{#1}{1}}{one, }{}
  \ifthenelse{\equal{#1}{2}}{two, }{}
  \ifthenelse{\equal{#1}{3}}{three, }{}
\doprint{1,2,3} % does work

\doprint{\mycsv} % does not work

If the list is stored in the macro, it seems to be no longer a list for \forcsvlist but a single token. Any other solution which solves my problem?

  • 2
    Related/duplicate tex.stackexchange.com/questions/22393/forcsvlist-and-expansion – egreg Feb 5 '13 at 14:59
  • @egreg Yep '\newcommand*\doprint[1]{\expandafter\forcsvlist\expandafter\printnumbers\expandafter{#1}}' does the trick. Thank you! Oh, i hate TeX ;-) – Josef Feb 5 '13 at 15:11
  • You can hide that in a \xforcsvlist macro: \newcommand{\xforcsvlist}[2]{\expandafter\forcsvlist\expandafter#1\expandafter{#2}} and then \newcommand{\doprint}[1]{\xforcsvlist\printnumbers{#1}} – egreg Feb 5 '13 at 15:26

Since #1 can't be all of 1, 2, 3 at the same time, there is an inefficiency in OP's \printnumbers. Here is an alternative solution.

\newcommand*\comma{\ifforeachlastitem.\else, \fi}
  \newforeach [expand list once] \x in {#1} {%
      \@latex@error{\string\doprint: no match}\@ehd
\doprint{} % empty list

This solution can be generalized to other list types beyond {1,2,3}.

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.