0

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:

\documentclass{article}
\usepackage{pgffor,etoolbox}
\usepackage{fp}
\usepackage{xstring}
\usepackage{xintexpr}
\usepackage{expl3,xparse}

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

\newcommand{\ifnuminarray}[2]{%
  \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
    \ifnum\elementasnum=\elementtocheck
      \def\isfound{1}% Set the flag to 1 if the element is found
      \breakforeach% Exit the loop if found
    \fi
  }%
  \ifnum\isfound=1
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}

\newcommand{\mytable}[2]{%
  \def\mylist{#2}% Convert the comma-separated list to an array
  \def\maxvalue{0}
  \foreach \element in \mylist {%
    \IfInteger{\element}{%
      \ifnum\element>\maxvalue
        \xdef\maxvalue{\element}%
      \fi
    }{}%
  }%
  \def\mytablecontents{}%
  \FPeval{\nbcolc}{clip(#1-1)}%
  \FPeval{\nblinc}{clip(trunc(\maxvalue/#1:0)+1)}%
  %\def\nblinc{\xintexpr{floor(\nblincnint)}}
  \foreach \i in {1,..., \nblinc}{%
     \foreach \j in {0,...,\nbcolc}{%
        \FPeval{\res}{clip((\nbcolc+1)*(\i-1)+\j)}
        % Check if \res is in the array using \IfSubStr
    \IfStringInList{\mylist}{\res}{%
          \xappto\mytablecontents{\fbox{$\res$} &}%
        }{%
          \xappto\mytablecontents{$\res$ &}%
        }%
     }%
     \gappto\mytablecontents{\\}%
  }%
  \FPeval{\nbcolt}{clip(#1+1)}%
  \begin{tabular}{r *{\nbcolt}{c}}%
     \mytablecontents%
   \end{tabular}%
}

\begin{document}
\begin{table}
\centering
\mytable{3}{2,4,8,11}
\end{table}
\end{document}

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,
Rhylx

2
  • 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

2

enter image description here

\documentclass{article}



\ExplSyntaxOn
\let\testTF\clist_if_in:nnTF
\ExplSyntaxOff


\begin{document}

1,5,7,9

\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}


\end{document}

2
  • 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 .