If, elseif and else conditionals in the preamble
As in almost any programming language, \if...
statements can be nested, however, there's no \elseif
, so \else \if....
has to be used and concluded with \fi
.
A new \if...
variable (well, macro actually) can be defined with
\newif\ifsomename
which is initially set to the false state. \somenametrue
will set to true
, \somenamefalse
will set it false.
Nested \if...\fi
can be very tedious, in TeX, however.
Here's some pseudo-code
\ifsomething...
do this and that
\else
\ifsomethingother
...
\else
\ifsomethingmore
\fi
\fi
\fi
The following small example should print 'Foo that' with bold font, since \dothattrue
was specified.
\documentclass{article}
\newif\ifdothis
\newif\ifdothat
\dothattrue % Setting the state of the boolean variable \ifdothat` to true, otherwise it's false already (or use \dothatfalse explicitly)
\ifdothis
\newcommand{\foo}{\textbf{Foo}}
\else
\ifdothat
\newcommand{\foo}{\textbf{Foo that}}
\else
\newcommand{\foo}{\textbf{Dummy content}}
\fi % \fi of \ifdothat
\fi % Outer \fi
\begin{document}
\foo
\end{document}
Multiple conditions can be handled with plain TeX conditionals using \ifnum
, for example
\ifnum0%
\iffirstsomething 1\fi
\ifsecondsomething 1\fi
>0 %
% Do stuff
\fi
which will only do the stuff if at least one condition is met. Similarly
\ifnum0%
\iffirstsomething\else1\fi
\ifsecondsomething\else1\fi
=0 %
% Do stuff
\fi
will be true only if both conditions are met.