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.

I was in correspondence with the author of a LaTeX package in which I volunteered code similar to the following (I have anonymised it so that it is not obvious what package):

\makeatletter
\newcommand{\savemacrostatewithname}[1]{%
    \expandafter\let%
        \csname saved@macro@state@named@#1\endcsname%
        \package@internal@macro%
}
\newcommand{\restoremacrostatewithname}[1]{%
    \ifcsname saved@macro@state@named@#1\endcsname%
    \else%
        \errmessage{Saved macro state named "#1" doesn't exist}%
    \fi%
    \expandafter\let%
        \expandafter\package@internal@macro%
        \csname saved@macro@state@named@#1\endcsname%
}
\makeatother

In their reply, one thing they said was that instead of writing #1 in your macros, you should write \detokenize{#1}, as it is safer. Why is it safer? What might go wrong if I don't?

share|improve this question
    
If you're sure that #1 will always be replaced with a string of ASCII characters, there's no difference. –  egreg Jan 18 at 13:41
    
@egreg 'Sure' is a risky business ;-) –  Joseph Wright Jan 18 at 15:26
    
@JosephWright People who monkey with category codes are on their own. –  egreg Jan 18 at 15:39
    
@egreg User input can legitimately contain ~ or _ with the underscore package loaded :-) –  Joseph Wright Jan 18 at 15:57
    
@JosephWright Let's say non TeX special characters, then; I don't consider underscore that creates more problems than it solves. –  egreg Jan 18 at 16:00
add comment

1 Answer

up vote 13 down vote accepted

If you have

\catcode`\!=\active
\def!{\hspace{2in} \fbox{boo} \hspace{2in}}

then \csname foo!\endcsname is an error as the name doesn't expand to a sequence of character tokens, but \csname\detokenize{foo!}\endcsname is same as \csname foo\string!\endcsname and makes the token with name foo!

share|improve this answer
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.