TeX/else

From Wikibooks, open books for an open world
< TeX
Jump to navigation Jump to search

Description[edit | edit source]

The \else command is used to include the optional else construct of TeX's many if-then-else control structures which include:

The use of each of these take the form

\if*<arguments> <TeX code 1> \else <TeX code 2> \fi

where \if* is one of the commands listed above, <arguments> are all the arguments, if any, required by \if*, and <TeX code 1> and <TeX code 2> are each a piece of TeX code. The code "\if*<arguments>" either evaluates to true of false; if true then TeX processes <TeX code 1> and ignores <TeX code 2>; if false then TeX ignores <TeX code 1> and processes <TeX code 2>.

Examples[edit | edit source]

Consider the code

\ifmmode \mathcal{A} \else $\mathcal{A}$ \fi

\ifmmode tests whether TeX is in one of the math modes; it does not require any arguments, and so <arguments> is blank. If TeX is in one of the math modes then \ifmmode evaluates to true. So if the above code were used in math mode, then the expression as a whole would reduce to "\mathcal{A}", otherwise "$\mathcal{A}$".

Now consider

\ifodd2 odd \else even \fi

In this case \ifodd requires an argument that evaluated to an integer. "\ifodd2" evaluates to false, and so the TeX will typeset the word "even".