11

Consider the following logical satatements:

If \conditionA != 100 and \conditionB != 100 do ``Something~A''
If \conditionA != 100 and \conditionB  = 100 do ``Something~B''
If \conditionA  = 100 and \conditionB != 100 do ``Something~C''
If \conditionA  = 100 and \conditionB  = 100 do ``Something~D''

How do I translate this into a

\ifnum ... do ... \else do ... \fi

type of argument?

I've found this post by Joseph Wright but I can't figure out how to modify the example to make it do what I would like.

P.S. Feel free to add missing tags.

  • In such a case I would consider using expl3 syntax instead. – Sean Allred Feb 15 '14 at 20:44
  • @SeanAllred You are very welcome to show me how to do it in that way. :) – Svend Tveskæg Feb 15 '14 at 20:46
11

With LaTeX3 syntax the command is expandable, if needed:

\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn

\int_new:N \l_svend_condition_A_int
\int_new:N \l_svend_condition_B_int

\cs_new:Npn \svend_check:
 {
  \int_compare:nTF { \l_svend_condition_A_int = 100 }
   {
    \int_compare:nTF { \l_svend_condition_B_int = 100 }
     {
      \typeout{Both~are~100}
     }
     {
      \typeout{A~is~100,~B~is~not~100}
     }
   }
   {
    \int_compare:nTF { \l_svend_condition_B_int = 100 }
     {
      \typeout{A~is~not~100,~B~is~100}
     }
     {
      \typeout{A~is~not~100,~B~is~not~100}
     }
   }
 }

\int_set:Nn \l_svend_condition_A_int {100}
\int_set:Nn \l_svend_condition_B_int {100}
\svend_check:
\int_set:Nn \l_svend_condition_A_int {100}
\int_set:Nn \l_svend_condition_B_int {101}
\svend_check:
\int_set:Nn \l_svend_condition_A_int {101}
\int_set:Nn \l_svend_condition_B_int {100}
\svend_check:
\int_set:Nn \l_svend_condition_A_int {101}
\int_set:Nn \l_svend_condition_B_int {101}
\svend_check:

Here's the output on the terminal:

Both are 100
A is 100, B is not 100
A is not 100, B is 100
A is not 100, B is not 100

With traditional syntax it's just the same:

\documentclass{article}

\newcommand{\Check}[2]{%
  \ifnum#1=100
    \ifnum#2=100
      \typeout{\#1 is 100, \#2 is 100}%
    \else
      \typeout{\#1 is 100, \#2 is not 100}%
    \fi
  \else
    \ifnum#2=100
      \typeout{\#1 is not 100, \#2 is 100}%
    \else
      \typeout{\#1 is not 100, \#2 is not 100}%
    \fi
  \fi
}

\Check{100}{100}
\Check{101}{100}
\Check{100}{101}
\Check{101}{101}

Here's the output:

\#1 is 100, \#2 is 100
\#1 is not 100, \#2 is 100
\#1 is 100, \#2 is not 100
\#1 is not 100, \#2 is not 100
7

For completeness, here's an etoolbox implementation:

enter image description here

\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\newcommand{\Test}[2]{%
  \ifnumequal{#1}{100}{%
    \ifnumequal{#2}{100}{%
      First arg = 100; Second arg = 100%
    }{%
      First arg = 100; Second arg != 100%
    }}{%
    \ifnumequal{#2}{100}{%
      First arg !=100; Second arg = 100%
    }{%
      First arg !=100; Second arg != 100%
    }}%      
}
\begin{document}

\Test{100}{100} \par
\Test{ 99}{100} \par
\Test{101}{101} \par
\Test{100}{101}

\end{document}
6

Does the solution have to be at the level of TeX-primitives? If not, I suppose you could load the ifthen package and execute something like this:

\ifthenelse{\NOT\conditionA=100}%
   {\ifthenelse{\NOT\ConditionB=100}%
      {"Do SomethingA"}%
      {"Do SomethingB"}}%
   {\ifthenelse{\NOT\ConditionB=100}%
      {"Do SomethingC"}%
      {"Do SomethingD"}}

Here, I'm assuming that \ConditionA and \ConditionB evaluate/expand to integers. If that's not the case, the \NOT... branch will always prevail.


Addendum: Assuming that the macros \ConditionA and \ConditionB are expandable and evaluate to integers, you could also employ the \ifnum "primitive" control sequence. Note that for the sake of simplicity equality rather than inequality is tested; hence the "Do" sequence is reversed relative to the earlier example.

\ifnum\conditionA=100%
   \ifnum\conditionB=100
      {"Do SomethingD"}
      \else {"Do SomethingC"}
   \fi
\else
   \ifnum\conditionB=100
      {"Do SomethingB"}
      \else {"Do SomethingA"}
   \fi
\fi
  • 2
    \unless from e-TeX would allow to have the branches in the same order as in the OP. – user4686 Feb 15 '14 at 22:46
5

For completeness, the (soon-to-be-completely-deprecated-hopefully) TeX-core solution, fully expandable, like the @egreg's one:

\long\def\FirstOfFour#1#2#3#4{#1}
\long\def\SecondOfFour#1#2#3#4{#2}
\long\def\ThirdOfFour#1#2#3#4{#3}
\long\def\FourthOfFour#1#2#3#4{#4}
\def\HundredTest#1#2{%
  \ifcase\numexpr
    \ifnum #1=100 1\else0\fi +
    \ifnum #2=100 2\else0\fi \relax
    \expandafter\FirstOfFour\or
    \expandafter\SecondOfFour\or
    \expandafter\ThirdOfFour\or
    \expandafter\FourthOfFour
  \fi
}

\HundredTest{100}{111}{NN}{YN}{NY}{YY} %YN
\HundredTest{100}{100}{NN}{YN}{NY}{YY} %YY
\HundredTest{111}{111}{NN}{YN}{NY}{YY} %NN
\HundredTest{111}{100}{NN}{YN}{NY}{YY} %NY

\bye
  • 1
    @tochecz why to be deprecated hopefully? do you think LaTeX3 will be ever be ported to be a layer on something entirely different from TeX? I doubt it. – user4686 Feb 15 '14 at 22:48
  • @jfbu Well, deprecated if you ask for a solution in LaTeX ;) – yo' Feb 15 '14 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.