19

I am using \if condition in a custom command in my LaTeX document. I would like to know where I can find some good explanation about \if command. I followed a Wikibooks \if page but it's just an example; there are not any explanation.

41

A very good reference for conditionals is contained in Chapter 13 Conditionals of TeX by Topic. Specially, conditionals in TeX have one of the following two forms

\if...<test tokens><true text>\fi
\if...<test tokens><true text>\else<false text>\fi

where \if here is used generically to reference a whole host of conditionals (\if, \ifcat, \ifx, \ifdim, \ifnum, ...).

Let's use your example:

\documentclass{minimal}
\begin{document}

\ifx\mycmd\undefined
undefed
\else
  \if\mycmd1
  defed, 1
  \else
  defed
  \fi
\fi

\def\mycmd{1}

\ifx\mycmd\undefined
undefed
\else
  \if\mycmd1
  defed, 1
  \else
  defed
  \fi
\fi

\def\mycmd{0}

\ifx\mycmd\undefined
undefed
\else
  \if\mycmd1
  defed, 1
  \else
  defed
  \fi
\fi

\end{document}

It uses a nesting of \ifx and \if conditionals. Let's look at the outer \ifx definition first. From section 13.2 Character and control sequence tests (specifically section 13.2.3 \ifx),

Equality of tokens is tested in a stronger sense [...] by

\ifx<token1><token2>

[...]
Control sequences are [...] equal if they are macros with the same parameter text and replacement text, and the same status with respect to \outer and \long. For example,

\def\a{z} \def\b{z} \def\c1{z} \def\d{\a}
\ifx\a\b %is true
\ifx\a\c %is false
\ifx\a\d %is false

Tokens following this test are not expanded.

So, in your example, we start with \ifx\mycmd\undefined. This is true since both \mycmd and \undefined do not exist, hence the output undefed. Subsequent tests \ifx\mycmd\undefined are both false, as \mycmd has been \defined to be something (and is there not undefined and different from \undefined).

Now look at the \if tests. From TeX by Topic (specifically section 13.2.1 \if),

After \if TeX will expand until two unexpandable tokens are obtained, so it is necessary to prefix expandable control sequences and active characters with \noexpand when testing them with \if.

We can ignore the first since \mycmd was undefined. Before the second test, \def\mycmd{1} is executed, so \if\mycmd1 expands \mycmd to 1, leaving you with \if11. This tests the character codes of 1 against 1, and they are equal (of course), hence the output defed, 1.

Lastly, after \def\mycmd{0}, we end up with \if01 (after expansion of \mycmd in \if\mycmd1), which is false as 0 is not 1. Hence, the output defed.

| improve this answer | |
  • Thank you very much! If I was able to I would upvote your poste. Thank you again – JuliusII Jan 7 '16 at 21:25
  • maybe one could add to innocent people that mastering the use of TeX conditionals takes quite some time and there are many surprises along the road (not possibly covered in the space for a single answer on this site). – user4686 Jan 10 '16 at 19:56
  • please provide a few simple examples instead of copypaste the reference. – qrtLs Nov 6 at 13:15
  • @qrtLs: The reference includes examples. Perhaps you can be more specific as to how this currently doesn't meet your needs. – Werner Nov 6 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.