LaTeX programming: how to implement conditionals
August 29th 2007

I have recently come across a problem while creating a LaTeX style (for making A0-size posters). Maybe it could be avoided or solved more elegantly, but I wanted to solve it with conditionals.

Basically, what I wanted to do was define a command (actually, an environment) that accepted one argument, and make it return different output, depending on the argument:

if (argument equals something) then
  do something
  do somethingelse
end if

It gave me some headaches to get it, but I also learned some interesting things on the way. There are at least two ways of playing with conditionals: defining boolean variables or directly using logical comparisons.

Defining logical valiables

We can define a logical variable logvar as follows:


By default, it is set to false. We can set it to true by:


and back to false by:


The variable can be used in a conditional as follows:


You can think of the above code as a single object, the output value of which will be "aaaa" if logvar is true, and "bbbb" if false. Basically, the following code will, thus, output "Today is great":




  is \textbf{great}

Direct logic comparison

The example I provide works for numbers, but check this page for more info. Recall that LaTeX works with integers (counters) and text strings. As far as I know, floating point operations are impossible in LaTeX (nothing is actually impossible in LaTeX, just veeery difficult).

For example, defining the following command in the preamble:

    number #1 is 3
    number #1 is not 3

allows us to call it in the document, so the following outputs "We know that number 33 is not 3":

We know that \isitthree{33}


Obviously the conditionals can be nested (put one inside another), when more than one condition needs to be tested. For example:




    almost sucks.
    is \textbf{great}
Tags: , , , ,


5 Responses to “LaTeX programming: how to implement conditionals”

  1. LaTeX programming: how to implement conditionals « handyfloss on 13 Sep 2008 at 15:58 pm #

    [...] Entry available at: [...]

  2. christopherolah on 19 Feb 2009 at 23:48 pm #

    Just left this reply at your old blog... now I'm leaving it here

    As you said, floating points are difficult to handle in LaTeX. Thankfully, there are packages that make it easy: fltpoint - The package provides simple floating point operations can be found a long with other packages useful for LaTeX programing in the CTAN calculating section.

  3. AprilCoolsDay on 16 Apr 2009 at 18:25 pm #

    For people who prefer latex-like coding instead of tex-like coding, ifthen package is there.

    \ifthenelse {\boolean{sunday}}
    {This is sunday} {this is no sunday}

  4. isilanes on 17 Apr 2009 at 8:45 am #

    Thanks AprilCoolsDay!

    It is comments like yours that add real value to a blog.

  5. Joost on 30 Nov 2010 at 23:20 pm #

    Really helpful, thank you. However I was having issues with the scopes of the variables. In a command, setting a conditions-variable will only take effect in that scope, not outside. It was quite hard to find the solution to this, however it is really easy:


    \global\ismondayfalse % Set \ifismonday to false, in the global scope
    % \ifismonday has properly been set tot false, even outside the command

Trackback URI | Comments RSS

Leave a Reply

Subscribe without commenting

« | »

  • The contents of this blog are under a Creative Commons License.

    Creative Commons License

  • Meta