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}.
1,916 questions
3
votes
2
answers
134
views
arithmetic operations with \ifthenelse
Is there a way to add arithmetic operations to \ifthenelse statements? Here is a minimal (non)example:
\documentclass{amsart}
\usepackage{tikz, ifthen}
\begin{document}
\begin{tikzpicture}
\...
0
votes
2
answers
59
views
Can we mix \newif's \ifname as in \ifgreen with \OR or \AND?
I created \ifCond1 and \ifCond2 with \newif.
If I want to test one condition, I use \ifCond1 Condition 1 OK \else Condition 2 KO \fi.
My question : is there a way to use \ifCond1 and \ifCond2 glued ...
1
vote
0
answers
33
views
Print biblatex' bibheading only if the underlying bibliography is non empty
In the following MCE, bibliographic references are grouped by year, with a bibheading for each year (an unnumbered section indicating the year in question).
\documentclass{article}
\usepackage{...
2
votes
1
answer
66
views
Conditional text in a document that uses xsim package
I would like to conditionally add text (for example, "- Answers") to my
exercise sheet title based on the global solution/print option set in
\xsimsetup.
My goal is to have the title change ...
8
votes
4
answers
333
views
Vertical dashed lines in tikz. Figure from Proofs Without Words
I’m trying to recreate a figure from Roger B. Nelsen’s Proofs Without Words: Exercises in Visual Thinking—specifically “Sums of Squares V” (Integer Sums, p. 81).
My code gets everything right except ...
2
votes
1
answer
60
views
Conditional formatting of annotated bibliography
I want to print a 'normal' and an annotated bibliography from the same .bib file.
Both documents need to have the same custom style (not pictured in the MWE), so I used the method in: Print and format ...
0
votes
2
answers
90
views
etoolbox conditionals with kvoptions string options
I'm writing my first LaTeX class. After some research I've found that kvoptions seems to be the most recommended for key value class options, and etoolbox is recommended over ifthen for conditionals.
...
5
votes
1
answer
122
views
How do I create a new environment with a conditional?
I'd to be able to set a value at the start of my document that sets in which chapters examples will have solutions. My attempt at this is as follows.
\newcommand{\solutionsupto}{2}
\newenvironment{...
0
votes
1
answer
104
views
Custom ordering of list items [duplicate]
Background: I want to create a multiple choice exam in two versions, where the order of the choices differ. That is, both versions have the same questions and a list of answer options, but these ...
3
votes
1
answer
134
views
Switch ouput on / off with specific syntax
I would like to define two switches such that this code
\begin{document}
\A This is text A
Some more text A
\B some text B \A again some text A
\end{document}
produces the following output:
This is ...
10
votes
2
answers
229
views
Interaction between \ifcat, \noexpand, and active characters
I have come across the following oddity. Consider this example:
\catcode`!=\active
\let~=\empty \ifcat\noexpand~\noexpand! yes\else no\fi
\let~=\relax \ifcat\noexpand~\noexpand! yes\else no\fi
Why ...
1
vote
0
answers
68
views
Exact positioning (placement) of text or boxes
This question is related to one of my previous questions:
Specifying exact placement of answers in exam tests
I would like to make exam tests that can be compiled without and with answers, and the ...
2
votes
1
answer
108
views
\ifbeamer@... inside an \if condition causes it to run
I have a custom .sty file that has an option, "slides", for including packages and commands specific to beamer documents. Here's how it's structured:
\NeedsTeXFormat{LaTeX2e}
\...
2
votes
2
answers
191
views
Difficulty writing an ifthenelse style command
Edit: The previous edit removed the extra line in my command definition, but it turns out that this was the actual problem and not that there were mismatches in argument types being passed between ...
1
vote
2
answers
110
views
sequence duplication problem while using \if
EDIT:
As Skillmon pointed out in the comments:
"If you input your file multiple times you're adding stuff to your sequences every time you input it (since you don't disable your \quan-macro on ...
0
votes
2
answers
122
views
How to avoid incomplete \ifx when stopping execution
Consider the following sample code.
%%% stop.cls
\stop
%%% test.tex
\documentclass{stop}
\begin{document}
\end{document}
LaTeXing test.tex generates the warning
(\end occurred when \ifx on line 2 ...
3
votes
2
answers
181
views
\chapter*{ } appear wrong in edited Bjornstrup chapter style
I basicually used this code to create a fading Bjornstrup style fancy heading. But I have one problem: this code doesn't display \chapter* properly. The annoying thing is I know what part of the code ...
4
votes
2
answers
111
views
Conditional statement in TikZ depending on external variable
Imagine you have a tikz figure defined in myfig.tex :
\begin{figure}
%conditional statement (mychoice==1)...
\draw[blue,thick] (0,0) -- (1,0);
%conditional statement (mychoice==2)...
\draw[red,thick] (...
3
votes
0
answers
67
views
Adjusting formatting on second page only
I want there to be specific indents (or something) that only applies to the second page. In particular, I have the if odd page for the last three blocks because if the boxes get enough text to push ...
3
votes
2
answers
106
views
Complete conditionals before processing branches
I’m writing a fully expandable macro that converts a TeX ⟨number⟩ to its Chinese representation (in the cardinal sense, not in the ordinal sense; the latter is much easier). The digits of the TeX ⟨...
0
votes
2
answers
110
views
Is it possible to test if a document class is loaded, and if not, load one, otherwise use the one already loaded?
I have a shared preamble file that provide uniform configuration for my documents. But it has no document class of its own. That way, I can determine the class for each document. But that means it ...
1
vote
2
answers
74
views
titlesec: Print title rule "bottom" or "top and bottom" based on conditions
I'd like to enclose subsection between \titlerule but I don't know how to avoid duplicating a line if a section is followed by a subsection.
\documentclass{article}
\usepackage{lipsum}
\usepackage{...
1
vote
0
answers
61
views
Creating toggle-able sections
I am trying to create a Power-Round style problem set and want to include solutions to the problems as well, having an easily toggle-able option to include or exclude the solutions in the output (...
-1
votes
1
answer
90
views
What about \iffalse .. \fi in the preamble? [closed]
I started to use the correction program in Overleaf, and one error message I keep getting at this point related to my use of \iffalse ... \fi is the following:
Suggested fix for error in classiclib....
1
vote
0
answers
78
views
ConTeXt: conditional text based on page breaks
I'm trying to write a script in ConTeXt, and it's entirely possible that a character's lines might run over a page break. When that occurs, I'd like to be able to have ConTeXt handle the following ...
5
votes
2
answers
209
views
Token-Based (Negative + Positive) Number Recognition
Not important, but would be nice to understand: I have a strange interaction on beamer (only) with my lovely sign-macro for tabular. The sign-macro --- which I picked up a long time ago but don't ...
0
votes
1
answer
74
views
Position of nodes with the "ifthen" package
I am not sure that the result of this code is the most appropriate for the nodes of points P and Q. What is the mistake made?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\...
11
votes
2
answers
457
views
How to use \IfBlankTF?
I am updating some LaTeX code, dragging it kicking and screaming to somewhere around LaTeX3. Most of it is my code, I am unlearning various old bad habits so that I can learn new and improved bad ...
0
votes
0
answers
40
views
Using lstlisting within an ifend
I am generating automatically subjets and corrections for exams.
I usually use a "TexteouVide" function that displays the correction only if a boolean "correction" is true
This ...
3
votes
1
answer
151
views
Expand or evaluate \bool_if or \tl_if_empty inside mandatory argument list (e.g. tabularray)?
This is a contrived MWE that creates two key-value pairs for a new environment. One is a string which I evaluate if its empty using \tl_if_empty and generate the start of the "\begin{longtblr}&...
2
votes
2
answers
138
views
How do if statements interact with tabular environments?
I recently built a \version command to help version exams. The idea is that I'd pass in 5 arguments and set a flag at the start of the document that determines which argument gets read. However, this ...
3
votes
1
answer
114
views
iftex and ifplatform
The \else of \ifwindows binds wrong IMHO. Is this a bug - or a feature?
\documentclass{article}
\usepackage{iftex}
\ifluatex
\usepackage{ifplatform}
\ifwindows
lua-win
\else
lua-non-win
...
1
vote
1
answer
96
views
How to conditionally include sections/formats from command line?
I've written my resume in LaTeX, however I think it would be useful to have the ability to compile a longer, multi-page version for certain jobs (such as ones requiring the federal format).
Right now, ...
7
votes
3
answers
282
views
New command with named arguments [duplicate]
I wanted to make a command which accepted named boolean arguments like:
\mycmd{foo=true, bar=false}
So, I tried using keyval package and tried:
\documentclass{article}
\usepackage{keyval}
\...
0
votes
1
answer
54
views
Define a custom environment with solutions in red that can be easily removed and replaced with blank space
I am writing an exam in LaTeX, and I would like to include the solutions in the text. I'd like there to be one line at the top that I can comment out to remove all solutions from the exam.
In the with-...
3
votes
2
answers
70
views
KOMA-Script – Different outcome of command: conditional if in title vs. TOC vs. running header
In the working draft of my book project, each of over 100 chapters gets a keyword/number for my own reference.
I display the keywords (1) in the TOC, (2) at the start of the chapter, and (3) in the ...
6
votes
2
answers
181
views
How to check if current font is a particular family and shape?
(This might really be a question about LaTeX macro names? Or about \ifx syntax?)
I would like to check if the current font is Brill Italics, and if so, do something.
I have tried something like this:
\...
5
votes
1
answer
132
views
Define a command depending on the definition of a counter
Is there a way to test the definition of a command and do, for example, the following (with a correct conditional statement analogous to \ifthenelse):
\documentclass{article}
\def\Mycommand{\...
2
votes
1
answer
111
views
Command '\ifstrequal' not evaluating to TRUE when arguments are indeed equal
I am trying to follow this post in defining a custom citation style where I show a different icon based on the type of the citation; as such, I am using the \ifstrequal command from the etoolbox ...
3
votes
1
answer
128
views
Conditionals in Asymptote
I was reading the Asymptote doc about arithmetic and logical operators: https://asymptote.sourceforge.io/doc/Arithmetic-_0026-logical.html.
How can I make a basic if elseif else statement in Asymptote?...
3
votes
1
answer
95
views
Change label based input value
Consider the following MWE:
\documentclass{article}
\usepackage{fullpage}
\newcounter{question}
\renewcommand{\thequestion}{\Roman{question}}
% see https://tex.stackexchange.com/a/729773/10898
\...
6
votes
1
answer
266
views
How to format a LaTeX input file to adapt equations for different layouts in multiple documents?
tldr:
I have a shared LaTeX input file containing a long equation (./theorem.tex). When included in a wide single-column document (notes.tex), the equation displays properly in one line. However, in a ...
2
votes
1
answer
132
views
Getting dark mode in local output using \time and darkmode [closed]
I would like to have my pdf outputs be automatically in darkmode if compiled at night according to my local time (I of course won't use it when I prepare this draft for publication). I see that I may ...
0
votes
2
answers
102
views
How to tell if a macro parameter is a command sequence
my first ever question.
I've primarily been using LateX, and not delving into the innards of TeX, but maybe that time has come.
Suppose I have a macro someMacro, which expects one argument.
If the ...
4
votes
2
answers
247
views
Package jsonparse not working with \ifthenelse
I am trying to use the package jsonparse1 (github page here, with documentation).
I am reading some data from json files, and I have to fill my documents depending on the json entries, therefore I (...
4
votes
2
answers
270
views
Creating a dynamic grading table
My goal is to create a dynamic grading table like this:
The table reads as follows.you would get grade 15 if you either had 50, 49 or 48 points, and grade 14 if you scored 47 points.
For that I am ...
4
votes
3
answers
517
views
Conditional Definition of a Mathematical Operator
I have the following command in LaTeX:
\DeclareMathOperator{\logistic}{lgc}
I would like to modify it as follows:
If the user simply writes \logistic, it should display just "lgc", as it ...
2
votes
3
answers
242
views
Condition IF in Tikz
I would like to draw a Riemann sums plot where the bars are graphed based on the heights of each pair of consecutive intervals. The issue will be with the ifthenelse statement as I do not think the ...
3
votes
2
answers
300
views
Testing, if a file exists. (The point is the point.)
Look at: %here is the problem
With the point in .\jobname1.tex it does not work, without it, it does.
\TeX
\jobname.tex\vskip\baselineskip
Testing, if file \jobname1.tex ...
3
votes
3
answers
216
views
Weirdness with `\ifnum`: `\ifnum\curyear=2024` gives an error, but `\ifnum2024=\curyear` works as expected
After an embarrassing amount of attempted research and trial and error, I've landed on the following code that works as expected. If the current year is 2024, it says "They match!". If it's ...