23

Are there any commands containing "if", similar to \ifnum, preferably in LaTeX (rathar than TeX)? For example, is there anything like \ifstring? (I am not after \ifstring only, I want to know which options are available, if any).

2
  • See also: LaTeX conditional expression
    – Leo Liu
    Jul 13, 2011 at 5:10
  • It's now a dozen year later and I still can't find an answer to the question of whether there is an \ifstring that will test if something is a string. Note that I'm not asking to test if two strings are equal - I'm asking for something that would behave as follows: \ifstring{this is \textbf{bold}}{true}{false} would evaluate to false \ifstring{this is a string}{true}{false} would evaluate to true. \ifstring{this has math $a=b$ in it}{true}{false} should be predictable. Basically if the argument to \ifstring contains any macros that need expansion, then it should evaluate to false.
    – mccurley
    Mar 13, 2023 at 6:09

5 Answers 5

30

The \if... commands are mostly TeX primitives (with \newif it's possible to define other conditionals).

There is something like \ifstring, but it uses \ifnum; the command \pdfstrcmp takes two strings as argument and compares it, returning -1 if the first precedes the second (in the lexicographic order based on ASCII codes), 0 if the strings are equal and 1 otherwise. The usual ways to exploit it are

\ifnum\pdfstrcmp{string1}{string2}=0
   <code if the strings are equal>
\else
   <code if the strings are different>
\fi

or

\ifcase\pdfstrcmp{string1}{string2}%
   <code if the strings are equal>
\or
   <code if string2 comes after string1>
\else
   <code if string1 comes before string2>
\fi

In XeTeX there's \strcmp that does the same; loading pdftexcmds one can use \pdf@strcmp with pdftex, xetex and luatex.

In the pseudocodes above "string1" and "string2" can be also macros, which will be expanded similarly to what happens with \edef and at the end (when no more expansions can be performed) the tokens are "detokenized" for the comparison (so \relax becomes a six character string followed by a space; for example \pdfstrcmp{\relax}{\string\relax\space} returns 0).

4
  • 2
    @egreg: if I recall correctly, they don't have to expand to characters only: the result of their full expansion is converted to a string (I think, through the same code as \detokenize). Jul 13, 2011 at 0:43
  • 1
    @Bruno: I'll correct my answer.
    – egreg
    Jul 13, 2011 at 7:21
  • Nitpick regarding the parenthetical remark in the first paragraph: \newif doesn't really make new conditionals. It merely provides a handy way to assign one of the existing conditionals \iffalse and \iftrue to a control sequence. Jul 13, 2011 at 15:38
  • 1
    @Harald: it depends on the point of view. They are new for the programmer, they aren't for TeX, which sees only \iftrue or \iffalse.
    – egreg
    Jul 13, 2011 at 15:43
17

The etoolbox package has a lot conditional commands which goes beyond those TeX primitives. Have a look at the manual you will find they very usefull.

1
  • 5
    In particular there is \ifstrequal{str1}{str2}{true}{false}, and many many others. The etoolbox is definitely a must for anyone trying to do any kind of “programatic” work in (La)TeX. Jul 13, 2011 at 8:03
10

An alternative is expl3. The basic language layer of LaTeX3 provides several conditionals. To handle strings you can use the function \str_if_eq:nnTF. Next to this conditional expl3 provides also a case function for strings named \str_case:nn(TF).

\str_if_eq:nnTF has the following syntax:

\str_if_eq:nnTF { <test string 1> } { <test string 2> }
 { <True Code> }
 { <False Code> }

\str_case:nn(TF) has the following syntax:

\str_case:nnTF { <test string> }
 { 
   { string case 1 } { code case 1 }
   { string case 2 } { code case 2 }
   ...
   { string case n } { code case n }
 }
 { <additional TRUE code> }
 { <FALSE case> }

(Either one of the T or F branches may be omitted provided the matching argument letter is also.)

The complete manual

1
  • 1
    Note that ultimately this is using the same \(pdf)strcmp primitive discussed by egreg in his answer.
    – Joseph Wright
    Dec 12, 2015 at 14:28
4

I have been using the xstring package for these kind of situations. See for example Using xstring to check .tex file name.

0

To compare 2 strings, that is string-version of \ifnum of TeX:

%https://tex.loria.fr/general/texbytopic.pdf
\def\ifstring#1#2{%
\def\testa{#1}%
\def\testb{#2}%
\ifx\testa\testb%
}%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .