In a bigger context, I need to check whether an integer parameter is contained in a set of numbers. I decided to encode sets as strings; elements are separated by !. I don't know whether this is the best way to do this, but a curious thing happens:
\documentclass{article}
\usepackage{pgffor,xifthen}
\newcommand{\ifin}[1]{
\ifthenelse{\isin{!#1!}{!1!3!}}{T}{F}%
}
\begin{document}
\ifin{1}\ifin{2}\ifin{3}\ifin{4}
\foreach \n in {1,...,4} {%
\ifin{\n}%
}
\end{document}
The expected output is
TFTF
TFTF
but the actual output is
TFTF
FFFF
Note that pdflatex does not report any errors or warnings.
I suspect that this is a more general issue (\isin checks whether strings are substrings of other strings; it probably fails because the parameter is a number) but I can not find material on this. How can I achieve the desired behaviour?
\foreach \n in {1,...,4} {% \edef\x{\noexpand\ifin{\n}}\x }– Marco Daniel May 17 '12 at 14:11\expandafter\ifin\expandafter{\n}%– David Carlisle May 17 '12 at 14:14\foreach. But if you do any assignment in the scope of\foreach, it will be local to that scope/group, and you won't find it outside that scope unless you prefix the assignment with\global. Moreover, I hope your example is actually a MWE, since it doesn't make much sense to me. – Ahmed Musa May 17 '12 at 15:01