# Package providing fully expandable conditionals

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

\newcommand\myif@num[1]
{\ifnum#1\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}


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:

\def\foo#1#2%
{%
... some stuff ...
\ifnum\bar=\z@% exit condition
\expandafter\expandafter\expandafter\@gobbletwo
\else
\ifnum\baz>\quux% another exit condition
\expandafter\expandafter\expandafter\@gobbletwo
\else
\expandafter\expandafter\expandafter\foo
\fi
\fi
\expandafter{\the\numexpr...}{stuff}%
}


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:

\def\foo#1#2%
{%
... some stuff ...
\myif@num{\bar=\z@}
{}
{%
\myif@num{\baz>\quux}
{}
{\expandafter\foo\expandafter{\the\numexpr...}{stuff}}%
}%
}


Much cleaner, isn't it?

-
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

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

\newcommand{\myif@num}[3]{%
\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

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


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.

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