Saturday, 1 June 2013

Is a TeX macro defined?

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, if both macros are undefined,
false, if one macro only is defined.
Therefore, given a surely undefined macro \undefined and the macro \CheckMe, which may or may not be defined, I can use:
\ifx \CheckMe \undefined
  CheckMe is NOT defined
\else
  CheckMe is defined!
\fi
Is this correct? Can we define a macro ifdef? working like:
\ifdef CheckMe         %or \ifdef \CheckMe
  CheckMe is defined!
\else
  CheckMe is NOT defined
\fi
I tried with something similar:
\def\ifnotdef#1{\ifx \csname#1\endcsname \undefined}
but it doesn't work.

No comments:

Post a Comment