[texhax] Swapping the meaning of _ and \_
Philipp Stephani
p.stephani2 at googlemail.com
Tue Apr 24 01:29:10 CEST 2012
2012/4/24 Suresh Govindachar <sgovindachar at yahoo.com>:
> Goal: In my source .tex file and in my own macros, I would like to use _ as
> an ordinary character and use \_ for subscripts in math mode.
>
> What I tried: Following some ideas in David Solomon's The Advanced TeXbook,
> I toyed with the idea of starting and ending all my files with the following
> blocks of code:
>
> \chardef\catcount=\catcode`_% store current meaning of _
> \ifnum\catcode`\_=\catcode`A\else%
> \catcode`_=11%
> \fi%
>
> \catcode`_=\catcount % reset meaning of _
That's fine (but you don't need the percent signs at the end of the lines).
> However, the preceding is _not_ a complete solution, for the following
> reasons:
>
> 1) Using un-escaped _ results in a raised dot -- why doesn't it result in
> just the _?
That's what happens to be at the glyph position corresponding to _ in
the current font. Try loading a font that has an underscore at this
position, like cmtt or ecrm.
>
> 2) Getting a _ with escaped _ is a tad bit more complicated than it used to
> be because now TeX needs to know when the control sequence \_ ends.
You should turn _ into an other character (catcode 12) instead, then
the control sequence \_ is unambiguous (like \& etc.).
> 4) Couldn't figure out how to get subscripts in math mode.
\let\_\sb
>
> 5) I understand that whenever I include anybody else's code, I would need to
> change the meaning of _ to its default value before doing the \input. After
> the \input line, I can redefine _ to be what I want it to be. What I don't
> know if the preceding suffices to allow me to use macros from that file, or
> if I need to set _ to its default value before using macros from that file
> too? I don't think so, but I am not sure.
Catcodes are assigned during the input stage, so as long _ has the
expected catcode while loading the file, you should be able to use
macros from that file (except they use \scantokens).
More information about the texhax
mailing list