Check if number is in list of numbers

\ifcase doesn't work like the switch statement in other languages, in which you choose what values have a branch of their own. The syntax of \ifcase is:

\ifcase<number>
    <case 0>
\or <case 1>
\or <case 2>
\or <as many as you want>
\else <other cases>
\fi

you can't skip a value. In your code you'd need:

\foreach \x in {10, 9,..., 0}
{
  \ifcase\x \relax
      % 0
  \or % 1
  \or % 2
  \or % 3
  \or % 4
  \or % 5
     \node[] {\x};
  \or % 6
  \or % 7
  \or % 8
  \or % 9
  \or % 10
  \else % other cases
  \fi
}

which is a handful. For a small number of exceptions you could use \ifnum:

\foreach \x in {10, 9,..., 0}
{
  \ifnum\x=5 \relax
     \node[] {\x};
  \else\ifnum\x=10 \relax
     % do things with \x=10
  \else
     % possibly more cases
  \fi\fi
}

which can become a mess, once you have more than a couple of cases.

My suggestion: \int_case:nnF. You can specify each case individually and a false branch in case no other is taken:

\documentclass{article}
\usepackage{tikz}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \IntCasennF \int_case:nnF
\ExplSyntaxOff
\begin{document}
\begin{tikzpicture}
\foreach \x in {10, 9,..., 0}
{
  \IntCasennF {\x}
    {
      {5}{\node[] {\x};}
      {10}{<Code for 10>}
    }
    {<Other cases>}
}
\end{tikzpicture}
\end{document}

\if I understand the question correctly, you want to check whether or not a number is equal to A, B, C etc. This can be done as follows.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach \X in {10, 9,..., 0}
{
\pgfmathtruncatemacro{\itest}{ifthenelse(\X==5||\X==7,1,0)}
    \ifnum\itest=1
         \node at (\X,0) {\X};
    \fi
}
\end{tikzpicture}
\end{document}

For more extensive applications I recommend the memberQ function, which may or may not become one day part of the pgf world. It tests if an integer is in a list of integers.

\documentclass{article}
\usepackage{tikz}

\makeatletter
\pgfmathdeclarefunction{memberQ}{2}{%
  \begingroup%
    \edef\pgfutil@tmpb{0}%
    \edef\pgfutil@tmpa{#2}%
    \expandafter\pgfmath@member@i#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 in {10, 9,..., 0}
{
\pgfmathtruncatemacro{\itest}{memberQ({1,4,8},\X)}
    \ifnum\itest=1
         \node at (\X,0) {\X};
    \fi
}
\end{tikzpicture}
\end{document}

Tags:

Conditionals