# Is it possible to temporally make undefined \csname control sequence not silently expand to \relax?

Context

Consider the following situation.

\documentclass{minimal}
\makeatletter
\expandafter\def\csname/test\endcsname{%
\foo{bar}}
\def\mymacro#1{%
% Note the / *after* #1
\expandafter\expandafter\expandafter\mymacro@i\csname/#1/\endcsname}
\def\mymacro@i\foo#1{%
\def\temp{#1}%
\show\temp}
\mymacro{test}
% Error: Use of \mymacro@i doesn't match its definition.
\begin{document}
\end{document}


I made a typo in the definition of \mymacro adding a / before \endcsname. The error shown made me believe that /test was improperly expanded by the three \expandafter but I was wrong. As /test/ is not defined, it silently expands to \relax and yes TeX is right saying that Use of \mymacro@i doesn't match its definition since I feed it with a \relax instead of a \foo.

Question

This behavior of \csname defined control sequence (that is expanding to \relax when undefined) is really handy but sometimes I'd like to deactivate it and maybe issue an Undefined control sequence error. Is it possible?

-

You cannot alter the behaviour of \csname, but you can use e-TeX's \ifcsname to issue an error:

\csname
\ifcsname#1\endcsname
\else
\expandafter\UndefinedName
\fi
#1%
\endcsname


where \UndefinedName is deliberately not defined. TeX will then issue an error complaining about Undefined control sequence, followed by #1 (which is what you actually want to complain about).

(Unlike \@ifundefined, \ifcsname is expandable and does not add to the hash table.)

-
Thanks Joseph. That fits exactly my needs since i need it in an expandable context. – cjorssen Jan 27 '12 at 13:25

You can check the command for existence and invoke the error yourself.

\@ifundefined{/#1/}{%
\GenericError{}{%
Command /#1/ undefined!
}{%
See the definition of \protect\mymacro!
}
}{}%


The \@ifundefined macro uses \csname so it will define the macro as \relax. I don't think that this is an issue (we invoke the error anyways), but you can surround the code by \begingroup ... \endgroup to make the definition local.

-
LaTeX2e's \@ifundefined will leave the macro equal to \relax unless you use grouping, if it was previously undefined. – Joseph Wright Jan 27 '12 at 9:21
@JosephWright I know, but I don't think it is that an issue as we invoke the error message. However, I should have mentioned it, thanks! – yo' Jan 27 '12 at 9:36
Thanks. However I prefer Joseph's solution since it is expandable. – cjorssen Jan 27 '12 at 13:27