[comp.text.tex] help: modifying \tt

kelem@castor.UUCP (03/05/91)

I need some help in creating a command similar to \tt that changes the
\catcode of _ to 11, the same as a letter.  I did this:
 
\def\code{\catcode`\_=11\ptt}
 
The command works fine for every place I've tried (plain text,
indices) except for section titles, e.g.
\section{{\code foo_bar} non-typewriter text}
 
Is there an easy way to get this to work?
 
Thanks,
Steve Kelem     (408)879-5347           xilinx!kelem@pyramid.com
Xilinx                                  FAX: (408)559-7114
2100 Logic Drive
San Jose, California 95124

MJD@MATH.AMS.COM (Michael Downes) (03/05/91)

Steve Kelem writes:
 
> I need some help in creating a command similar to \tt that changes the
> \catcode of _ to 11, the same as a letter.  I did this:
>
> \def\code{\catcode`\_=11\ptt}
>
> The command works fine for every place I've tried (plain text,
> indices) except for section titles, e.g.
> \section{{\code foo_bar} non-typewriter text}
 
I think essentially what you want is to retain the normal meaning of the
underscore character (start a subscript) for math formulas, while making
it an ordinary printing character outside of math. This is a perfect
application for the special mathcode of "8000 (TeXbook, p. 155).
An example file is appended below.  There are a couple of
side effects: if you forget or mistype a $ at the beginning of
a math formula (e.g., 4n_1$ where you intended $ instead of 4),
the _ character will no longer generate a warning about the missing
$ sign; and if you want to use \code inside math then you had
better define it as follows:
 
       \newcommand{\code}[1]{\mbox{\tt#1}}
 
instead of as in the example file (and you'll need to explicitly specify
\scriptsize if you use \code in a sub or superscript).
 
%%%%%%%%%%%%% cut here %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% underscore.tex
\documentstyle{article}
 
% Give _ a special mathcode that will make it active, but only in math
% formulas.
\mathcode`\_="8000
 
% Now define active character _ to do a subscript operation.
\catcode`\_=\active
\let _ =\sb
\show\sb % Return to continue
\show _ % Return to continue
 
%      Now give _ the catcode that we want to be in effect for normal
%      (non-math) text).  The previous definition will thus remain
%      inactive until we enter a math formula.
\catcode`\_=12 % or maybe 11?
 
%      Now the definition of \code can be simplified since we
%      no longer need to change the catcode of _
\newcommand{\code}[1]{{\tt#1}}
 
\begin{document}
\title{Horocyclic K\"ahler numbers of \(h_1(R)/SU_1(2,F)\)}
\author{Who Else}
\maketitle
 
\tableofcontents
\section{Test of \code{code_hack_ery}}
 
If this \code{code_hack_ery} works it will be splendid.
Now, what about subscripts in math? For all \(g\in X_\zeta\),
the sequence \(g_1, g_2, g_3, g_5, \dots, g_n\), where \(n\) is
a Fibonacci number, is flexotopic to the set of surreal integers.
 
\section{And math such as \(a_b\) in an argument?}
 
End of tests.
 
\end{document}
-------