[texhax] strange problem with a sty file and \catcode

Uwe Lueck uwe.lueck at web.de
Fri Nov 19 12:06:47 CET 2010


A really nice and funny observation, very instructive for learning to deal with category code changes.

\usepackage, before actually reading the package, looks for a "[" after its mandatory argument, in case there is a version request. If the command containing the new "letter" follows instead, the new command is truncated at the "new letter", because the category code change within the package hasn't been executed yet. I.e., at looking for a "[", TeX tokenizes what is ahead of the mandatory argument, and does this with the category code of the "new letter" still being the one from before the package. So after reading the package, TeX processes the next token, which is the command truncated at the "new letter".

When an empty line follows, what \usepackage finds instead of "[" is \par (I think).

What Phil Taylor may like (or he certainly would have suggested it if he had already looked at this): You can place a \relax right after the mandatory argument of \usepackage to get around this funny problem. The documentation of the new package should tell something like this.

Cheers, 

    Uwe.


"Vafa Khalighi" <vafakhlgh at gmail.com>, 19.11.2010 09:26:09:
> If i create a simple sty file, say mypackage.sty which includes:
[...]
> \catcode`$=11
> \newcommand*{\eqcommand}[2]{%
> \expandafter\@ifdefinable  \csname#1\endcsname{%
> \expandafter\let\csname#1\expandafter\endcsname\csname#2\endcsname}}
> \eqcommand{new$command}{newcommand}
[...]
> then this tex file gives error:
>
> \documentclass{article}
> \usepackage{mypackage}
> \new$command\test{Test}
[...]
> ! Undefined control sequence.
> <recently read> \new
> 
> l.3 \new
> $command\test{Test}
> ?
> 
> Then I I change my tex file so that I leave a blank line after \usepackage{mypackage}, no error happens:
> 
> \documentclass{article}
> \usepackage{mypackage}
>  
> \new$command\test{Test}
> \begin{document}
> This is a test\TeX
> \end{document}
 
> but even without using a package, this gives no error:
>
> \documentclass{article}
> \makeatletter
> \catcode`$=11
> \newcommand*{\eqcommand}[2]{%
> \expandafter\@ifdefinable  \csname#1\endcsname{%
> \expandafter\let\csname#1\expandafter\endcsname\csname#2\endcsname}}
> \eqcommand{new$command}{newcommand}
> \makeatother
> \new$command\test{Test}
> \begin{document}
> This is a test\TeX
> \end{document}


More information about the texhax mailing list