I have a macro \command to ensure that the names of macros in my documentation are consistently formatted. Been work great, until I had an urge to procrastinate and had this brilliant idea that I can easily catch most spelling errors of the macro names, by checking that the macro exists.
Seems simple enough. So I added:
\ifcsname#1\endcsname%
\else%
\par\textbf{\textcolor{red}{\textbackslash#1} is not defined.}
\fi%
to the macro that does the formating, where #1 is the name of the macro. This seems to work fine in the basic case as illustrated by the MWE below that produces:

and correctly tells me that bfseriess and \foo are not defined.
However, if I attempt to use this in a nested fashion
\command{foo=\command{MyDef}}
I get the error message
Missing \endcsname inserted. <to be read again> \protect l.25 \command{foo=\command{MyDef}}
Question:
I have a feeling that the solution is in the question listed in the references but I don't know exactly how to \protect things. So, what changes can I make to the \command macro so that I can uncomment the last line in the MWE and the proper error messages.
Reference:
Code:
\documentclass{article}
\usepackage{xcolor}
%% The last line should not produce any error
%% messages (red text) if these two are defined.
%
%\newcommand*{\foo}{}%
%\newcommand*{\MyDef}{}%
\newcommand{\command}[1]{%
\textbf{\textbackslash#1}%
%% Since this is used to typeset macro names, we
%% can check for typos by ensuring the macro exists
\ifcsname#1\endcsname%
\else%
\par\fcolorbox{red}{red!20}{\textcolor{blue}{\textbackslash#1} is not defined.}
\fi%
}%
\begin{document}
To bold text use \command{textbf} or \command{bfseriess}.
A useful token is \command{foo}.
This token needs to be set with:
%\command{foo=\command{MyDef}}
\end{document}



\ifcsname. For example, if you change the definition to\newcommand\command[1]{(#1 is \ifcsname#1\endcsname \else not \fi defined)}then the document compiles correctly. – Aditya Jan 21 at 1:55\protectshere and there, but haven't guessed the correct sequence yet. – Peter Grill Jan 21 at 2:00\protectthat's hidden in the definition of\textbf. You can trigger the error message e.g. with\DeclareRobustCommand\foo{}\csname\foo\endcsname– cgnieder Jan 21 at 10:16