I’m adding this answer only to justify my assertion that the present question is “somehow related” to How can I check whether two control sequences have the same name? (see the comments).
Werner’s solution is great, but it has a slight drawback and a characteristic that, depending on what you want to do, can turn out to be a useful feature, or not.
The useful feature, that you might want to disable, is that it finds the control sequence being sought for at any level of expansion; the drawback is that the test is not purely expandable, since it is executed in the stomach.
The following approach uses two steps: in the first step, that requires digestion (since it needs to execute assignments), the name to be searched for is defined; having done that—and this is the second step—a purely expandable test can be performed to find out whether the replacement text of another macro contains the control sequence that has been declared before. The test does not expand the replacement text any further (that is, only one level of expansion is considered).
\documentclass{article}
\makeatletter
% Check for availability of names:
\@ifdefinable\MacroDoesNotContainSavedName{}
\@ifdefinable\@MacroDoesNotContainSavedName{}
\newcommand*\DefineMacroNameToBeSearched[1]{%
\def\@MacroDoesNotContainSavedName##1#1##2\@@@{%
\ifx\@empty##2\@empty % if ##2 is empty
}%
\def\MacroDoesNotContainSavedName##1{%
TT\fi
\expandafter\@MacroDoesNotContainSavedName##1#1\@@@
}%
}
% \newcommand*\checkdefinitions{%
% \show\MacroDoesNotContainSavedName
% \show\@MacroDoesNotContainSavedName
% }
\makeatother
\newcommand*{\DonaldDuck}{Quack!}
\newcommand*{\Duckburg}{%
Ducks living in Duckburg: \UncleScrooge, \DonaldDuck,
\Huey, \Dewey, \Louie, and many others.%
}
\newcommand*{\Micetown}{%
Mices living in Micetown: \MickeyMouse, \MinnieMouse,~\ldots
}
\newcommand*{\MinnieMouse}{%
I'm secretly in love with \DonalDuck!%
}
\begin{document}
\DefineMacroNameToBeSearched{\DonaldDuck}
% \DefineMacroNameToBeSearched{\MickeyMouse}
% \DefineMacroNameToBeSearched{\Huey}
% \checkdefinitions
Test for Duckburg: the macro
\if\MacroDoesNotContainSavedName{\Duckburg}%
DOESN'T CONTAIN%
\else
CONTAINS%
\fi
\ the saved name.
Test for Micetown: the macro
\if\MacroDoesNotContainSavedName{\Micetown}%
DOESN'T CONTAIN%
\else
CONTAINS%
\fi
\ the saved name.
\end{document}
You may uncomment various section of this code to see different behaviors. Some of the commented-out code serves diagnostic purposes.
This code requires nothing more than the original Knuth’s TeX. The test is completely independent of the meaning, and type (macro, primitive command, …), of the control sequence being searched for, which can also be undefined. Drawback: you cannot reliably test for the presence of a macro named “\@empty
”; this can be cured in the usual way with \detokenize
(without destroying the property of the test of being purely expandable).
\noexpandarg
feature ofxstring
could provide some help – user31729 Jul 18 '15 at 15:00