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,859
questions
0
votes
0
answers
11
views
Accede to particular data inside variable
Suppose I have multiple products, each with a name, number, description, and photos (which could be one or multiple photos).
I want to automatically list the names of the products on the cover page ...
2
votes
2
answers
86
views
default-valued optional (boolean) parameter for a new command in tikz
I want to define a command for TikZ which has a default-valued optional parameter which is a Boolean, initially set to false.
I've looked around and have found:
xparse appears to be obsolete: https://...
0
votes
0
answers
44
views
How can I make a tcolorbox round its south corners if its bottom is close enough to the bottom of the page?
I'm making a document with triangular footers around the outer corner of the page, where the page number is. If a tcolorbox is too tall and too close to the bottom, it can be cut off by these footers.
...
4
votes
1
answer
65
views
collcell and \IfBlankTF comparison not working
I'm having trouble with the \collcell package - a macro I made, which uses \IfBlankTF, works fine outside of a table, but somehow \collectcell is feeding something that's "non-blank" into my ...
5
votes
3
answers
477
views
Counter in Loop
I'm trying to set the color of every three items in a 5x3 pattern from a tikz picture:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \m in {1,2,3}{
\...
3
votes
2
answers
103
views
Defining command for use with ifnum
I want to define a conditional command that I can easily use with \ifnum, hence such a command should expand to a number. However, I sometimes need to define temporary commands which breaks such usage,...
0
votes
0
answers
42
views
Listings hook to detect whether the current line contains a colon?
TL;DR: I want to use the listings package for typesetting YAML snippets. For example:
\begin{lstlisting}[language=yaml]
key: value
list:
- value1
- value2
env:
key1: value1
key2: value2
\end{...
3
votes
2
answers
131
views
Help with loops – why is there an Ω?
I am just getting some experience with looping constructs in TeX/LaTeX. I thought I understood how \@tfor works, and things were mostly coming out as I wanted. But I ran into some unexpected ...
4
votes
2
answers
181
views
conditional enumerate labels
I am writing a math book. I have exercises that have questions AND subquestions (for example 1 then 1.a, 1.b, 2 then 2.a, 2.b, 2.c etc.). I also have exercises that have one questions, without ...
3
votes
1
answer
256
views
How does \if:w work?
I cannot work out how \if:w works and LaTeX3's manual does not show any example on how to use it:
I want to check whether the document is currently in oneside or twoside mode:
\if@twoside
Twoside ...
4
votes
1
answer
112
views
Why \IfPackageAtLeastTF{⟨package⟩}{⟨date⟩}{\PassOptionsToPackage{⟨option⟩}{⟨package⟩}}{} doesn't pass ⟨option⟩ to ⟨package⟩?
My current version of parskip is 2021-03-14 so:
\IfPackageAtLeastTF{parskip}{2018-08-24}{\PassOptionsToPackage{indent}{parskip}}{}
\usepackage{parskip}
should pass the indent option to parskip. But ...
1
vote
1
answer
26
views
Displaying article title in fancyhdr header with conditionals
I use the tip found here How to display article title in fancyhdr header to display article title in fancy header. But I'm not able to make it works within a condition sequence.
As an example, this ...
2
votes
2
answers
140
views
Checking if a certain fragment is located earlier in the document
Very often I want to refer to a certain claim I mentioned earlier in the document: "As I said earlier, blah." Since I often rearrange material, I need to ensure that "blah" indeed ...
1
vote
2
answers
46
views
Can I change the effect of a command depending on the environment in which it's invoked?
I need a user-defined command to behave differently depending on whether it's invoked inside a \begin{quotation} environment or not. Is that possible? I don't know where to start!
1
vote
1
answer
32
views
Analyze number of spreadtab rows for conditionally changing text elements in the document
again I need your help. I'm writing my invoices for my small music busines with LATEX scrlttr2 and with a spreadtab-tables. It is working very well, but I want to have an automatic selection of some ...
3
votes
2
answers
104
views
\ifnum to draw a tikzpicture, less than or equals
I want to make a conditional statement that draws one tikz picture for specific values of \t and another for different \t values.
Here is a M(non)WE:
\documentclass{article}
\usepackage{tikz,ifthen}
\...
2
votes
2
answers
57
views
Switching between translations of the Bible, looking up text
I have a document with scriptures listed, that I can call. (I'm not sure this is the best way to do it, but it's based on this stackoverflow answer.)
\documentclass{book}
\makeatletter
\newcommand\...
1
vote
0
answers
21
views
Conditional Output with user input
I am trying to write a form in Latex where I'm asking a question an if the answer is no I whant to ask another question. I'm a real beginner in Latex and I need a little help how to start it.
It ...
0
votes
0
answers
43
views
Check if a file has been modified since last compile in Overleaf
I have an if-statement that I call in my .tex document in Overleaf
\IfFileExists{mypdf.pdf}
{\includegraphics[width=1\textwidth}{mypdf.pdf}}
{\includestandalone[width=1\...
2
votes
1
answer
43
views
How make raggedleft really right aligned in if/else environment?
For some reason, the \raggedleft isn't really right-aligned. I tried minipage but that apparently doesn't work in the if/else environment.
\documentclass[
% handout,
english,usenames,...
3
votes
2
answers
55
views
Array or tabular in Beamer breaks \if
I want to have an array or tabular environment in an \if statement in Beamer. If I have an article, this compiles just fine:
\documentclass{article}
\def\x{1}
\begin{document}
\if\x1
\begin{tabular}{...
3
votes
3
answers
359
views
How to define a command (or similar) that makes a conditional expression reusable?
I'm struggling with the correct use of conditional expressions with ifthenelse at the moment. I would like to define a command (or similar) that represents a certain conditional expression in order to ...
2
votes
3
answers
272
views
Defining a custom if command
In the following minimal example, I am able to define a custom \if* command based on ifcsname, however when I use it inside \loop construct I get Extra \fi errors
\documentclass{article}
\...
0
votes
2
answers
35
views
How to read from external file and compare to given string [duplicate]
I am trying to extract text from an external file (someexternalfile.txt) and compare it with a given string (sometext) but it is saying that the external text and the given string are not the same (...
0
votes
1
answer
57
views
Comparison of macros fails
I am new to LaTex and have been given a problem by my colleagues. They define a document type in a TeX file (e.g. 'svvr', 'srs', ...). This external definition corresponds to lines 4 and 5 in my ...
6
votes
3
answers
221
views
How can I improve the spacing after a top float?
Context
I'm trying to create a macro (called here \myCommand) that separates two paragraphs and adds a different vertical space depending on the context. But I suppose this is a general problem which ...
2
votes
1
answer
36
views
Iterating over a list and conditionals in tabular environment not working. Incomplete \ifnum; all text was ignored after line 72
i work on a project with a lot of people and there are many who are not really familiar with LaTeX. Therefore, I have to define a command (\timeplan{}), which simplifies coloring tablecells (each ...
0
votes
0
answers
37
views
Drawing items at selected grid locations
I would like to draw a grid and place queens are selected columns for each row. I am able to draw the grid, but the placement of queens is improper.
\makeatletter\newcommand{\showQL}[3][,]{%
\def\...
1
vote
2
answers
35
views
Weird Question with def, and condition
MWE:
\documentclass{article}
\def\ENLANG{english}
\def\FRLANG{french}
\def\LANG{\ENLANG}
\begin{document}
\if\LANG\ENLANG \ENLANG \else \FRLANG \fi
\ifx\LANG\ENLANG \ENLANG \else \FRLANG \fi
\LANG\ \...
1
vote
1
answer
66
views
Detect if a parameter passed to command is a mathematical formula
I'm defining a command which displays braced paragraphs and an isomorphism between them, but I want the command to recognize if one of the paragraphs is something purely mathematical and therefore not ...
7
votes
4
answers
312
views
Adding up numbers
In a local class file I maintain, we have a system that can automatically add up numbers (in our case, marks for an assignment) and then output the sum.
I would like to develop it to add up limited ...
1
vote
2
answers
55
views
How to determine from the document body whether or not a page has a top float?
For a particular application, I need to know whether or not a page contains a top float. I have therefore taken the definition of the conditional \iftopfloat{<true>}{<false>} given in the ...
5
votes
6
answers
248
views
nested \ifx with extra \else error
I have this code with nested ifx (plain TeX):
\def\test#1#2%
{
\edef\cmpa{#1}
\edef\cmpb{x}
\ifx\cmpa\cmpb
it is x
\edef\cmpa{#2}
\edef\cmpb{1}
\ifx\cmpa\cmpb
and 1
\...
2
votes
1
answer
54
views
\ifthenelse atomic proposition not behaving as expected when paired with fancy footer
I understand that the title of the thread is a bit cryptic, but I admit that I don't have much of a clue about the reason for which this (apparently simple) conditional (sometimes) fails in working as ...
0
votes
0
answers
34
views
No page number for TOC entry followed by entry deeper and same page
Let's assume that my Introduction is Chapter 1 on page 1, with no sectioning.
And Chapter 2, Section 2.1 and Subsection 2.1.1 appear on page 3. And Subsection 2.1.2 and Section 2.2 and Section 2.3 ...
3
votes
1
answer
159
views
If-else-fi within foreach with recursion
Given a list of numbers of size n, n-1 combinations are formed. The numbers of each combination are multiplied and all the products are added. This is taken from question 711870. Recursion works on ...
1
vote
0
answers
32
views
How to make conditional command with non-numerical condition? [duplicate]
I would like to know if someone has a solution to the following problem:
I have recently start to create shortcut to certain operator to display directly the parenthesis around their arguments.
\...
3
votes
4
answers
137
views
Compare first token after expanding macro once and discard the rest
How does one write an if clause that checks whether the first element of the immediate expansion is \mymacro, without fully expanding the input?
\documentclass{minimal}
\def\mymacro#1{Does something ...
1
vote
2
answers
73
views
How to do an if after an `exp:args` in expl3
Following this answer by @JasperHabicht, in my code, I would like to add an option to that macro so, depending on a flag I can execute either \drawMoveFromSgfCoords or \drawStoneFromSgfCoords. I still ...
5
votes
4
answers
487
views
Inline If-Else Statement Based on Macro Argument
How do I do an inline if-else statement?
\draw[draw = #1, fill = \if black#1 white \else black \fi, line width = 0.1mm]
(#2, #3) circle [radius = 0.25cm];
I've tried to do it based on this answer, ...
1
vote
1
answer
102
views
How to Color Certain cells in TikZ Pictures
I'm interested on coloring certain cells of a TikZ picture. It is the following:
I did it by using the grid and creating the numbers. I present the code I create above:
\begin{tikzpicture}[scale = 0....
3
votes
2
answers
218
views
Can I issue a warning when an uppercase symbol is used in my math command?
I have been using something like
\renewcommand\vec[1] {\boldsymbol{\MakeLowercase{#1}}}
\newcommand\mat[1] {\boldsymbol{\MakeUppercase{#1}}}
for some time now to typeset vectors and matrices.
...
3
votes
1
answer
61
views
ifmtarg testing a macro that is empty?
The following code works as expected:
\NewDocumentCommand{\checker}{m}{\@ifmtarg{#1}{empty}{not empty}}
\checker{} % Prints "empty"
\checker{x} % Prints "not empty"
However, how ...
2
votes
3
answers
136
views
Conditional macros
Goal: I would like to have a macro that substitutes different commands depending in whether in displayed math mode or not. In particular, it should generate fixed-size angular brackets in text lines, \...
3
votes
4
answers
940
views
How can I write a LaTex command with 'if'-'then'-'else'?
I read LaTeX \if condition and How to write if and then in algorithm?, but so far it did not ring a bell for me.
I am trying to create a command named dottedSkill like below.
The logic behind it is:
...
0
votes
2
answers
59
views
Counter is incremented inside \if, even condition is not met
I have defined three counters which I'm going to increment within a condition. This should lead to the result that functional requirements are prefixed with FA (and a subsequent number) and non ...
2
votes
1
answer
105
views
Searching a list of words using Boolean operators
In the project I'm currently working on, the user can define, within an environment defined by the project, a list of tags, stored internally in a macro (for example, \tags). Each item in this list ...
4
votes
2
answers
274
views
Using numbers from new commands in ifnum
I am trying to create a Latex document in which I define a list of items (e.g. fruits) at the beginning in order to change the resulting text / appearance of the document depending of the amount of ...
5
votes
2
answers
253
views
Remove page number, if there is only one page in a section
In my current project, I want to create worksheets that I can print out and distribute. To do this, I want to reset the page number for each worksheet, but only display the page number if the ...
1
vote
2
answers
36
views
Suppress text output but leave enumerate item labels in place
I am typesetting an exam question like
\documentclass{article}
\newenvironment{answer}{\par[}{]}
\newenvironment{answersonly}{}{}
\begin{document}
\begin{answersonly}
\begin{enumerate}
\item Question ...