Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

up vote 5 down vote accepted
\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.

share|improve this answer
add comment

Here is one way, where the first macros are essentially from lambda.sty by Alan Jeffrey:

\def\True#1#2{#1}
\def\False#1#2{#2}
\def\Or#1#2{#1\True{#2}}
\def\gobblefalse\else\gobbletrue\fi#1#2{\fi#1}
\def\gobbletrue\fi#1#2{\fi#2}
\def\TeXif#1{#1\gobblefalse\else\gobbletrue\fi}

\def\Morethan#1#2{\TeXif{\ifnum#1>#2 }}
\def\Equals#1#2{\TeXif{\ifnum#1=#2 }}

\def\Test#1{%
  \Or
    {\Morethan{#1}\variable}
    {\Equals{#1}0}
  {True}
  {False}}

\newcount\variable
\variable=5

\Test{2} \Test{5} \Test{10} \Test{0}

\variable=2
\Test{2} \Test{5} \Test{0} \Test{1}
\bye

I have a feeling there is a possibility for a higher level abstraction in the spirit of Schemesque cond here somewhere...

share|improve this answer
add comment

You can use

\def\variable{5}
\def\test#1{%
  \ifnum0#1>\variable
    True
  \else
    \ifnum0#1=0
      True
    \else
      False
    \fi
  \fi
}
\test{6} \par
\test{5} \par
\test{4} \par
\test{3} \par
\test{2} \par
\test{1} \par
\test{0}

which outputs

True
False
False
False
False
False
True

Note that this would only work for positive integers.

share|improve this answer
add comment

My solution(s) has the some idea as Werner's answer but extend this a little:

\def\variable{5}

% Solution 1
\def\ifOr#1#2{%
 \csname%
  \ifnum#1%
    iftrue%
  \else%
    \ifnum#2%
      iftrue%
    \else%
      iffalse%
    \fi%
  \fi%
 \endcsname%
}
\def\test#1{%
  \ifOr{0#1>\variable}{#1=0}%
    True%
  \else%
    False%
  \fi%
}

% Solution 2
\def\ifOR#1#2{%
 \csname%
  #1%
    iftrue%
  \else%
    #2%
      iftrue%
    \else%
      iffalse%
    \fi%
  \fi%
 \endcsname%
}
\def\Test#1{%
  \ifOR{\ifnum0#1>\variable}{\ifnum#1=0}%
    True%
  \else%
    False%
  \fi%
}

\test{8} \Test{8} \par
\test{4} \Test{4} \par
\test{3} \Test{3} \par
\test{2} \Test{2} \par
\test{1} \Test{1} \par
\test{0} \Test{0}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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