5

I have a scenario where I need to define a few commands and environments based on some condition in the preamble. I used to use \newtoggle, set the conditional using \toggletrue or \togglefalse and use \iftoggle macros defined in the etoolbox package. However, the conditionals that I have now are not bound by the boolean type. It conforms to the below if-else structure

if <condition 1>
    define commands <a1, a2>
    define environments <b1, b2>
elseif <condition 2>
    define commands <a3, a4>
    define environments <b3, b4>
.. 
else
    define commands <p, q>
    define environments <x, y>

I tried using the algorithmic package, but apparently, I cannot make use of that in the preamble. Is there any way I could define multi-conditional if-else cases in the preamble?.

Update 1 The command and environment names in the different cases will be the same, just the definition will be different

Update 2 The conditions are multiple toggles. I plan to use the \newtoggle commands to define the conditional variables.

\newtoggle{variable1}
\toggletrue{variable1}
\newtoggle{variable2}
\togglefalse{variable2}

The pseudo code for the conditionals are as follows

% The conditions
if variable1 && variable2
    % do something
else if variable1 && !variable2
    % do something else
else if !variable1 && variable2
    % do the thing
else 
    % do the other thing

I know this can be done using nested if-conditionals, but I was wondering if there are any non-nested ways to do it.

  • 1
    Do you mean the \if...\else...\fi syntax? For numeric (i.e. integer numbers conditions) there is \ifcase ... – user31729 Sep 4 '15 at 17:35
  • 1
    Can you explain the conditions? That is, what are <condition 1>, <condition 2>, ...? – Werner Sep 4 '15 at 17:38
  • @Werner Updated the question – Some guy Sep 4 '15 at 17:47
  • 2
    please see texdoc.net/texmf-dist/doc/plain/texbytopic/… – touhami Sep 4 '15 at 17:48
  • @Rakesh: As far as I know, you can't use toggles together in a row with && etc. – user31729 Sep 4 '15 at 17:52
11

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}
  • This functionality is what I was looking for. However, I guess there is no escaping nested conditionals in case of multiple cases. – Some guy Sep 4 '15 at 17:57
  • @Rakesh: No, not with \if... and toggles from etoolbox. You could use \AND etc. operators with xifthen boolean variables – user31729 Sep 4 '15 at 18:00
6

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.

  • This is a nice way to do it as well! – Some guy Sep 8 '15 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.