I'm currently working on a presentation about the packages mentioned in the title. The problem I have is, I don't really understand why anyone would use conditional statements in a LaTeX document. What is the advantage of using such statements? Could you give me a real world application, something where they are actually helpful for the "mainstream" LaTeX-User ?

  • 1
    A quick example includes when someone writes an exam that includes the answers with every question. Each answer is written using a specific environment, which allows one to conditionally turn them on or off, depending on whether you want to create the exam with/without answers. In essence, a single document source that could create different outputs via some condition that the user can set. Related package: comment
    – Werner
    Commented Jul 17, 2018 at 19:06
  • Ahh so I could use that for making one document and then compiling it into two or more different versions of itself by just changing one value? And say something like (pseudo code): if nbr == 1 then ... if nmbr == 2 ... and so on and so forth. Am I heading in the right direction ? Commented Jul 17, 2018 at 19:12
  • Yes, that's it. You use conditionals to determine what properties the output needs to have/not.
    – Werner
    Commented Jul 17, 2018 at 19:23
  • Direct usage in the document environment should be rare, but conditionals are a very important tool in macro definitions.
    – egreg
    Commented Jul 17, 2018 at 19:52
  • What are macros ? I'm sorry but as you can see I'm very new to LaTeX. Commented Jul 17, 2018 at 20:00

2 Answers 2


Whether or not you use the \ifthenelse syntax there are all kinds of tests that one might do.

You could for instance:

  • test if the height of an image is larger or smaller than its width and then decide to rotate the image or set it on a landscape page, or wherever else is appropriate

  • set a boolean flag in the preamble and then conditionally include parts of a document, eg a presentation with or without notes or a question sheet with or without answers.

  • make trial typesettings of some text in a box and then test the resulting size (or line breaking badness or whatever) and repeat the tests with different settings, eg smaller font sizes, before finally using one box.

  • You can make tests and vary the presentation of counters so for example make a cross reference say section 3.2 if you are not in chapter 3 but just section 2 if you are

  • You can test the \day and use st or nd or rd as appropriate to make 17th June etc.

many of these tests are available as specific package commands eg varwidth for trial settings, or varioref for variant references and several packages for date formatting, but it's always possible to think of something to test which doesn't have a specific command defined in a package.


Conditionals are used throughout the LaTeX kernel to aid in the typesetting of sectional units and more. The availability to the end user often allows one to create documents that can be altered with a change in a variable. That is, rather than creating two separate documents, each having basically the same content with only minor changes here and there, you can create a single document that conditions on its contents based on a variable.

As a practical example, consider someone writing up an exam for their students. They write the entire examination as a set of questions and answers, each answer being written inside an answer environment (say) immediately after the corresponding question. Now you can use the comment package to print the document without the answers (using \excludecomment{answer}) which prepares the actual exam, and with answers (using \includecomment{answer}) when you want a complete solution.

Another example comes from the cross-referencing package varioref that allows the user to specify \vref{<ref>} in such a way that the reference changes depending on its location relative to the referenced object; "see the section above" or "below" rather than some absolute reference "see section 1.2".

Another example from the LaTeX kernel: Printing integers to always have two digits using \two@digits. Here's the definition from the kernel:

\def\two@digits#1{\ifnum#1<10 0\fi\number#1}

Read as pseudocode, \two@digits{<num>} first checks (conditions) whether <num> is less than 10. If so, it prints 0, then it prints <num>. This ensures that numbers less than 10 will be preceded by 0. This conditioning allows for consistent typesetting when width of numbers are required, say. Of course, it could be used in other environments as well.

The ifthen and xifthen packages are considered obsolete since there are other options available, but the principle remains the same.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .