我正在尝试查找一个命令或定义一个命令,该命令接受一个整数列表和一个整数,当整数在列表中时执行操作,如果不在列表中则执行其他操作。我已经看到 \IfSubStr 来自字符串包可以工作,但不能完全满足我的要求,因为例如,如果列表是 {2, 3, 5, 11} 并且 int 是 1,它将为真。我也尝试过使用 expl3。这是我对一个最小示例的尝试:
\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}
我真的不知道为什么它不起作用。条件总是错误的。我现在有点迷茫了。
有人能帮我吗?
提前谢谢,
Rhylx