{conditionals} is about macros like \if and \ifx that may execute other macros depending on whether certain conditions are true or not. Popular packages are {etoolbox} and {ifthen}.
44
votes
4answers
16k views
LaTeX conditional expression
I would like to be able to set a flag at the beginning of the main flag (say output) that tells if I want to produce a paper document or an electronic document.
Then I should check this flag to ...
44
votes
2answers
10k views
Why is the ifthen package obsolete?
In the LaTeX companion, the authors recommend to use the package ifthen for complex conditional.
But in his answer to LaTeX conditional expression Will Robertson doesn't mention it, and in the ...
40
votes
6answers
1k views
Which document class is being used?
Is there a way to programmatically determine which document class is in effect?
I'd like to be able to write conditional macros that behave differently depending on which document class is currently ...
29
votes
2answers
3k views
What does \ifx\\#1\\ stand for?
The following usage of \ifx can be found in several packages and also in answers on this site. What does this syntax exactly mean? Are there any alternatives and drawbacks?
...
27
votes
1answer
280 views
How can I know if a node is already defined?
I think I've seen this question before, but I can't find it. The problem is simple; before I assign a name to a node I would like to know if the name is already used.
I think when a node is created ...
25
votes
2answers
910 views
Understanding \@ifnextchar
I understand the definitions of the \makeatletter and \makeatother commands and also \def and \newcommand somewhat. But, explained in a simple way, what is happening in the following line?
...
25
votes
4answers
1k views
Alternate he / she in text
This question led to a new package:
he-she
I have seen a book that instead of using the masculine pronoun (he, his, etc.) alternates the gender ('he' then 'she' then next time 'he' again).
Is ...
24
votes
6answers
2k views
Test whether token list is empty
Is it possible to test whether a token list register is empty without expanding it?
24
votes
4answers
2k views
How to check if a macro value is empty or will not create text with plain TeX conditionals?
I have a macro which holds some text value, e.g. \macro{some text}. If it is empty, I would like for nothing to appear in the document, but it has some content of any kind which would produce text ...
23
votes
7answers
2k views
Conditional typesetting / build
Let's say I have .tex file which is a set of problems and solutions, of which I want to make two pdf files, one which includes the solutions and one which doesn't. so I need to build the .tex file ...
23
votes
3answers
452 views
Is it possible to define an acronym-command which behaves differently on the first invocation than on the rest?
Most technical writers will immediately see how I'd like to use this. I'm writing a paper about something I'll call That Which Will Be Abbreviated (TWWBA). I'd like to have a macro, \TWWBA{}, that ...
23
votes
3answers
4k views
When to use @ in an \if statement
I am new to LaTeX and so this question might come across as rather basic.
It might also reflect my biases/assumptions from my C/C++ programming days.
I was seeing the code of the 'exam' class and ...
23
votes
2answers
930 views
Parametrize shading in table through TikZ
I wanted to do some shading in a table according to its values. For example, if you see the table below:
a b c d
a 90 10 0 0
b 0 80 10 10
c 0 0 95 5
d 0 10 5 85
I want to ...
22
votes
1answer
2k views
Test if a package (or package option) is loaded
[french]{babel} (aka frenchb) is incompatible with some other packages, such as tikz, since it tries to insert \thinspace before semi-colons.
In order to make these two play nice, you have to call ...
22
votes
1answer
946 views
Check if next char is {
Using \@ifnextchar one can detect if e.g. the next Character is a [, e.g.:
\@ifnextchar[%
{
%% code to execute if [ scanned.
}{
%% code to execute if [ scanned.
}
However, I can’t seem to ...
21
votes
5answers
341 views
Test if a given control sequence is a length register
I'm in need of a macro which receives a control sequence as argument and needs to branch if this macro is actually a length (of any kind, e.g. dimen, skip etc.) or "just" a normal macro (including one ...
21
votes
4answers
1k views
Test success of write18
For the new version of my package and class standalone I like to call external programs: further (pdf)latex runs to optionally compile included standalone files, and to convert the resulting PDF to ...
21
votes
2answers
2k views
Opposite of \ensuremath: ensure that I'm *not* in math mode?
I'd like a macro that is the opposite of \ensuremath: something like \ensurenotmath, which makes sure that its argument is not typeset in math mode. Is there such a macro, or a way to implement it?
...
21
votes
1answer
956 views
What is an \if?
I'm reading TeX by Topic, but in little chunks (rather than from first page to last) and just read the stuff on conditionals. I want to be sure that I understand what's going on when TeX starts ...
20
votes
6answers
2k views
Is it possible to keep my translation together with original text?
I have an English article in LaTeX, which I'm going to translate to Russian. I want to keep my translation in sync with the original text. It means that I want every paragraph to stay as close to its ...
19
votes
6answers
2k views
What is \iffalse for?
So this is something I've wondered about for a while. What use is a conditional that always evaluates to false? I guess it's a neat way to "hide code from LaTeX" but apart from that, is it ever ...
19
votes
4answers
4k views
How to conditionally define a new command in LaTeX?
I'm trying to compile a LaTeX code with an older version of some package. Thus, I'm having a few issues due to undefined commands.
How can I add conditional code so that I can define some work-around ...
18
votes
4answers
544 views
On testing two fully expanded character strings for equality
I know three macros that test two fully expanded character strings for equality:
\ifthenelse{\equal{<op1>}{<op2>}}{<true>}{<false>} (ifthen package)
...
18
votes
4answers
1k views
Redundancy in bib file: conditionally suppress url if same as doi?
Mathscinet gives bibtex entries with both a doi and a url field included. Typically they look something like
@article {some-article,
[…]
DOI = {10.1016/j.apal.2008.12.003},
URL = ...
18
votes
1answer
185 views
Check if at begin of a line
For a list of bullet-separated items, I want the separator to be swallowed if it is the first or last element of the line. That is, I want something like
\renewcommand{\and}{%
\ifnolinebreak% ...
17
votes
6answers
1k views
Checking if a number is a multiple of 4
Using etoolbox, ifthen, or another form of conditional checking, how can I check if a value is a multiple of 4? E.g. if the number is 4, 8, 12, 16, etc., then it returns "true".
17
votes
4answers
974 views
How I can automatically add a page if the total number of pages is odd?
I need automatically add a blank page (also without document header, numbering and footer) at the end of the document, if the total numbers of pages is odd. How I can do that?
And preferably without ...
17
votes
3answers
258 views
Where can I find a documentation for low-level tex conditionals like \if?
I want to understand low-level stuff like \if better. I'm not looking for some random examples off Google showing me some statements but a specification of some sort - something mean and unforgiving. ...
17
votes
3answers
371 views
Is there an \iftex4ht command, and if not, how should it be defined?
Background: I'm using TikZ to generate a load of pictures that I want to put on the web as SVGs. So I'm using the pgfsys-tex4ht.def driver and it's working very well. My gut feeling is that the SVG ...
17
votes
2answers
659 views
Check if an entry is cited multiple times
I am trying to find out whether it is currently possible to have a test in Biblatex that tests at the very first time an entry is cited whether the same entry will be cited more than once in the ...
17
votes
1answer
369 views
How does TeX handle \else and \fi in conditionals?
I have seen packages using
\def\afterelse#1\else#2\fi{\fi#1}
which TeX seems happy to accept. But on taking the first branch, isn't TeX expecting \else instead of \fi? That is, isn't the following ...
17
votes
1answer
192 views
Is there a right way to do combined conditionals in LaTeX3?
I have some LaTeX3 code and I want to add a new feature. This will involve choosing between various actions at a particular point. There are two choices to be made, and these choices are ...
16
votes
5answers
415 views
Applying an operation only to capitals
I'd like to apply a function to capitals only so that:
\emphcaps{YaWeH}
Outputs like:
\emph{Y}a\emph{W}e\emph{H}
Preferably without using too many packages (I already use memoir).
NOTES: I use ...
16
votes
4answers
904 views
Testing for number
How do I test if a given parameter is a number, in order to apply a different style? And if not, just ignore... Like:
\domorestuffifnumber{things} -> things
\domorestuffifnumber{123} -> ...
16
votes
4answers
511 views
Is this branching code I wrote a bit obfuscated?
I've been away for a while and I forget if this sort of question is appropriate, but here goes. Recently I wrote some code (in expl3 but I hope it's clear enough) and in hindsight I wonder on its ...
16
votes
2answers
2k views
How to form “if … or … then” conditionals in TeX?
I feel really stupid for asking this, but how do you form more complex if conditionals in TeX? I'm looking for something like:
\ifnum\x=1 OR \ifnum\x=14
{do this}
\else
{do that}
\fi
I ...
16
votes
1answer
108 views
Compile PDF without proofs
I am writing my degree thesis, and I need to make a synthesis of it: basically what I want to do is make a copy without the proofs...is there a way to exclude them automatically, without having to ...
16
votes
2answers
222 views
How can I detect if I'm inside or outside of a float environment?
I am trying to detect whether I am inside or outside a float. I know the way how caption works. It uses the internal \@captype. But in the example below you see that this test failed by using ...
15
votes
7answers
388 views
How to define a command to feed the contents of a list into an \ifcase construct?
It's common to use an \ifcase construct to redefine a command depending on the value of a counter. The following example illustrates this:
\documentclass{article}
\usepackage{xcolor}
...
15
votes
3answers
295 views
What's the use of the @compatibility condition?
All of the standard LaTeX document classes (article, book and report) declare some basic functionality and parameters based on the condition @compatibility. Here's an extract from from article.cls ...
15
votes
3answers
841 views
Conditionals in \newcommand
I find myself using the \newcommand macro a lot and apart from having the occasional difficulty with properly defining the optional parameter the macro works well. There is one serious limitation ...
15
votes
3answers
331 views
Includegraphics maximum width
With \includegraphics I can give the width of the resulting image (which would be scaled).
How can I give a maximum width of the image? That is, if the original is smaller than X, I'd like to keep ...
15
votes
2answers
150 views
Difference between \newbool and \newtoggle from etoolbox package
Paul Gaborit's answer to How to detect if option provided via \tikzset uses
\newbool to define a new boolean
\booltrue to set a boolean, and
\ifbool to perform a conditional operation
all from ...
15
votes
1answer
390 views
What exactly does \@doendpe do?
Quoting from the LaTeX2e sources:
[\@endparenv, \@doendpe]
To suppress the paragraph indentation in text immediately following a
paragraph-making environment, \everypar is changed to remove ...
15
votes
1answer
142 views
First occurrence of a command
I'm writing some macros that need to behave differently after first occurrence. I decided to use the etoolbox package's toggle. My MWE is as follows:
\documentclass{article}
\usepackage{etoolbox}
...
14
votes
3answers
388 views
How to make time-dependent code?
I would like to create a command \ifafter{year}{month}{day}{yes}{no} so that if the current date is after year/month/day, the command would produce yes, and otherwise produce no.
I came up with a ...
11
votes
1answer
70 views
Is something the first item on a page?
How can i identify logically, whether something is the first item on a page, i.e. there is nothing above it (apart from header)?
Consider the following, is is basically a feature environment, which ...
8
votes
0answers
138 views
How to detect whether “I'm on arXiv.org”?
I write a class. I'm interested in having a different page headers (and eventually make some other small differences) based on whether the class is used on the site arXiv.org or not. Minimal example ...
7
votes
1answer
47 views
Scan argument for parentheses
I'd like to define a macro which expands to (#1) if #1 does not contain any parentheses, but expands to \big(#1\big) if it does.
I'd choose simplicity over generality: if a solution works in the most ...
3
votes
2answers
222 views
ifthenelse conditional & macro
Why I get the error below if I use the
\ifthenelse{\boolean{@parts}}%
{%
}{}
conditional (defined into classicthesis.sty) you try to comment it, you don't have any error?
...

