Questions tagged [conditionals]

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

Filter by
Sorted by
Tagged with
1
vote
2answers
4k views

How to rotate image 90 if height overful?

I think the approach can be with adjustbox but not sure. Pseudocode: If height overfull, turn the image 90 degree clockwise, apply all images the width constraint now Do not pass max width (= \...
19
votes
1answer
2k views

testing for a documentclass option

I know that there are lots of similar question, but none of those have helped me so far - and I did google a lot. So my question is: I have a document class (emulateapj) which accepts a few options. ...
19
votes
1answer
1k 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 ...
14
votes
4answers
4k views

How to make a “condition” in a macro?

I would like to make a macro with two parameters which return a text. For instance, I want \M{1}{5} to return [1,5], and \M{2}{2} to return [2] (because the two arguments are the same). So I need to ...
11
votes
1answer
1k views

Hiding section titles when the section is empty

I have created a macro like this: \newcommand{\showsection}[2]{ \ifstrequal{#2}{}{}{\section{#1} #2} } It should be used like this in the document: \showsection{Books}{ Do you know any good ...
10
votes
1answer
342 views

querying whether a font feature has been activated via a fontspec command

A LaTeX package I'm currently writing makes use of -- and definitely requires -- some of the power of LuaLaTeX. I would like to make my package's user interface -- in particular, the options that need ...
10
votes
2answers
591 views

tikz, conditional setting of shape parameters

In my illustration I use simplified sketches of stopwatches (timers), which appear in three different "modes": (1) start of interval (coloured green), (2) time in progress (yellow), and time is ...
9
votes
1answer
989 views

How do I configure fonts and package options for different engines?

I would like to be able to rely on the code in my document class to respond correctly to whether it is being rendered using pdfLaTeX or XeTeX engines. The editor I'm using (LyX) does this using ...
9
votes
2answers
589 views

conditional against font color

I want to change the text according to its current font color. I can test argument against font family \documentclass[a4paper,12pt]{article} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \...
9
votes
3answers
674 views

Defining tabular rows with macros using `\ifnum` leads to `Incomplete \ifnum`

I am trying to define tabular rows using a macro which shall perform an conditional definition of the tabular row according to a given parameter. Please consider the following MWE: \documentclass{...
8
votes
1answer
2k views

How to tell if the current section is defined in Beamer?

I am customising the footer of my Beamer slides. The footer displays the title of the slides, followed by a ":", and then the current section title. This works fine if the current section is defined. ...
7
votes
1answer
1k views

Conditional compilation of beamer slides

I'm preparing a beamer slideshow, and I will have to print slides. I would rather use landscape mode slides, but it is possible that I need to use portrait mode ones. Sadly, I won't know until a few ...
7
votes
1answer
1k views

etoolbox's iftoggle vs verbatim

My problem is that I can set up toggles, using etoolbox's \newtoggle{x}, set it to true with \toggletrue{x} and even use it with iftoggle{x}{this for true}{this for false}. However, if I try to put a ...
6
votes
2answers
534 views

How to use the total page count in a conditional expression?

I would like to have different footer or page style depending on whether the document has one or more pages. I am not very experienced with TeX programming, so the answers about "Comparing an argument ...
5
votes
3answers
616 views

How to change the output of a command if it has been called already?

I want to create a command that changes its output the second time it gets called. For example, I want it to yield 1 the first time it's called, and 2 the second time: \newcommand\mycommand { do ...
4
votes
2answers
140 views

Is is possible to conditionally switch languages within one document? [duplicate]

I'm quite new to biblatex. I'm typesetting a collection that has papers both in English and Russian, and I'm making a single bibliography at the end of the book. For the bibliography to look pretty, ...
3
votes
1answer
1k views

Conditional formatting, rounding and postprocessing of a column in pgfplotstable

I want to round p-values in a table to two decimals (for example 0.05), have values less than 0.05 in bold font and have values less than 0.01 to come out as '<0.01' in bold font. Like this (the ...
15
votes
2answers
907 views

How to tell when to use pdflatex, latex, and xelatex for any given tex file?

I'm writing an automatic compiling pipeline that should be able to compile any given tex file into a pdf. The diversity of the input tex files mainly shows up in what format of figures they use and ...
13
votes
3answers
2k views

Do all \if's need to be defined before a nested if?

I have this MWE, which does not compile (Extra \else, Extra \fi) unless I load the showlabels package before the outer \if. (\ifSL@AMS is defined in showlabels.sty.) Is that a general requirement that ...
12
votes
3answers
32k views

How to avoid empty 'thebibliography' environment (bibtex) if there are no references?

I have a general and central preamble for a lot of similar-style documents (several dozens) and most of the documents have a Bibliography at the beginning, so I call \bibliographystyle{alphadin} \...
11
votes
1answer
2k views

Conditional using non-integer numbers

I am trying to generate fractals using affine transformations. I need to generate a random number in the interval (0,1), and depending on the value of this random number choose a function to ...
10
votes
1answer
961 views

Testing lines read from external file

I am trying to read some data from an external file and perform specific actions based on the contents of a given line. Below is my attempt to test a read line using \ifx - it does not work. I've ...
9
votes
1answer
192 views

How to use \ref command in the test block of \ifnum?

Is there a way to use \ref{} inside the test part of \ifnum. The following example runs into an error. \documentclass{article} \begin{document} \begin{table} \caption{ex} \label{tab:ex} \...
8
votes
3answers
572 views

What is wrong with \ifnum#1=0.09\relax?

The following code does not compile with the following error message Missing = inserted for \ifnum \documentclass[pstricks,border=4pt]{standalone} \usepackage{multido} \psset{linewidth=.5\...
8
votes
1answer
730 views

Conditional page breaking

I have used LaTeX on and off for a number of years now for several large documents with evolving versions over long lifetimes. But every time it seems like, powerful and wonderful as LaTeX is, there ...
7
votes
2answers
1k views

problem with \ifthenelse + multicolumn

I encounter various problems when i want to control the display of a table with a ifthenelse test. For example, consider the following M(non)WE : \documentclass{article} \usepackage{multirow} \...
6
votes
2answers
4k views

Expand away empty macros within ifthenelse

I want to test whether a string, when it is finally printed, is empty. The following illustrates my problem, \documentclass{article} \usepackage{ifthen} \newcommand{\isempty}[1]% { \ifthenelse{\...
6
votes
2answers
238 views

Separators for author group (IF conditon)

I am new to LaTeX, just a beginner, basically a Perl developer. I have created a LaTeX file \begin{document} \cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\...
6
votes
2answers
177 views

Is “conditionals” name of package?

In some package I have seen construct like this one: \if\blank{\myMacro}% %do something \else %do something else \fi I like it and I want to use it to test if call to a macro has empty result. I ...
5
votes
2answers
466 views

Detect whether I'm on a left- or right-hand page? [duplicate]

How do I detect whether I'm on a left- or right-hand page? I would like to define a custom command with a conditional statement that executes a certain command if it's on a left-hand page and another ...
5
votes
3answers
121 views

Modified \IfStrEqCase with multiple matches per case

I often use xtring's \IfStrEqCase for conditional processing. This works great, but wondering if there is an easy way to enhance the current behaviour and allow to group mutliple cases into a single ...
5
votes
2answers
377 views

Enhanced fill-in-the-blank document

I am about to improve my lecture slides from last semester. Specifically, I would like to add a fill-in-the-blank option. I found this code suggestion (from here): \documentclass{article} \...
5
votes
2answers
77 views

filecontents: select rows of group to display

I have a file that contains 1,000 rows of product data. I want to select only a specific product type to print in my document. Minimal code in main.tex: \documentclass{article} \usepackage{...
5
votes
1answer
676 views

How to check image exists or not?

I'm working on Sweave and using includegraphics for include image in report dynamically. Pseudocode: <<> for ( i to 100) # run loop 1 to 100 { includegraphics(image....
4
votes
1answer
194 views

Why does this test with ifthenelse not compile?

Why does the following test using \ifthenelse not compile: \documentclass[border=30pt, tikz]{standalone} \usepackage{tikz,tikz-3dplot} \usepackage{ifthen} \usepackage{animate, calc}%%%%%%%%%%%% \...
4
votes
3answers
446 views

Show “Page X of Y” only if there are multiple pages?

I'm trying to add a line in the header to be "Page X of Y", but only if there are multiple pages. My main problem is how to format the if statement. So far I have \ifx \pageref{LastPage} 1 \...
4
votes
2answers
141 views

How to test if a given reference belongs to a list of labels?

This question is a prolongation of my previous question How to choose from a list for using result in \ref?. I got really good and interesting answers. Unfortunately I can not implement them for my ...
4
votes
1answer
93 views

How to define a style of undefined key choice? [duplicate]

I would like to implement something like default in a switch statement, but for .is choice pgf key. I know that there is an error key handler /errors/unknown choice value but I don't know how to use ...
3
votes
2answers
183 views

How to test whether report has been called with “draft” option

How can I test within a document, whether or not the initial \documentclass[a4paper,11pt,twoside,fleqn,draft]{report} includes the "draft" option? My naive attempt \newif\ifmydraft \mydraftfalse \...
3
votes
1answer
690 views

How to compare two counters using TeX conditionals in ConTeXt?

I have two counters, \definenumber[words] and \definenumber[items]. I have tried to use TeX conditionals to compare them, e.g.: \ifnum\getnumber[words]=\getnumber[items] This will print some text ...
2
votes
1answer
426 views

Nested conditional glossary terms using Glossaries \ifglsused in \newglossaryentry

If I want to conditionally expand a glossary entry based on or related to another glossary term, I find the conditional \ifglsused{}{}{} statement is very helpful. To satisfy parent modifications ...
2
votes
1answer
603 views

When using \IfEq to compare strings, is it one of those times when I should use an expandable xparse command?

I recently asked How can I migrate commands and environments from LaTeX2e to LaTeX3? [XeTeX] While that question is good, the answer is likely quite complex and would take a some time to effectively ...
2
votes
1answer
30 views

Printing the bibliography conditionally by hooking \cite to toggle a boolean

I want to print the Bibliography slide only if there were any citations in the presentation. To that end I have redefined the \cite command to include the toggling of a boolean. I tried multiple ...
2
votes
1answer
67 views

Indentation of footnotes depending on number of digits in footnote marks on the same page

I wanted to adjust the left indentation of footnotes depending on the number of digits of the footnote mark. Thus, footnotes with one digit are aligned a the length of single \parindent (which is set ...
2
votes
1answer
85 views

Format cells if they contain a specific symbol with siunitx

In my previous question, David Carlisle gave a great solution for coloring all text in a cell when the cell contains an \*. I played a little bit with this code, but couldn't include it, if the ...
1
vote
1answer
475 views

Check the value of a pgfkey

I need to be able to check if the value of a pgfkey is empty or not. A solution I have tried (using package etoolbox): \edef\temp{\pgfkeysvalueof{/path/key}}% \expandafter\ifstrequal\expandafter{\...
1
vote
2answers
341 views

Conditional \endinput

I want to prevent processing rest part of a package in case if it is loaded not from lualatex. For some reason I cannot load ifluatex package, so I borrowed definition of \ifluatex from that package ...
1
vote
2answers
402 views

Membership check on comma separated list [duplicate]

Does anyone know an easy way to check if an item is a member in a comma-separated list? Something like this would be excellent: \ifmember{elem1}{elem1,elem2,elem3}{true}{false} The above would ...
0
votes
0answers
83 views

Conditional Latex table: Cell value color based on its value

I would like to create a conditional where values less than 7 are in red and values greater than or equal to 7 are in blue. I would like a result like the one below: Follows my MWE. \documentclass[...
28
votes
2answers
22k views

Variables for hiding or showing text in Latex

I would like to have some variables for showing or hiding text in Latex. For example, I would like to have two versions of a document. A short version and a long version by changing a variable at the ...