It's one of the uncountable ways to check whether an argument is empty. Almost all of them suffer of some drawback: in the case at hand, the argument mustn't be \\
.
What does \ifx
do? It compares the two tokens following it, without expanding them. The test return success if both tokens are equal in the sense of \meaning
or \show
:
if they are characters (or control sequences \let
to a character), they must represent the same (character code, category code) pair
if they are control sequences (but not \let
to a character), they must have the same meaning; the cases are too many to list them all.
Before analyzing the macro, we should know something about TeX conditional: the abstract form is
\ifZ<test><true text>\else<false text>\fi
where \ifZ
denotes one of the primitive conditionals. The <test>
can take many forms, depending on \ifZ
(it's empty, in some cases); what is important is that the <true text>
is everything that goes from the end of <test>
to \else
(or \fi
if \else
is missing).
So let's see what happens when we call \mycommand{abc}
, that is, the default argument, which is empty, is substituted to #1
: the tokens that TeX sees are
\ifx\\\\<true>\else<false>\fi
Of course the test is true, because the tokens that follow \ifx
are both \\
.
When we call \mycommand[xyz]{abc}
, instead, TeX sees
\ifx\\xyz\\<true>\else<false>\fi
and TeX compares \\
to x
and they are different. But now the <true text>
is
yz\\<true>
(remember: the <true text>
is what goes from the test to the \else
) and it will be ignored altogether.
This test is quite safe, because it doesn't do expansion; however it might fail miserably if the user calls \mycommand[\\]{abc}
, so often different delimiter tokens are used, which are supposed not to creep into user's input (for instance \uchyph
or \vfuzz
). Another similar test might be
\if\relax\noexpand#1\relax
with the caveat that \if
does expand what's next until two unexpandable tokens remain. The first token is \relax
, which is unexpandable. The second one will be what becomes of the expansion of \noexpand
, that is, the first token of #1
(if non empty) but made unexpandable; if #1
is empty, then \noexpand
applies to \relax
, resulting again in \relax
because it's unexpandable.
In both cases some token is reserved (\\
in the first case, \relax
in the second one). The safest test is
\if\relax\detokenize{#1}\relax
because the expansion of a non empty #1
can never give a first token equivalent to \relax
: even if #1
is \relax
, \detokenize{\relax}
will give the string \relax
(the first token of which is the character \
).
Again, if #1
is empty, the test compares \relax
to \relax
; if #1
is not empty, everything will be ignored up to the \else
. Of course it requires that an engine with the e-TeX extensions is run: it won't work with "Knuth TeX".
Best Answer
In your example,
\nop
is defined to take a single argument and do nothing with it. As such,\nop
literally translates to "perform no operation", or "gobble your argument". In fact, there exists a similar core macro\@gobble
(and friends) which does exactly the same (fromlatex.ltx
:The first gobbles a single argument, making
\@gobble{<anything>}
expand to nothing. The second gobbles two arguments, making\@gobbletwo{<anything>}{<anything>}
expand to nothing, and so on.What's the use case here? Well, perhaps you define a macro that writes something in the margin, like a "to do" note:
Now, later in your production, you decide that this operation is no longer valid/useful. So, you can make
\mymacro
act like\nop
and do nothing, which allows you to leave your code as-is:or, at definition in your preamble, change it to a no-op: