[Tex/LaTex] How to create a conditional which checks if one or another condition is true in plain TeX

conditionalsplain-tex

I need to make a conditional like this:

IF #1 > \variable OR #1 = 0
THEN
   PRINT "True"
ELSE
   PRINT "False"

If #1 is greater than \variable or is equal to 0, then it it true. The value only contains integers of 0 or greater. There are never any decimals.

I have this conditional in plain TeX, which checks if #1 is greater than \variable, but I do not know how to add the #1 = 0 part.

\ifnum0#1>\variable
    True
\else
    False
\fi

How can I create a conditional that checks if at least one of these conditionals is true?

Best Answer

\if\ifnum#1>\variable T\else\ifnum#1=0 T\else F\fi\fi T%
  TRUE
\else
  FALSE
\fi

\if expands tokens until two non-expandable tokens remain. If one of the conditions is true, then the expansion is T, otherwise F. This is compared with the last T in the line.

  • Advantage: TRUE is not duplicated as in

    \ifnum#1>\variable
      TRUE
    \else
      \ifnum#1=0 %
        TRUE
      \else
        FALSE
      \fi
    \fi
    
  • Advantage: All number parsings are stopped by well defined tokens. Example: The token T stops the scanning of the number \variable. If the TRUE part starts with a number 123abc and \variable is defined as 5

    \def\variable{5}
    \ifnum#1>\variable
      123abc
    \fi
    

    Then #1 must be greater than 5123!

    Depending on the definition of \variable the evaluation of \variable as number might skip a first space of the TRUE part.

  • Advantage: The conditionals are well matched, thus this construct can also be used inside other conditionals. If TeX skips a conditional branch it only checks the command tokens for real \if... primitives, \else, \fi, and \or (for \ifcase).

    For example with a macro \ifOR the following is not well matched:

    \ifOR{\ifnum#1>\variable}{\ifnum#1=0}
      TRUE
    \else
      FALSE
    \fi
    

    \ifOR does not count as macro, but there are two \ifnum, but only one \fi.

  • Disadvantage: The \if condition is not easy to read.

Related Question