{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}.

learn more… | top users | synonyms (1)

94
votes
4answers
34k 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 ...
54
votes
2answers
17k 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 ...
49
votes
6answers
2k 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 ...
37
votes
2answers
6k 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? ...
36
votes
4answers
5k 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 ...
33
votes
2answers
2k 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? ...
32
votes
6answers
4k 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 ...
32
votes
1answer
3k 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 ...
31
votes
7answers
6k 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 ...
31
votes
3answers
2k views

Difference between \if and \ifx

I try to understand the subtleties of TeX programming, but it is not always easy. For example, in the following code : \def\first{abc} \def\second{abc} \ifx\first\second OK!\else false \fi I ...
29
votes
1answer
398 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 ...
27
votes
6answers
4k views

Test whether token list is empty

Is it possible to test whether a token list register is empty without expanding it?
27
votes
4answers
6k 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 ...
26
votes
3answers
2k 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 ...
25
votes
3answers
7k 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 ...
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 ...
25
votes
2answers
3k 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? ...
25
votes
4answers
2k 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 ...
24
votes
4answers
2k 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) ...
24
votes
3answers
485 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 ...
24
votes
2answers
4k 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 ...
24
votes
1answer
1k 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 ...
23
votes
2answers
11k views

Conditionally hidden slides in beamer

When creating a presentation, I sometimes create extra slides that contain additional information, a more thorough explanation, or an extra plot pertaining to certain parts of my talk. These extra ...
23
votes
1answer
1k 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 ...
22
votes
6answers
4k 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 ...
22
votes
5answers
351 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 ...
22
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 = ...
22
votes
4answers
536 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 ...
22
votes
1answer
563 views

Use of \relax after \ifnum … \fi construction

I'm reading Malcolm Clark's A Plain TeX Primer. On page 111 there's an exercise to define a macro that prints the current time. I've reproduced Clark's answer below: \newcount\minleft ...
21
votes
5answers
2k 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 ...
21
votes
4answers
3k views

How can I check in LaTeX (or plain TeX) whether a command exists, by name?

I'm looking for a LaTeX way to control macro expansion, contingent on some macro being defined or not. I'm looking for something like this (but then actually working): \newcommand[1]{\checkfor}{ ...
21
votes
2answers
373 views

Show footnotes on odd pages only

I'm writing my thesis using the scrbook-class. Is it possible to display footnotes only on the odd (right) pages? This should mean that \footnote{something} should be collected on even pages and ...
20
votes
4answers
2k 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} -> ...
20
votes
1answer
122 views

Is implicit `\relax` in conditionals explained anywhere in the TeXbook?

I have been relying for years on TeX's behavior where, say \if1\fi expands to nothing and \if1\else\fi does the same. In the second case TeX inserts an 'implicit' \relax when it tries to expand \else. ...
19
votes
3answers
3k views

What is the difference between \ifx\somecommand\undefined and \ifdefined\somecommand\else?

What is the difference between \ifx\cyrdash\undefined and \ifdefined\cyrdash\else? \cyrdash is any macro.
18
votes
7answers
659 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} ...
18
votes
6answers
9k views

If Then Else for odd page/even page

How to use if-then-else structure in LaTeX? I need a example of odd and even page. if odd then command 1 else command 2
18
votes
2answers
2k views

Use “default” figure if file is missing?

I was recently in charge of organizing all of the references and labels in a very large scientific proposal document. My workflow typically involved compiling the document, seeing a "There were ...
18
votes
2answers
2k views

What does \begingroup\expandafter…\endgroup do?

I often see code of the following form in package implementations (this example is from the LaTeX3 sources): \begingroup\expandafter\expandafter\expandafter\endgroup \expandafter\ifx\csname ...
18
votes
1answer
248 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 ...
18
votes
2answers
785 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 ...
18
votes
1answer
353 views

How to check if a variable contains a word in plain TeX?

I have a variable that contains a long list of keywords, e.g.: \keywords{computers, business, biology, fine art, literature, zoology} I need to make an if-then statement that checks for the ...
18
votes
1answer
267 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% ...
18
votes
2answers
333 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 ...
17
votes
6answers
2k 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
3answers
807 views

Conditionally Remove Brackets

I'm trying to create a command with an optional argument using \newcommand{\Af}[1]{{\mathbf{A}_{(#1)}} where the argument is a number. However, I'd like that if no argument is given the bracket ...
17
votes
6answers
962 views

Placing many conditionals in a series

I frequently need to do something like this: IF x < 1, DO a ELSE IF x < 2, DO b ELSE IF x < 3, DO c ELSE, DO d Using etoolbox, I end up nesting a lot of ifnumless, like this: ...
17
votes
5answers
805 views

Combining \ifxetex and \ifluatex with the logical OR operation

I want to write in the preamble of a LaTeX document code that should be take into account by different compilers when the document is typeset. There are fragments that should be used: only by ...
17
votes
3answers
1k 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 ...
17
votes
2answers
1k 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 ...