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 '13 at 01:55\protectshere and there, but haven't guessed the correct sequence yet. – Peter Grill Jan 21 '13 at 02:00MyDef, TeX thinks the\csnameshould have ended by now! and triggers that error so that needs to be hidden somehow. And what should be the error message by the way? Because that's not a valid token anyway. – percusse Jan 21 '13 at 02:17\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 '13 at 10:16\protect(one could get rid of it); but\textbfdoes many other unexpandable things that can't go inside\csname...\endcsname. – egreg Jan 21 '13 at 10:56\text...commands pose problems in expansion contexts but the details are not yet clear to me. Maybe I should ask a question... – cgnieder Jan 21 '13 at 11:06