[texhax] Swapping the meaning of _ and \_

Suresh Govindachar sgovindachar at yahoo.com
Tue Apr 24 00:49:35 CEST 2012


The context for this question is plain TeX:

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 _

     \catcode`_=\catcount % reset meaning of _

However, the preceding is _not_ a complete solution, for the following

1) Using un-escaped _ results in a raised dot -- why doesn't it result in
just the _?

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.

3) What more needs to be done so that the un-escaped _ results in _?

4) Couldn't figure out how to get subscripts in math mode.

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.

So what's a good way to swap the meaning of _ and \_?



More information about the texhax mailing list