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 ?
2 Answers
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.
- 603,163
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
storndorrdas 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.
- 757,742
comment– Werner Jul 17 '18 at 19:06documentenvironment should be rare, but conditionals are a very important tool in macro definitions. – egreg Jul 17 '18 at 19:52