Because FLOSS is handy, isn’t it?

LaTeX programming: how to implement conditionals

Posted by isilanes on August 29, 2007

Blog moved to: handyfloss.net

Entry available at: http://handyfloss.net/2007.08/latex-programming-how-to-implement-conditionals/

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}

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

  1. Sam said

    An excellent post! Thanks for the information on doing conditionals – it’s a handy way to only hand in part of my draft thesis to my supervisor.

  2. Great. This is just what I needed for some documentation for our software base. We have a compile time configuration system which can spit out configuration variables in a different formats. I considered using the C preprocessor to modify the .tex files, but now I can just make an output module for the configuration system which spits out .tex files with the variables and do the inclusion/exclusion in text directly. Thanks.

  3. Jan K. said

    Thanks for the post. Appretiate it.

  4. jb said

    This is exactly what I needed to know to set up a template for student recommendation letters that can quickly switch between printing onto hardcopy letterhead and a fully PDF letter with letterhead and signature included.

  5. christopherolah said

    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

  6. bangKai said


  7. Tony said

    This post has been bestowed the honor of being bookmarked. 🙂 Excellent and extremely useful information.

  8. Soab said

    you know the ifthen package, dont you?

  9. Sruthi N.Paul said

    Thanks Isilanes.
    it helped me a lot in my programing.

  10. imipak said

    I would not personally recommend your method for complex tasks, although it IS certainly great for simpler conditionals. (First rule of programming: never use a more complex function than you need – you risk adding in side-effects and bugs). For the examples you give, and that kind of level of work, what you’re using is ideal.

    For more complex tasks, you need something with a bit more oomph. However, the previous commenter was not only sarcastic in their reference to ifthen, but they were also rather foolish. Ifthen is considered obsolete, it doesn’t play well with other packages (exactly the kind of side-effects I was meaning), it’s not good with null arguments and it’s not maintained. xifthen is the package of choice if you need ifthen-like capabilities, although many robust if/then/else packages now exist.

    If you’re wanting something with far more power still, the Lua scripting language is now available in some forms of LaTeX. (If you’re using TeXLive or a similar distro, you’ll have a LaTeX compiler that supports it.) I am not fond of Lua, but it does give you an enormous level of additional control over documents.

    In other words, your choices are limited by how long you want to search.

  11. We absolutely love your blog and find most of
    your post’s to be exactly I’m looking for.
    Do you offer guest writers to write content to suit your needs?
    I wouldn’t mind publishing a post or elaborating on a number of the subjects you write related to here. Again, awesome web log!

  12. Pretty nice post. I just stumbled upon your blog and
    wished to say that I have really enjoyed browsing your blog posts.

    After all I’ll be subscribing to your feed and I hope you write again soon!

  13. A very good example! I will be using it to choose package by declaring conditionals using \DeclareOption, as we cant \usepackage inside \DeclareOption.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: