I'm trying to find a command or define a command that takes a list of integer and an integer and does things when the integer in in the list and does something else if it is not the case. I have seen that there is \IfSubStr from the xstring package which works but doesn't exactly do what I want since for example it will be true if the list is {2, 3, 5, 11} and the int is 1. I have also tried to use expl3. Here is my attempt on a minimal example:


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

  \def\isfound{0}% Initialize a flag
  \def\elementtocheck{#1}% Store the element to check
  \foreach \element in #2 {%
    \FPeval{\elementasnum}{\element}% Convert the element in the array to a number
      \def\isfound{1}% Set the flag to 1 if the element is found
      \breakforeach% Exit the loop if found

  \def\mylist{#2}% Convert the comma-separated list to an array
  \foreach \element in \mylist {%
  \foreach \i in {1,..., \nblinc}{%
     \foreach \j in {0,...,\nbcolc}{%
        % Check if \res is in the array using \IfSubStr
          \xappto\mytablecontents{\fbox{$\res$} &}%
          \xappto\mytablecontents{$\res$ &}%
  \begin{tabular}{r *{\nbcolt}{c}}%


I don't really know why it doesn't work. The condition is always false. I'm kind of lost now.
Can someone please help me out ?
Thanks in advance,

  • Well the first advice is: Don't overcomplicate your code and don't mix so many packages. You do not need expl3, fp, xstring, etoolbox, pgffor and primitive TeX tests in one piece of code. Beside: you are storing the numbers in commands, that means you must expand the argument: \NewDocumentCommand \IfStringInList {mmmm} { \ExpandArgs{ee}\clist_if_in:nnTF {#1} {#2} {#3} {#4} }. Sep 20, 2023 at 17:53
  • Oh thanks a lot. Yeah, I'm really new to this aspect of LaTex and just wanned to be able to have a first version that works. Tbh, I thought that this was the simplest version (actually sorry the command ifnuminarray is useless). I tried to find other ways to manipulate lists and to create a table like so but didn't found an easier way. Do you think it is possible to add tikz commands in the tablecontent definition, because instead of \fbox I would prefer to circle the entries.
    – Rhylx
    Sep 20, 2023 at 18:10

1 Answer 1


enter image description here





\testTF{1,5,7,9}{7}{7 is in}{7 is NOT in}

\testTF{1,5,7,9}{3}{3 is in}{3 is NOT in}


  • Thanks for this reply. This does not make use of any package. Also as Ulrike said I need to expand arguments to make it work. The same trick should also work here isn't it ?
    – Rhylx
    Sep 21, 2023 at 10:22
  • @Rhylx yes, or more directly in expl3 instead of \ExpandArgs{ee} use \clist_if_in:eeTF which you can define via \cs_generate_variant:Nn \clist_if_in:nnTF {ee} You never need to load expl3 or xparse in current latex (and since the features they provide are pre-loaded you don't usually need the older packages such as etoolbox, fp, xstring for new code either) Sep 21, 2023 at 10:27

You must log in to answer this question.

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