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

22
votes
5answers
348 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 ...
16
votes
1answer
639 views

Conditional pagebreak?

Is there a way I can have LaTeX insert a pagebreak, if the last line of the page is a certain string? e.g. \noindent \textsl{Solution}: thanks very much!
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 ...
16
votes
5answers
722 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 ...
16
votes
5answers
455 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 ...
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
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 ...
17
votes
6answers
872 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: ...
15
votes
1answer
176 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} ...
15
votes
2answers
212 views

Is there a way to determine if there is a float on a page?

I am busy developing some code to draw layouts. Part of the code overlays the layout as shown in the images below: The black arrows to the side of the images, denote the \topfraction and ...
14
votes
2answers
702 views

Custom quotation style which depends on length of quotation

I am writing my PhD-thesis and I need to meet certain formatting requirements in regard to quotations. I know there is the quote environment. But in this conditions it doesn't match my universities ...
15
votes
3answers
590 views

Redefine command when inside a specific other command possible?

How, in general, can I (re)define commands based on in which other command they are nested? A more specific example: I have a custom latex command (say, \code{}) that makes text appear bold. ...
12
votes
1answer
166 views

Simple way to switch inline asymptote figures on and off

I use the inline asymptote environment \begin{asy} to draw a lot of the figures in my papers. To those not familiar with asymptote, the asymptote package creates *.asy files for each of the asy ...
12
votes
1answer
2k views

\ifnum and pgfmath: error

In the following example I want to use \ifnum to compare two numbers computed by pgfmath, for simplicity I use simply 1 and 2 in the example below. Compiling this code gives me the error: ERROR: ...
9
votes
2answers
117 views

Provide boolean from two arguments case insensitives

In most publications scientific names are abbreviated except the first time. Moreover, the names must be in italics and only the first letter of the generic name should be upper-case. The biocon ...
8
votes
1answer
260 views

Biblatex: Convert doi-url into doi field

I use biblatex and would like my bibliography to only include a DOI field, whenever there is DOI information available. This involves two things: Biblatex has to suppress the URL field whenever a ...
8
votes
1answer
962 views

How to use mod operation in latex with tikz

I need something similar to \ifnum \j mod 2 = 0 rest of a number, but do not know how to do this in line 22. \documentclass{article} \usepackage{tikz} \usepackage[active,tightpage]{preview} ...
8
votes
1answer
332 views

Require a certain (or later) version of a package?

Is there a way to enforce that a certain version, or a later version, of a package is loaded when building the document? I've found that compiling a document on another platform, where the package ...
6
votes
2answers
304 views

Expand command read from file

I have a file, that contains a list of latex commands each command per line. Now I want to read this file line per line and execute/expand the macros automatically. I tried the following but this only ...
6
votes
1answer
377 views

pgfplotstable: Conditional post-processing of cell content on a per-column basis

I am trying to add an asterisk to the content of certain cells depending on the cell's content (essentially, adding a marker for significant p-values) but I am having trouble trying to do so on a ...
5
votes
2answers
225 views

Checking if a macro expands to one of two given strings

I want to test if the expansion of a given macro, say \mystring, is equal to one of two given strings, say string1, string2. I guess it's possible to do this with etoolbox, with something like: ...
17
votes
2answers
349 views

Why does this simple \ifx test fail?

This should be easy, but I don't understand why the test below fails... I expected Hurrah! to be printed when the \currentchar count reaches 90 (i.e. the character code of Z). There must be some ...
16
votes
4answers
650 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 ...
14
votes
3answers
451 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 ...
13
votes
3answers
400 views

Making a “For Loop”

I am trying to make a "for loop" that will iterate the variable reaches 1. However, none of the "for loops" I have found in the Internet works. These files will be distributed to other people and ...
11
votes
2answers
382 views

Is a TeX macro defined?

I am looking for a way in TeX to check if a macro was defined. Currently I use a rather tangled way. I exploit a sort of side effect when comparing two macros via \ifx; this command returns: true, ...
10
votes
3answers
144 views

How to align the conditions in a hierarchical structure?

I know how to align the conditions on the left side of the conditions with either aligned, cases, or array if there is only one level. However, I can't figure out how to do so in a hierarchical ...
10
votes
1answer
262 views

What documentation thoroughly explains how to use TeX conditionals?

I have a document which needs a large variety of plain TeX conditionals, e.g.: IF #1 >= 1 AND #2 >= 10 IF #1 != “tree” OR #2 == “mountain” IF 10 > #1 > 2 I have found limited ...
9
votes
1answer
269 views

Macros for hiding empty sections do not work properly

In an attempt to make it possible to outline much of my document by adding new parts to it, but without making parts that I have not written anything on show up in the compiled document, I have ...
8
votes
1answer
441 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 ...
8
votes
3answers
3k views

