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}
You have to isolate the first token in #1
from the rest and uppercase it; the fact that \uppercase
doesn't expand anything and puts back the token list into the input stream after its operation can be exploited in the following way:
\newcommand{\mycommand}[1]{\mycommandaux#1\relax}
\def\mycommandaux#1#2\relax{%
\uppercase{\expandafter\gdef\csname #1}#2\endcsname{the macro #1#2 expanded}%
}
\mycommand{abc}
\show\Abc
Of course an empty argument to \mycommand
will result in an error.
If you want to provide a definition, say
\mycommand{abc}{Something else}
to be equivalent to \def\Abc{Something else}
, just leave out the tokens after \endcsname
\newcommand{\mycommand}[1]{\mycommandaux#1\relax}
\def\mycommandaux#1#2\relax{%
\uppercase{\expandafter\gdef\csname #1}#2\endcsname
}
\mycommand{abc}{Something else}
\show\Abc
The command \uppercase
is a bit stranger than other TeX primitives. Indeed the <general text>
it requires as argument first does a travel down TeX's stomach to be "regurgitated" after each character token has been transformed using the \uccode
vector: if a character has positive \uccode
, TeX will transform it into that character (the category code is unchanged). So, for instance, TeX is setup so that \uccode`a=`A
and so an a
becomes an A
after regurgitation. Non character token are left unchanged and no expansion is performed.
Here's the working in slow motion (first version).
\mycommand{abc}
Here #1
is abc
\mycommandaux abc\relax
Here #1
is a
and #2
is bc
(argument #2
is delimited by \relax
, while #1
is undelimited, so the only first token is grabbed)
\uppercase{\expandafter\gdef\csname a}bc\endcsname {the macro abc expanded}
Tex executes the \uppercase
and puts back
\expandafter\gdef\csname A
in the input stream.
\expandafter\gdef\csname Abc\endcsname{the macro abc expanded}
\gdef\Abc{the macro abc expanded}
Et voilĂ .
A variant with \newcommand
instead of \gdef
, that makes it easy to define macros with arguments:
\newcommand{\mycommand}[1]{\mycommandaux#1\relax}
\def\mycommandaux#1#2\relax{%
\uppercase{\expandafter\newcommand\csname #1}#2\endcsname
}
\mycommand{abc}{Something else}
\show\Abc
\mycommand{uvw}[1]{Something else with #1}
\show\Uvw
With \gdef
the standard parameter text should be used.
\newcommand{\mycommand}[1]{\mycommandaux#1\relax}
\def\mycommandaux#1#2\relax{%
\uppercase{\expandafter\gdef\csname #1}#2\endcsname
}
\mycommand{abc}{Something else}
\show\Abc
\mycommand{uvw}#1{Something else with #1}
\show\Uvw
Best Answer
The problem with this approach is that is not completely expandable, as it relies on assignments to
\escapechar
, while being independent of the value it has at the moment the test is performed.This test distinguishes the last case, which is not possible with
\ifcat
. Nor with\ifcsmacro
of etoolbox, it seems.