[texhax] Swapping the meaning of _ and \_

Heiko Oberdiek heiko.oberdiek at googlemail.com
Tue Apr 24 01:21:42 CEST 2012


On Mon, Apr 23, 2012 at 03:49:35PM -0700, Suresh Govindachar wrote:

> 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 _
>      \ifnum\catcode`\_=\catcode`A\else%
>      \catcode`_=11%
>      \fi%
> 
>      \catcode`_=\catcount % reset meaning of _
> 
> 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 _?

It depends on the font encoding, OT1 is awkward, only in the
typewriter family (\tt/\ttfamily) you get the underscore.
This is fixed in T1 encoding, for example.

> 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.

Save the original \_ in a different macro:

\let\_\underscore

and use \underscore to get a underscore in OT1 with roman/sans serif
families.

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

Use fonts with a different font encoding (e.g. T1) that have
the underscore at the position of the underscore.

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

Happily TeX provides primitives for the function of catcodes 7 (^)
and 8 (_): \sp for superscripts and \sb for subscripts.

\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.

It depends on anybody else's code:
* Some packages don't use _ at all.
* For example, I have tried to make my packages that also support
  plain TeX robust in regard of unusual catcodes. At the beginning
  package pdftexcmds.sty sets the catcode of _ to 12 and at the
  end the previous value is restored.
* For other packages you have indeed to set the catcode of _
  to the standard value before loading the package and can
  restore it afterwards. I think for most packages this will be ok.
* But some packages might use _ as part of there user interfaces,
  that might conflict with your redefinitions.

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

One way (untested):

\edef\RestoreCatcodeUnderscore{%
  \def\noexpand\RestoreCatcodeUnderscore{%
    \catcode95=\the\catcode95\relax
    % or \catcode`\noexpand\_=\the\catcode`\_\relax
  }%
}
\RestoreCatcodeUnderscore
% Now macro \RestoreCatcodeUnderscore is available that
% restores the previous catcode of the underscore.

\def\SetCatcodeUnderscore{%
  \catcode95=11 %
}

\let\SavedUnderscore\_
\let\_\sb

% and both can be put together:
\def\RestoreUnderscores{%
  \RestoreCatcodeUnderscore
  \let\_\SavedUnderscore
}
\def\SetUnderscores{%
  \SetCatcodeUnderscore
  \let\_\sb
}

\SetUnderscores

% loading a package
\RestoreUnderscores
\input foo
\SetUnderscores

Yours sincerely
  Heiko Oberdiek


More information about the texhax mailing list