Wissensdatenbank

Lösung

### \if Constructs

The standard "\if" command does not test for a binary result, rather it always performs a character comparison on two arguments after macro expansion. As a result the following \if construct will work (sometimes):

\if \@testmacro \@relax \@relax
Commands to use if the macro is undefined
\else
Commands to use if the macro IS defined
\fi

The difficulty with this construct is that will fail if \@testmacro has certain special characters in it (mainly the comment character '%') or some type of unusual expansion for which the first two characters match. It also suffers from a condition where characters left over after the comparison can actually fall into the true branch of the statement and be executed as commands. All in all while this approach can be use, it is prone to difficulties.

### \ifx Constructs

Given the problems associated with the intuitive use of \if there is a better method of testing for macro existence using the little known (yet standard) TeX \ifx directive. This directive compares two arguments, but does NOT expand macros. This has the effect of causing a match on two macros only when:

• Both arguments are macros AND their first level expansion is identical AND they have the same status with respect to \long and \outer

One consequence of this statement is that two undefined macros will cause a match. Hence the following construct will ALWAYS correctly test whether a given macro is or is not defined:

\ifx \@testmacro \@empty
Code to be executed if the macro is undefined
\else
Code to be executed if the macro IS defined
\fi
Artikeldetails
 Artikel-ID: 16 Kategorie: pronego Datum (hinzugefügt): 05.02.2009 10:49:43 Aufrufe: 739

<< Zurück