I wouldn't use ifthen
. A pgf membership test can be used to implement your condition \x in {1,2}
,
\documentclass[tikz,border=3mm]{standalone}
\makeatletter
\pgfmathdeclarefunction{memberQ}{2}{%
\begingroup%
\edef\pgfutil@tmpb{0}%
\edef\pgfutil@tmpa{#2}%
\expandafter\pgfmath@member@i\pgfutil@firstofone#1\pgfmath@token@stop
\edef\pgfmathresult{\pgfutil@tmpb}%
\pgfmath@smuggleone\pgfmathresult%
\endgroup}
\def\pgfmath@member@i#1{%
\ifx\pgfmath@token@stop#1%
\else
\ifnum#1=\pgfutil@tmpa\relax%
\gdef\pgfutil@tmpb{1}%
%\typeout{#1=\pgfutil@tmpa}
\fi%
\expandafter\pgfmath@member@i
\fi}
\makeatother
\begin{document}
\begin{tikzpicture}
\foreach \x [evaluate=\x as \isMember using {int(memberQ({1,2},\x))}] in {0,...,3}{
\foreach \y in {0,...,2}{
\draw \ifnum\isMember=1
[red,fill=red]
\fi (\x,\y) circle[radius=0.4cm];
}
}
\end{tikzpicture}
\end{document}
This membership test works unless the last entry of the list has a substructure. Similar limitations apply to the dim
function, which is built in pgf
, but not mentioned in the manual, presumably for that reason. However, as long as you have ordinary lists, both memberQ
and dim
do work properly. (Proposed alternatives with a much more extensive codes also have drawbacks, sometimes even more severe, and I wish that those who propose them would mention them, too.)
As you can see, an ordinary \ifnum
can be built into the path.
\draw \ifnum\itest=1
[red,fill=red]
\fi (\x,\y) circle[radius=0.4cm];
You then only have to draw one path. You can also use the conventional ifthenelse
instead of the memberQ
function.
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \x [evaluate=\x as \itest using {int(ifthenelse(\x>0 && \x <3,1,0))}] in {0,...,3}{
\foreach \y in {0,...,2}{
\draw \ifnum\itest=1
[red,fill=red]
\fi (\x,\y) circle[radius=0.4cm];
}
}
\end{tikzpicture}
\end{document}
In this case you do not even have to use evaluate
, you can simply use the fact that \numexpr
truncates the result,
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,3}{
\foreach \y in {0,...,2}{
\draw \ifnum\the\numexpr\x/2\relax=1
[red,fill=red]
\fi (\x,\y) circle[radius=0.4cm];
}
}
\end{tikzpicture}
\end{document}

\ifthenelse
. – egreg Nov 13 '19 at 15:34in
test forifthenelse
. Try\ifthenelse{\x < 3}
or similar. – Rmano Nov 13 '19 at 15:44\documentclass{...}
, the required\usepackage
's,\begin{document}
, and\end{document}
. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Rmano Nov 13 '19 at 15:46