20
votes
1answer
114 views

Is implicit `\relax` in conditionals explained anywhere in the TeXbook?

I have been relying for years on TeX's behavior where, say \if1\fi expands to nothing and \if1\else\fi does the same. In the second case TeX inserts an 'implicit' \relax when it tries to expand \else. ...
17
votes
2answers
348 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 ...
21
votes
1answer
416 views

Use of \relax after \ifnum … \fi construction

I'm reading Malcolm Clark's A Plain TeX Primer. On page 111 there's an exercise to define a macro that prints the current time. I've reproduced Clark's answer below: \newcount\minleft ...
7
votes
3answers
135 views

Defining tabular rows with macros using `\ifnum` leads to `Incomplete \ifnum`

I am trying to define tabular rows using a macro which shall perform an conditional definition of the tabular row according to a given parameter. Please consider the following MWE: ...
3
votes
1answer
116 views

Are there more to applications of \iftrue and \iffalse?

While reading "TeX for the impatient" on the topics of iftrue and iffalse, I encountered this example, \def\isbigger{\let\bigger=\iftrue} \def\isnotbigger{\let\bigger=\iffalse} \def\test#1#2{\ifnum ...
30
votes
3answers
2k views

Difference between \if and \ifx

I try to understand the subtleties of TeX programming, but it is not always easy. For example, in the following code : \def\first{abc} \def\second{abc} \ifx\first\second OK!\else false \fi I ...
12
votes
2answers
136 views

What would be a suitable replacement for ifcsname in a non-etex situation?

From the pgfkeys.code.tex file: % This is useful: \def\pgfkeys@ifcsname#1\endcsname#2\else#3\fi{\expandafter\ifx\csname#1\endcsnam e\relax#3\else#2\fi}% \ifx\eTeXrevision\undefined% \else% ...
11
votes
2answers
380 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, ...
2
votes
1answer
74 views

How to check if a fragile macro value is empty or will not create text with plain TeX conditionals?

A similar question has been already asked here, but has not been answered yet for fragile commands such as eg. \@author? What would be the right way to get detect if \author has been called with a ...
10
votes
3answers
437 views

Check for empty macro argument

There is a description on how to check whether the argument of a macro is empty in What does \ifx\\#1\\ stand for? Unfortunately, I can't get any of the examples there to work. Among others, I have ...
5
votes
2answers
274 views

Boolean based environments

I know that this question is basically identical to that of Hide custom environment content based on boolean Except I want to avoid using the environ package, which, otherwise, would be the best ...
17
votes
1answer
558 views

How does TeX handle \else and \fi in conditionals?

I have seen packages using \def\afterelse#1\else#2\fi{\fi#1} which TeX seems happy to accept. But on taking the first branch, isn't TeX expecting \else instead of \fi? That is, isn't the following ...
10
votes
5answers
775 views

Teach me how to use \loop…\repeat constructs, etc

The following code snippet is a part of the solution given here: \def\IncludeOutput#1{ \parskip=2mm \def\examplename{#1} \pdfximage{\Directory/\examplename.pdf}% ...
9
votes
1answer
805 views

Handling \ifnum's “! Missing number, treated as zero.”

Let's say I have a variable, which may - or may NOT - be a number; how can I handle the cases when it isn't a number, without crashing with "! Missing number, treated as zero." ? In practical terms, ...
1
vote
2answers
558 views

String equality in \ifx conditional using output from concatenating macros

Please consider the following MWE - where I try to test for string equality between an parameter, and a test string prepared by a macro: \documentclass{article} \def\testStr#1{IOIO#1} ...
7
votes
2answers
597 views

How to check if a value is not equal using TeX conditionals?

I have a value defined in a macro, e.g.: \mymacro{apple} I need to check if a value does not equal a string, e.g.: IF #1 NOT EQUAL TO "apple" THEN PRINT "It is not a apple, it is #1." FI I ...
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 ...
33
votes
4answers
4k views

How to check if a macro value is empty or will not create text with plain TeX conditionals?

I have a macro which holds some text value, e.g. \macro{some text}. If it is empty, I would like for nothing to appear in the document, but it has some content of any kind which would produce text ...
8
votes
3answers
446 views

How to make a plain TeX conditional which checks if some specific numbers are present?

I need to make a simple conditional in a macro. If #1 is "1", "2", "3", "4", "5", or "6", then print Text: #1, otherwise, do nothing, even if it is empty. I tried this code: \ifnum#1=1 Text:~#1 ...
2
votes
1answer
263 views

How to compare two counters using TeX conditionals in ConTeXt?

I have two counters, \definenumber[words] and \definenumber[items]. I have tried to use TeX conditionals to compare them, e.g.: \ifnum\getnumber[words]=\getnumber[items] This will print some text ...
1
vote
1answer
245 views

Unexpected conditional branch with \ifdim

I have a problem with the following code: \newtoks\sectoks \sectoks={\noindent} \newtoks\subsubjectstyle \subsubjectstyle={\emitsectglue 1\the\sectoks} \newtoks\postsectoks ...
22
votes
6answers
3k views

What is \iffalse for?

So this is something I've wondered about for a while. What use is a conditional that always evaluates to false? I guess it's a neat way to "hide code from LaTeX" but apart from that, is it ever ...
17
votes
3answers
292 views

Where can I find a documentation for low-level tex conditionals like \if?

I want to understand low-level stuff like \if better. I'm not looking for some random examples off Google showing me some statements but a specification of some sort - something mean and unforgiving. ...
15
votes
3answers
3k 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.
23
votes
2answers
4k views

How to form “if … or … then” conditionals in TeX?

I feel really stupid for asking this, but how do you form more complex if conditionals in TeX? I'm looking for something like: \ifnum\x=1 OR \ifnum\x=14 {do this} \else {do that} \fi I ...
7
votes
3answers
201 views

Detecting which version of the LaTeX format is in use

ltxcmds provides \ltx@ifpackagelater, \ltx@ifclasslater, \ltx@iffilelater and there is \@ifpackagelater - but is there any "if TeX later" or some other way to (automatically) get the used TeX version ...
10
votes
5answers
605 views

Test if token is a control sequence

Sometimes more complex (La)TeX macros test the next input token and branch dependent on its type. I'm aware how to test for catcodes and character codes, but sometimes I like to handle control ...
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 ...
23
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 ...
26
votes
6answers
3k views

Test whether token list is empty

Is it possible to test whether a token list register is empty without expanding it?