[texhax] What's wrong with \catcode`\_=12

Heiko Oberdiek heiko.oberdiek at googlemail.com
Thu Oct 6 02:53:00 CEST 2011


On Thu, Oct 06, 2011 at 08:29:21AM +0800, Yue Wu wrote:

> I want to define a macro to let _ can be directly used in it but failed,
> The minimum failed example:
> 
> \def\mytest#1{{\chardef\_=95 \catcode`\_=12 #1}}
> \mytest{foo_bar}

The definition text of \mytest is executed *after* the macro's
argument is read and tokenized. Therefore the catcode change
comes too late. If \mytest is not used inside the argument
of other commands, the catcode can be changed the following way:

\def\mytest{%
  \begingroup
  \catcode`\_=12 %
  \mytesti
}
\def\mytesti#1{%
  \endgroup
  #1%
}
\mytest{foo_bar}

What's the purpose of "\chardef\_=95"?
That "\_" prints as "_"?

\def\mytest{%
  \begingroup
  \catcode`\_=12 %
  \mytesti
}
\def\mytesti#1{%
  \chardef\_=95 %
  #1%
  \endgroup
}
\mytest{foo_bar\_bla}

Yours sincerely
  Heiko Oberdiek


More information about the texhax mailing list