How to make LaTeX ignore the contents of an environment?

I would like to make a \todo command and environment whose behavior changes depending whether I define a command \showtodos. A basic \todo command is simple to write: ...
8
votes
3answers
247 views

Including only the parts of a document corresponding to a conditional expression

Suppose I have a LaTeX document, which uses a conditional. Ie. \ifdefined\submit [....] \else [....] \fi So, if we have, say, defined \def\submit{} then the first part of the conditional ...
7
votes
1answer
175 views

How to detect that some macro expands to empty string or spaces only?

I need to check several macros and do some actions only if their expansion is not empty nor space only. In pseudo code I would look like this \if\macroToBeTested \doSomething ...
6
votes
2answers
269 views

How to extend a macro for e-mail addresses to support multiple arguments?

I prepared the following macros to format e-mail adresses in a e-mail header. \usepackage{xifthen}% Provides \ifthenelse and \isempty % E-Mail header strings with format. ...
5
votes
1answer
94 views

Any way to set a variable externally?

I just posted a question and can now generate a conditional section in a text: how to make a conditional document feature But to extend and generalize the question, first how could I create a ...
5
votes
1answer
114 views

How to define a macro conditionally based on whether it is being used in text mode or math mode?

I'd like to be able to make a macro \foo whose definition would look something like \newcommand{\foo}{ \ifintextmode{abc} \ifinmathmode{x\cdot y\otimes z} } so that \foo $\foo$ produces ...
4
votes
1answer
184 views

(x)ifthen in TikZ

I am searching for a way to conditionally handle some items in a TikZ \foreach specially using an \ifthenelse condition. However, the compiler responds ! Undefined control sequence. <argument> ...
4
votes
2answers
373 views

How to detect words starting with # in listings package?

I am writing some code with the listings package and I need to detect the words starting with the number sign (#) and apply certain style. For example, if I have this code: something #foo ...
3
votes
1answer
64 views

Conditions for fancy chapter title

I use a book class and here is a part of my fancy title page for the mainmatter chapters. \newlength\chapterwidth \settowidth\chapterwidth{\huge\chaptertitlename} \titleformat{\chapter}[display] ...
3
votes
1answer
1k views

How to add a condition in \tikzset?

I am trying to make some modification on the answer of Andrew Stacey of my previous question "A macro of drawing a rectangle with several parameters in TikZ". The current code is the following: ...
3
votes
1answer
355 views

conditional highlighting with soul

The journal PLoS ONE, says in the FAQ When submitting your revision, you will need to include the following new files: [...] A ‘clean’ copy of your revised manuscript. A revised manuscript with ...
2
votes
1answer
682 views

Multilingual beamer presentations from a single souce?

I have a beamer presentation of which I have two language versions (german and english), more languages are upcoming. I keep both versions in one source and select the one to typeset with the comment ...
1
vote
2answers
520 views

Book in two (or three) languages in LaTeX

I am writing a book in two languages: a Russian version and an English version. Is it possible to do this in the same file? Something like \tltext{Hello world!}{Здравствуй, мир!}, and mark at the ...
14
votes
5answers
269 views

A macro that conditionally disregards the need for one of its arguments

Let's suppose I want to write nested equations that contain a fair few nested parenthetical/delimiter characters, like (), [], {}, ||, and perhaps others. Suppose too that I think these things look ...
12
votes
2answers
170 views

How to test whether the starred-version of a command is defined?

This question is similar to How to conditionally define a new command in LaTeX? but I want to test if the starred version of a command is defined. The naive thing \makeatletter ...
12
votes
2answers
393 views

Missing Item number in enumerate

You would think that I would know better than to mess with an egreg solution, but in my defense I used a DavidCarlisle solution from \setbox vs. \sbox and \savebox - What are the differences I need to ...
12
votes
3answers
398 views

How do I test for a character, irrespective of catcode?

This came up from Labels in TikZ are incorrectly interpreted, but it's also something I've run into with the braids package, and a couple of other things I've done. Also, from looking at the TikZ ...
11
votes
3answers
384 views

How to ensure document total page numbers is a multiple of four?

I'm producing a document (book class) that will ultimately be produced using offset lithography. It's therefore useful to ensure the total number of pages is a multiple of four by adding as many blank ...
11
votes
2answers
234 views

Can I detect whether LaTeX is in vertical mode?

I am writing a command that creates a margin paragraph. This requires that this command is only used in vertical mode, which is fine. If the command is used in a float, LaTeX gives a "not in outer par ...
11
votes
2answers
407 views

biblatex: Something like \ifentrytype

I’d like to change the bibmacro cite:short only for one entry type (@music). So is there a way to test for the entry type?