My question is similar to the one asked in ASdeL's question, except I am testing a loop variable against a predefined set. It seems that none of the solutions there are working for me because \imgidx is not expanded before the comparison. How can I go about this?


\NewDocumentCommand \IfStringInList {mmmm}
  { \clist_if_in:nnTF {#2} {#1} {#3} {#4} }


\def \imgperrow {8}

\foreach \x in {0, ..., 4}
    \foreach \y in {1, ..., \ymax}
        \pgfmathtruncatemacro {\imgidx}{\x*\imgperrow + \y}
        {\IfStringInList {\imgidx}{1,2,3,4}{img A}{img B}}


In this example, I should get the first four to be "img A," but all of them show up as "img B."

Thank you for your help.


1 Answer 1


You're right that you need to expand \imgidx. Because you are passing a variable as the argument, you just need to change \clist_if_in:nnTF to \clist_if_in:nVTF, which takes a command instead of a braced token list as its second argument. You might also want to rename as

\NewDocumentCommand \IfStringValueInList {mmmm}
  { \clist_if_in:nVTF {#2} #1 {#3} {#4} }

with a corresponding change in the code that performs the test.

The clist functions don't expand their arguments unless requested, so here the V means "before doing the test extract the Value of the input variable and use that as the argument instead".

By the way, I'm not sure if this matters too much inside a PGF environment but note that you need to add comment signs to avoid unwanted whitespace from being inserted in text contexts:

\foreach \x in {0, ..., 4}
    \foreach \y in {1, ..., \ymax}
        \pgfmathtruncatemacro {\imgidx}{\x*\imgperrow + \y}%
        {\IfStringValueInList \imgidx{1,2,3,4}{A}{B}}%
  • V expects an unbraced argument (we know it also works with braces, but…)
    – egreg
    Commented Nov 14, 2017 at 23:25
  • @egreg — true. thanks for keeping me honest :) Commented Nov 15, 2017 at 10:39

You must log in to answer this question.

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