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
3
votes
1answer
518 views

Count number of paragraphs in command argument

Is there a way to count the number of paragraphs in a command argument? I would like to make a command doing something like this: \newcounter{\numberofparagraphs} \newcommand{\testcmd}[1]{ \...
17
votes
2answers
795 views

How does one detect whether one is in the preamble or not?

In LaTeX, I'd like to know whether a documentclass has been loaded and whether code is inside a \begin{document}. In other words, is there a \ifpreamble similar to \ifvmode and friends? Partially ...
56
votes
1answer
10k 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 \...
7
votes
1answer
778 views

LaTeX3: Defining key to set boolean variable

I want to define a key, which, if set, sets a boolean variable to false. The documentation is not clear on that point. It states (source3.pdf, sect. 87, p. 134): Defines <key> to set <...
31
votes
3answers
7k 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: \newif\...
12
votes
1answer
4k views

Using ifthenelse in pgfmath

Following questions on constructing functions on the forum, I tried to build some myself. I can't seem to get the ifthenelse function in TikZ/PGF to work. Here is an example: \documentclass[]{minimal}...
7
votes
2answers
2k views

Nest StrLen and ifthenelse commands

Here is a sample of code I am working on: \documentclass[10pt]{report} \usepackage{xstring} \usepackage{ifthen} \begin{document} \StrLen{123456}\\ %Prints 6 ...
19
votes
2answers
2k 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 ...
10
votes
5answers
4k 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 ...
41
votes
2answers
5k 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? ...
4
votes
1answer
498 views

TikZ. Is there a way to alternate path to be drawn basing on some condition?

What I want is to connect nodes by stepped (horizontal-vertical-horizontal, hvh) path if they have different y-coordinate, and by straight line if they are on the same level. Costruction of the only ...
4
votes
0answers
159 views

Condition to determine if current line ends or starts the page [duplicate]

Possible Duplicate: Conditioning behaviour of a macro on whether on top of a new page I have two paragraphs with some separator (rule) between them. I want to make this separator appear only if ...
21
votes
2answers
3k views

Environments with conditionals in: why doesn't this work?

In response to this question I thought the obvious thing to do was the following: \documentclass{article} \newif\iffoo \newenvironment{foobar}{\iffoo}{\fi} \begin{document} \begin{foobar} Here is ...
6
votes
2answers
594 views

In the verbatim environment, how can I display an 'if' statement I declared as '\ifcondition'?

I have declared a couple of if commands of the form \newif\iffinal \newif\ifsolutions Now I want to write about it in the same document! So I want to write something like \iffinal (...) \...
25
votes
2answers
5k 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 ...
6
votes
2answers
290 views

Telling LaTeX where to compile to from inside your document?

Here is a hypothetical scenario. I want to make a beamer presentation and a handout for that presentation. I start my file as follows: \newif\ifhandout \handouttrue \ifhandout \PassOptionsToClass{...
93
votes
4answers
35k 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 ...
28
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 ...
6
votes
1answer
271 views

What does \global\boolfalse{cbx@loccit} mean?

Pardon the newbie question. I see this in the first part of a \DeclareCiteCommand and I'm not sure what it means. Google has not returned any joy. Here's an example \newbibmacro*{cite}{% \...
22
votes
2answers
1k 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 ...
6
votes
1answer
275 views

Asserting reference inequality

Is there a way for including assertions in a LaTeX document? I want to check that some condition is true, and only allow compilation of the document if it is (or at least complain otherwise). ...
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 ...
3
votes
1answer
243 views

Driver-dependent invocations of the graphix package?

The LaTeX General Guidelines show a conditional invocation of the graphicx package. I wanted to test this out. I did the following: Created a main.tex in which I LEFT OUT the big, complicated, ...
25
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 ...
6
votes
3answers
2k views

Conditionally displaying/hiding parts of document using macros

I'm currently using the following approach to conditionally display/hide parts of my document: % PREAMBLE \def\MyVersion{1} % BODY \ifnum\MyVersion=1 This is version one. % possibly other ...
7
votes
1answer
345 views

Different content if command returns empty text (ConTeXt)

I have the following code in my ConTeXt environment: \startsetups[sectioninfo] \ss \rlap{\userpagenumber} \hfill § \getmarking[sectionnumber][current] \emspace \bf \getmarking[section][...
31
votes
4answers
2k 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 ...
6
votes
3answers
346 views

Zero parameters in command definition

Let us say I want to define a command for typesetting (a+b)^2: \newcommand{\sumsquare}[2]{\ensuremath{(#1+#2)^2}} How can I modify it so that \sumsquare{a}{0} will produce a^2?
1
vote
2answers
298 views

How to multiply compile according to selected tags?

I would like to do the following, by I am not sure how. Say I have a LaTeX file with series of annotations, each with zero or more "tags" (say, about their contents or the project they are related to)...
8
votes
2answers
4k views

LaTeX: Use command argument in conditional?

I'm in the process of writing a command that outputs a certain text only if its argument is a nonzero number. I'd like to be able to write something like: \newcommand{mycommand}[1]{ \if{#1 != 0} Some ...
12
votes
3answers
911 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 ...
6
votes
4answers
1k views

Declaring document-wide font size outside of the document class command

I have a document, where I want two versions: one versions 10pt size and another version, 2 page to a side 12pt. So I currently have: \documentclass[10pt,a4paper,oneside,openany,article]{memoir} \...
5
votes
2answers
1k views

Compare a character to a character code?

How can one compare a character (e.g. "U") with a character code in a certain font (e.g. "\char85"). I'd expect the following to work: \documentclass{article} \usepackage{ifthen} \begin{document} \...
2
votes
1answer
309 views

Are there any packages that I should not be using with ifthen package

I am new to TeX and I started adding package files to prepare my document without worrying about conflicts. I am asking this subjective question because I am not sure if I can post entire document ...
95
votes
4answers
138k 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?
8
votes
2answers
3k views

\if@ statement explanation

I'm trying to tweak some .cls to fit my needs, and I'd really appreciate it if someone could explain me these statements: \if@radseminar% ... \else ... \fi If I understand correctly, it will match @...
18
votes
3answers
2k 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. However,...
3
votes
2answers
186 views

Conditionals within captions

I have defined a conditional using the ifthen package as: \newcommand{\Switch}[1] { \ifthenelse{\equal{#1}{1}}{Test1}{Test2} } Running this command works without problem. However, if I try to place ...
4
votes
2answers
489 views

How do I set a variable inside of \rlap

Two questions please about this code example: 1) Why does \rlap prevent me from setting a variable? 2) How do I set a variable from within an \rlap that is visible outside of \rlap? I'm using MikTex ...
18
votes
2answers
3k 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 \...
2
votes
1answer
767 views

how to use memoir \ifoddpage

Why does \ifoddpage seem to always return false in the following example? This is related to question 6143 on this same forum. I'm using XeLaTeX to produce a pdf file (i.e., xelatex test.tex) \...
32
votes
6answers
4k 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} -> \...
17
votes
4answers
1k views

Check if LuaTeX is running

I want to check if my tex document is compiled with LuaTeX (or any other Lua TeX compiler). The simple approach from the TeXbook is to check the \directlua macro against \@empty, like \ifx\directlua\...
5
votes
2answers
777 views

a/an substitution in LaTeX

I was wondering if there is a conditional command that I can use in LaTeX (e.g., \ifthenelse) that would allow me to, in one case, use the word 'a' and in another use the word 'an' based on the ...
15
votes
1answer
424 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 ...
18
votes
1answer
2k 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!
55
votes
6answers
22k views

If Then Else for odd page/even page

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

How to use the exact same file for handout and presentation modes in beamer

I want to use the exact same file for my beamer presentation and handout. I do not like the need to comment out lines in order to create the handout or presentation (my current solution), nor do I ...
240
votes
4answers
144k 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 ...
32
votes
6answers
3k 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 = {http://dx....