How can I check in LaTeX (or plain TeX) whether a command exists, by name?
Is this what you are looking for:
\documentclass[12pt]{article}
\newcommand{\checkfor}[1]{%
\ifcsname#1\endcsname%
... command '#1' exists ...%
\else%
... command '#1' does not exist ...%
\fi%
}
\begin{document}
\checkfor{CommandName}\par
\checkfor{section}
\end{document}
The etoolbox
provides two macros for this:
\ifdef{<control sequence>}{<true>}{<false>}
Expands to <true> if the <control sequence> is defined, and to <false> otherwise.
Note that control sequences will be considered as defined even if their meaning
is \relax. This command is a LaTeX wrapper for the e-TeX primitive \ifdefined.
\ifcsdef{<csname>}{<true>}{<false>}
Similar to \ifdef except that it takes a control sequence name as its first argument.
This command is a LaTeX wrapper for the e-TeX primitive \ifcsname.
The LaTeX kernel standard macro here is \@ifundefined
, used as
\@ifundefined{foo}
{%
% \foo not defined
}
{%
% \foo defined
}%
In earlier LaTeX2e releases, this test was not 'expandable' and would cause \foo
to be equal to \relax
if it had not previously been defined. However, the kernel now uses essentially the same code as in Peter's answer, which means that these issues are no longer present. (The change is possible as the LaTeX2e kernel now requires and uses e-TeX.)