{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)

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 ...
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 ...
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 ...
15
votes
2answers
1k views

How to programmatically make tabular rows using `\whiledo` ?

The following code snippet does not work: \documentclass{minimal} \usepackage{longtable} \usepackage{ifthen} \newcounter{xyz} \setcounter{xyz}{0} \begin{document} \begin{longtable}{|*2{c|}} \hline ...
35
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 ...
10
votes
2answers
996 views

Test if a paragraph has a page break in it?

I'm trying to use TikZ to make fancier frames. I'm using the clever \tikzmark thing that Andrew Stacey wrote to put nodes at the top left and bottom right of the body of an environment. Then I use ...
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 ...
17
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
5
votes
1answer
632 views

Changbars to indicate location of conditional text

I am using etoolbox as per this question on LaTeX conditional expression, and would like to modify the conditional to print some sort of indicator (like a vertical change bar in the margin) to show ...
29
votes
1answer
397 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 ...
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 ...
14
votes
2answers
6k views

large braces for specifying values of variables by condition

How do I typeset something like, X = 0 if a=1, 1 otherwise, using those huge left braces and specifying each condition in a line?
13
votes
5answers
762 views

Command behavior depending on current environment

I would like to write a LaTeX macro whose code depends on the parent environment such as: \newcommand\test{ %if current_environment=env1 test1 %elseif current_environment=env2 test2 %else } ...
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? ...
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 ...
13
votes
2answers
738 views

Conditional “if a package is available”

I'd like to do something like the following (in pseudo-TeX) \ifpackageavailable{packagefoo} \usefoo \else \usefooreplacement \fi For example, I know that a template file I wrote is used by people ...
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 = ...
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 ...
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 ...
15
votes
3answers
1k views

Detect beginning of a sentence in a macro for capitalization

Is it possible to make macros give different results at the beginning of a new sentence? Suppose that I want the macro "\secname" to write "Section" at the beginning of a new sentence, and "section" ...
14
votes
2answers
1k views

How can I test for the current font?

The situation is the following, I have one font a with one feature and another font b with another feature and I am writing a command that if the current font is a need to switch to font b but if the ...
13
votes
2answers
968 views

Define a Macro via Macro if Given Macro is not defined

I want to define a macro if it is not already defined. I can do this inline as the example below shows. But I would like to have a macro to do this functionality for me. My attempt to make this work ...
12
votes
3answers
718 views

Is there a way to detect from inside a package that MiKTeX is used?

Is there a way to detect that the MiKTeX TeX LaTeX executable is used? Something like \ifmiktex or even \IfFileExists{<some file only present in MiKTeX>}{}{}? The reason is that in one of my ...
5
votes
2answers
366 views

biblatex conditional for printing an extradate

This is a follow-up question to biblatex format for online sources. I have the following biblatex configuration file. Thanks to Guido for the inputs. I have also taken some code for the ...
10
votes
3answers
559 views

Check for empty macro argument

There is a description on how to check whether the argument of a macro is empty in What does \ifx\\#1\\ stand for? Unfortunately, I can't get any of the examples there to work. Among others, I have ...
10
votes
5answers
2k views

How to test if a number is negative

Is there an easy way to test if a number is negative? I don't need a general solution, but it does need to be able to handle a floating point value. I was using this solution from tex.ac.uk until I ...
15
votes
5answers
2k views

Proper way to detect empty/blank text

I am attempting to ignore a command if it is passed with empty/blank text. My use case is not as complicated as one Expand away empty macros within ifthenelse where @egreg provides what seems like a ...
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} -> ...
22
votes
4answers
534 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 ...
13
votes
1answer
477 views

Column break only if within first column of multicol

Background Creating books automatically using LaTeX-escaped user-generated content. Problem The book layouts make use of the multi-column package, with the book content separated into sections. If ...
16
votes
1answer
481 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 ...
11
votes
2answers
2k views

Check if a string contains a given character

I want to implement an if-then-else depending on whether the argument contains a special character or not. Should I be looking here: http://www.tug.org/TUGboat/Articles/tb28-1/tb88glister.pdf, or does ...
11
votes
2answers
590 views

How can I check if the current code is inside a certain environment?

I would like to define a command which checks if it is executed inside a certain environment. Like this: \documentclass{article} \newenvironment{myenv}[0]{at start}{ at end} ...
4
votes
2answers
563 views

how do I use the \input command on windows, combined with \IfFileExists?

I'm trying to use a command in my LaTeX file that inputs a file if it exists, and if it doesn't exit, inputs another one. This is the command: ...
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?
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) ...
11
votes
2answers
4k views

Conditional cases expression

How can I create a command \if with 1 argument, namely #1, \if{#1=1}{symbol1}...{#1=n}{symboln} that returns symbol1, when #1 is 1, ..., and returns symboln, when #1 is n, and returns nothing, ...
15
votes
1answer
351 views

Conditioning behaviour of a macro on whether on top of a new page

Is there a way to find out, whether the currently typeset material will go to top of a page? I am trying to write a macro which, when invoked, typesets a horizontal divider to separate the subsequent ...
13
votes
1answer
212 views

Formatting floats differently based on placement

I'm interested in changing how floats are formatted depending on whether they are placed at the top, bottom, or middle of a page, or on a float page. One package I am aware of that does something like ...
18
votes
2answers
784 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 ...
14
votes
2answers
318 views

String comparison in \ifthenelse

I want find a certain line of text in a data file cities.txt: A;London B;Berlin C;Paris with the xstring and [xifthen][2] packages: \documentclass[12pt, a4paper]{article} \usepackage{xstring} ...
11
votes
3answers
2k views

Hide custom environment content based on boolean

I have a custom environment which adds some text around its content, but I also want it to be able to show or hide its contents depending on a boolean. I have tried doing the following: ...
11
votes
1answer
419 views

Loading a package conditionally

Is it possible to load some package from a .sty file only if some option is selected? (Naive approach \DeclareOption{something}{\RequirePackage{something}} doesn't work.)
18
votes
2answers
332 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
3answers
419 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 ...
12
votes
3answers
670 views

How to check if the value of a parameter is a number?

I have a command with a parameter. I want to check if its value is a positive number and perform actions based on that. \newcommand{\mycommand}[1]{ \ifnum#1>0% %some actions \fi } ...
11
votes
1answer
183 views

How can I check if the current code is inside a tikzpicture?

I read the answer for this question (inside a environment) but I would like to know if there is something more specific. LaTeX keeps the current environment in the macro \@currenvir but how do the ...
11
votes
1answer
281 views

\ifmmode doesn't seem to work correctly inside an array environment

I’m struggling with getting \ifmmode to work correctly when used inside an array environment which is inside a displaymath environment. Here is a minimal example: \documentclass{article} ...