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 have a document which needs a large variety of plain TeX conditionals, e.g.:

IF #1 >= 1 AND #2 >= 10
IF #1 != “tree” OR #2 == “mountain”
IF 10 > #1 > 2

I have found limited documentation on this subject and most is quite terse and not easy to understand, or they just describe the individual components, but do not explain how they are used together. What documentation thoroughly describes how to create plain TeX conditionals, while providing ample example code, and showing how to avoid common mistakes?

share|improve this question
1  
They are of course explained in The TeXBook, but should also be included in the free books TeX By Topic and TeX for the Inpatient. I'm not sure if this is the detail/level you are looking for. Note that there are only single expression ifs, so you can't directly use AND or OR, but have to split them to multiple cascaded ifs. –  Martin Scharrer Apr 25 '12 at 10:34
1  
In addition to what @Martin says, there are a number of questions on TeX-sx about conditionals. They highlight, for example, TeX's \if is not used in the way you seem to be suggesting in your psuedo-code. So we could do with a bit more information on what you do know/have tried. –  Joseph Wright Apr 25 '12 at 10:38
1  
Also, do you want to focus on how to use the plain TeX conditionals to set up for example Boolean logic 'higher level' conditionals? That has been implemented by a number of people (Philipp Lehman in etoolbox, the LaTeX3 project, etc.), but will need quite a lot of detail. It may also obscure how the primitives work, at least to some extent. –  Joseph Wright Apr 25 '12 at 10:41
    
Just use plain-luatex instead and your pseduo code will work (except for the last one where you need if 10 > #1 and #1 > 2 and you need to use lowercase and, or, and if). –  Aditya Apr 25 '12 at 16:47

1 Answer 1

up vote 11 down vote accepted

As mentioned in the comments the TeXBook is the ultimate reference for these things. But also it is worth mentioning the terminology which may help when searching for free documentation on this site or elsewhere.

The conditionals themselves \if \ifnum and friends are TeX primitives rather than defined in plain TeX. TeX itself does not provide boolean operators such as OR and AND although of course various macro packages implement these (by suitable nesting of the primitive if constructs) but plain TeX does not in fact define any such macros. So a literal reading (but not so helpful) reading of your question would lead to the answer that there is no documentation as plain TeX does not have AND or OR. (there is an \or primitive that is used with \ifcase but that is not the infix connector implied by your question).

Your first example:

IF #1 >= 1 AND #2 >= 10

\ifnum#1>1 
  \ifnum#2>9
     yes
   \else
     no
   \fi
 \else
 no
\fi

Your second example, here you would have to be more specific

IF #1 != “tree” OR #2 == “mountain”

does #1 have to be given as {tree} or does \def\x{tree}..... {\x} count as true, and if so do you just want to allow one level of expansion or an arbitrary number. The testing is rather different in each case.

But perhaps

\def\treetest{tree}\def\mountaintest{mountain}
\def\testa{#1}\def\testb{#2}
\ifx\treetest\testa
   \ifx\mountaintest\testb
      yes
    \else
      no
    \fi
 \else
   yes
 \fi

This one is the same as the first, really.

IF 10 > #1 > 2
share|improve this answer

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.