TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a package providing a collection of fully expandable conditionals like this one:


Background: Let's say I want to program an expandable recursive macro \foo which contains a nested use of \ifnum. Then this could look like this:

  ... some stuff ...
  \ifnum\bar=\z@% exit condition
    \ifnum\baz>\quux% another exit condition

But even this won't work because the last \fi is not always destroyed. So off to \romannumeral trickery...

With the above macro this conditional looks like this:

  ... some stuff ...

Much cleaner, isn't it?

share|improve this question
Do you want just the conditionals, or conditionals plus 'other stuff'? I assume what you want here is 'conditionals with LaTeX syntax', similar to for example \@ifpackageloaded in how they behave? – Joseph Wright Sep 5 '12 at 11:19
@JosephWright Well the use case is that I need exactly this one in one of my packages, but I hate redefining generic stuff, so the minimal "canonical" package providing this would suffice. – Stephan Lehmke Sep 5 '12 at 11:20
@JosephWright I don't care so much for the syntax, but for convenient expandable programming without the need to desintegrate a lot of leftover \fis. – Stephan Lehmke Sep 5 '12 at 11:22
The classic in this area (and one of the first uses I think of that \expandafter trick (which didn't appear in plain or latex2.09) is Alan Jeffrey's lambda.sty – David Carlisle Sep 5 '12 at 11:26
It's not stated in the question, but I assume we can use an e-TeX solution to allow integer expressions, not just single integers? – Joseph Wright Sep 5 '12 at 11:48
up vote 8 down vote accepted

I'm not sure there is a package providing only the tests requests, but the two obvious candidates for this as part of a more general toolkit are etoolbox and expl3. The etoolbox package is not as big in scope as expl3, and uses a more 'traditional' TeX syntax. In the case in question, you would have

  \ifnumcomp{#1}{#2}{#3} % eg "1 + 1", "=" and "2" would take the TRUE branch

where the two integer values are expressions but must be given separately.

The LaTeX3 expl3 package provides an entire programming language, of which expandable tests are only part. A suitable definition here would be

 \cs_new_eq:NN \my@ifnum \int_compare:nTF

as \int_compare:nTF takes a single argument (such as 1 + 1 = 2) and evaluates the test to absorb either the TRUE or FALSE branch as needed. (Note that there is also a slightly faster \int_compare:nNnTF, which like \ifnumcomp needs three arguments.) The LaTeX3 conditionals are available as 'only TRUE' or 'only FALSE' versions, e.g. \int_compare:nT, to avoid needing empty groups.

Perhaps at a tangent to what is wanted, but you might also take a look at boolexpr for expandable Boolean expressions. Both etoolbox and expl3 also do this, but the implementations and interfaces are different.

share|improve this answer
Great, etoolbox seems to be just what I need! (only used \patchcommand so far ;-) – Stephan Lehmke Sep 5 '12 at 11:54

Your Answer


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.