Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

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.)

share|improve this answer
    
Thanks Joseph. That fits exactly my needs since i need it in an expandable context. –  cjorssen Jan 27 '12 at 13:25
add comment

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.

share|improve this answer
    
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! –  tohecz Jan 27 '12 at 9:36
    
Thanks. However I prefer Joseph's solution since it is expandable. –  cjorssen Jan 27 '12 at 13:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.