The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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
4
votes
1answer
2k views

How to refer to the absolute row number in a \DTLforeach loop

Consider the following MWE : \documentclass[12pt]{article} \usepackage{datatool} \begin{document} \DTLnewdb{table} \DTLnewrow{table} \DTLnewdbentry{table}{a}{hallo} \DTLnewdbentry{table}{b}{non} \...
11
votes
3answers
453 views

How to \enddocument inside an environment

I have a document of the form \begin{document} some text \begin{myenv} .... \end{myenv} some other stuff \end\document I would like to set a boolean \newif\if@test in the header to control the ...
14
votes
3answers
617 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 ...
11
votes
2answers
537 views

How to check if a section title (or any heading) is the first line of a new page?

I would like to be able to use an \if command to check if a \section appears on the first line of a new page. If a section heading is the first line of a new page, I would like to then adjust some of ...
5
votes
2answers
382 views

Check if mathversion (font) is defined

One can define a new mathversion using \DeclareMathVersion{sans}. I wonder if I can test the existence of such a font definition, for example with \IfMathVersionDefined{sansmath} {math font is ...
42
votes
3answers
8k 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
5answers
3k 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 } \begin{...
7
votes
1answer
699 views

Is something like “or” acceptable in conditional tests defined by \newif?

I am using conditional tests in my .cls file which has two options "final" and "draft". I am rather familiar with conditional tests but I am just curious to know how to define the "or" in the ...
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 ...
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".
9
votes
4answers
3k views

Undefined control sequence \equal in section title while using packages hyperref and ifthen

I have a command that uses package ifthen. I use the command in section titles. I have to protect the command because otherwise I obtained the error Undefined control sequence \equal. So far, so ...
15
votes
3answers
9k 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: \ifthenelse{\isundefined{\...
2
votes
1answer
180 views

Check if multiple column types are defined fails

Using the commands of the discussion How to check if a column type is defined? and How to implement a command that checks for loaded packages? I have build an enviroment to check if more than one ...
5
votes
3answers
990 views

Own document variables with and without arguments

I use the following to introduce new document variables to my document. They should behave like \author or \title. So when they're used without parameter, they should print the corresponding variable. ...
39
votes
1answer
1k 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 \...
2
votes
1answer
504 views

pgfplots: externalization and ifthen

I use pgfplots with externalization. When I try to include a macro that uses \ifthenelse, always the 'else' statement is being executed. When I switch off externalization, it works again. See this ...
12
votes
2answers
5k views

Checking if two conditionals are met

I am using the etoolbox package to check some variables. I can use \ifnumless{a}{b}{True}{False} to check if "a" is less than "b" and output "True" if true and "False" if false". How can I check if ...
8
votes
1answer
199 views

How can I conditionally include the page number in a cross-reference (when the object referenced is on a different page)?

When a cross-reference to a figure is not on the same page as the figure, I would like to include the page number in the figure. But when they are on the same page, this is redundant--and I want to ...
5
votes
2answers
2k 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: \IfFileExists{/media/kingstonGB/LaTeX/preamble}{\input{/...
9
votes
1answer
153 views

An enumerate environment in which the label is omitted when item number is just one and displayed as usual when item number is more than one

How to define an enumerate environment in which the label is omitted when item number is just one and displayed as usual when item number is more than one?
18
votes
6answers
2k 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: \ifnumless{x}{...
6
votes
2answers
2k views

etoolbox: environment hooks in boolean switch

What did I wrong, that the following does not work here: (Edit: I mixed up the true and false parts, but I don't want to change the code, because the answers refer to this in a way confusing one.) \...
7
votes
1answer
1k views

\newif conditional causing problem in \typeout or \edef

The following code produces result as expected, i.e., False True on the typeset document. \documentclass{minimal} \newif\iftest \newcommand{\test}[2]{ \iftest #1 \else #2 \fi } \newcommand{...
6
votes
1answer
350 views

How do I use \ifcommandkey , or how do I check if a key was given?

I'm trying to create a keycommand with optional arguments, but I can't get it to work. I looked at Comparing an argument to a string when argument is a result of a command with etoolbox and although ...
14
votes
3answers
1k views

biblatex: is there a command analogous to \ifciteseen but within one page?

Biblatex is loaded as \usepackage[style=numeric-comp,hyperref=true]{biblatex} \ExecuteBibliographyOptions{citetracker=true,sorting=none,firstinits=true} I am looking for an if-command, which would ...
4
votes
1answer
872 views

latex @temps(wa) commands

I would like to know more about the temporary commands of LaTeX, such as \@tempa, \@tempswatrue especially for the following construct, where I wonder if my replacement is correct: % Provide bool for ...
8
votes
2answers
829 views

How to check if a column type is defined?

Column types for tables can be defined and overwritten with \newcolumntype. I would like to know if a column type is already defined for an example which shall only be executed in the case where the ...
15
votes
3answers
2k views

How to implement a command that checks for loaded packages?

I use the following code to check if a package is loaded: \RequirePackage{ltxcmds} \newcommand{\IfPackageLoaded}[2]{\ltx@ifpackageloaded{#1}{#2}{}} However, sometimes I want to check if more than ...
26
votes
3answers
581 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 ...
6
votes
3answers
343 views

How to conditionally control the width of a figure with minimal code duplication?

I am using \iftoggle to switch between a for-review (doublespaced, onecolumn) and photo-ready (singlespaced, twocolumn) layout. I want to change the width of figures to be optimal for both, depending ...
1
vote
3answers
2k views

Detecting if inside \section

How can I make a conditional that checks if it is currently inside a \part, \chapter, or \section? IF inside sectioning = TRUE DO "This text is inside a section." ELSE DO "This text is not ...
0
votes
0answers
110 views

How to check for document class in conditionals [duplicate]

Possible Duplicate: Which documentclass is being used? I know how to use conditionals in LaTeX, but I need to check what documentclass is being used. I need to not include a package when I'm ...
1
vote
0answers
128 views

Is it possible to use logical connectives in conditionals? [duplicate]

Possible Duplicate: How to form “if … or … then” conditionals in TeX? By writing \ifnum cond1 and cond2, the compilation is successful but the generated document is ...
1
vote
0answers
83 views

Is there a way to optionally import a package? [duplicate]

Possible Duplicate: Conditional “if a package is available” I'm pretty lazy, and have my own .sty files which import a bunch of commonly used features. But, sometime system ...
57
votes
2answers
13k 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? \newcommand{\mycommand}[2][...
11
votes
2answers
440 views

Is it possible to abort loading a package if it's too old?

I'm working on a package that has to work on both TeXlive 2009 (Ubuntu, so updating TeXlive is a nonstarter) and later versions, and I'd like to be able to use fontspec with LuaLaTeX if we're on ...
6
votes
2answers
482 views

macro for custom indexing with conditionals and text recognition

I was unable to solve this by myself and by searching this wonderful site. The problem is with indexing. I still consider myself as a LaTeX newbie :) The situation: - I'm working on an english book ...
8
votes
2answers
291 views

How to put a conditional construct in \edef

\ifthenelse works fine normally, but when used in \edef: \documentclass{article} \usepackage{ifthen} \newcommand\testing[1][]{% \edef\tmp{\ifthenelse{\equal{#1}{}}{Blank}{#1}}% } \begin{document} ...
6
votes
2answers
988 views

Feeding arguments to xstring macros

In Footnotes with automatic final period, the comment was made to provide the possibility to add a period . to a footnote conditionally, based on whether the footnote already has a period or not. I ...
83
votes
7answers
30k 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 ...
27
votes
1answer
3k 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 ...
9
votes
1answer
1k views

Parsing a macro argument character-by-character for conditional execution

In an attempt to specify a seemingly endless amount of arguments to a macro, I would like to supply only one #1, where #1 is a string that contains a bunch of specific characters. For example \...
20
votes
4answers
2k views

How to determine whether a list of figures is empty, then not show it at all?

I want to check whether the document has any figures so as to decide whether or not to have a list of figures. Is this possible? Feasible without doing something like parsing the lof file? Note that ...
2
votes
1answer
645 views

Should this combination of counters and \ifnum conditions work or does it need modification? Is there a better way?

I'm producing the final product of my thesis with LaTeX. I'm having trouble with the front matter of my thesis, specifically with getting LaTeX to recognize that if the front matter stops at a certain ...
17
votes
4answers
1k 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 ...
4
votes
1answer
4k views

command \equal{#1}{} produces error if compactitem is in argument

I want to check if a variable is empty or not in a custom command. This works well, as long as it contains "normal text" but as soon as there is an itemize environment, I get an error message: \...
0
votes
0answers
83 views

If-then in a document style for specific document classes [duplicate]

Possible Duplicate: Which documentclass is being used? I want to make a style that would include some specific instructions if the document is using the memoir class, but I don't want those ...
19
votes
3answers
2k 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 ...
4
votes
1answer
2k 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: \...
29
votes
3answers
8k 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.