From: yhartojo@enuxha.eas.asu.edu (Francis Hartojo)
Subject: ULEM.STY for underlining text in TeX/LaTeX.
Organization: Somewhere in the middle of nowhere
Date: Thu, 1 Oct 1992 03:18:40 GMT

     Upon request from Mr. Douglas West (west@symcom.math.uiuc.edu), here is
the ulem.sty file that contains the macro for underlining text in TeX/LaTeX.
It works on both TeX and LaTeX.  For further information, read the
documentation provided with the codes.
=====>  CUT HERE  <=====
%
%  U L E M . S T Y
%
%  A style file to provide underlining that can stretch and be broken between
%  lines.  It replaces italics with underlining in \em-phasized text.
%  It is most suitable for simple text such as {\em ibid.} or {\em \LaTeX:
%  A Document Preparation System\/} that may need to be underlined in a
%  manuscript submitted for publication.  Only to be used when emphasis
%  is delimited by braces.
%
%  So you want to do some underlining but still want \em to give italics?
%  Just enter the declaration \normalem before beginning your document
%  and specify your underlining as \uline{some text} (alias \ULine also).
%  \uline also works in plain TeX, just \input ulem.sty.  If you use this 
%  method of underlining a lot, you might want to abbreviate the definition, 
%  changing \ULine or \uline to \ul.
%
%  Methods for wavy-underlines, striking out, or marking over text are 
%  also shown as comments. In summary:
%    \uline{important} %  underlined text
%    \uwave{boat}      %  wavy underline 
%    \sout{wrong}      %  line drawn through word
%    \xout{removed with hash marks}% marked over with //////.
%  If underlining for emphasis is switched on (default) then
%    {\em emphasized\/}  is underlined; 
%  declare \normalem to turn this off, \ULforem to turn it back on.
%  To replace boldface (\bf) with wavey underlines, put \UWforbf in the 
%  preamble. 
%
%  More instructions after \endinput.
%
%  Copyright (c) 1989,1992 by Donald Arseneau
%
%  These macros may be freely transmitted, reproduced, or modified for
%  non-commercial purposes provided that this notice is left intact.

%  First, set catcode of @ in case it isn't a "letter" already
\expandafter\edef\csname UL@catcode\endcsname
  {\catcode`\noexpand\@=\the\catcode`\@
  \let \csname UL@catcode\endcsname \noexpand\UnDefinedButNoAt}
\catcode`\@=11

% Allow plain TeX to use this style:
\ifx\@ifundefined\@underfined 
  \def\@height{height}
  \def\@depth{depth}
  \def\@width{width}
  \def\@empty{}
  \def\@gobble#1{}
  \let\pem\it
  \let\pbf\bf
% Do non-outer \newif and balance \if's and \fi's when skipping
  \csname newif\endcsname\if@ignore   \ifx\fi\relax\fi
\fi

%  For underlining normally

\def\ULem{\let\pem\LA@em
   \expandafter\UL@set\expandafter{\ifnum0=`}\fi}%% -> \UL@set{
\def\ULine{\bgroup\UL@set}
\let\uline\ULine

\let\LA@em\pem
\def\ULforem{\let\pem\ULem}
\def\normalem{\let\pem\LA@em}
\ULforem  %  default is to use underlining for \em, 

\newbox\UL@hyphenbox
\newbox\UL@box
\newcount\UL@spfactor
\newcount\UL@penalty
\newskip\UL@skip
\newdimen\UL@lht
\newdimen\UL@ldp
%
%  Could do this...
%  \UL@lht=-.8pt \UL@ldp=1.2pt  
%  ...see note a few lines down and definition of \sout

\let\LA@space\ \let\LA@hskip\hskip 

\def\UL@end*{\relax\relax}% something harmless unlikely to be found elsewhere

%   Set the depth of the underline.  For a different depth, \UL@set should 
%   be changed; or eliminated so \UL@ldp and \UL@lht can be defined just once 
%   as style parameters. For non-underlining applications, \UL@set should 
%   be bypassed and some other macro should make any necessary settings.
%
\def\UL@set{\setbox\UL@box\hbox{(}\UL@ldp\dp\UL@box 
    \UL@lht-\UL@ldp\advance\UL@lht.4\p@\UL@on}

\def\UL@on#1{\leavevmode\everymath{\UL@hrest}\let\-\UL@dischyp
    \let\ \UL@space \let\hskip\UL@hskip \let\\\UL@cr
    \let\newline\UL@newline \let\@lnbk\UL@lnbk \let\@nolnbk\UL@nolnbk 
    \def\hfil{\hskip\z@ plus1fil\relax}\def\hfill{\hskip\z@ plus1fill\relax}%
    \setbox\UL@hyphenbox\hbox{\setbox\UL@box\hbox{\char\hyphenchar\font
      }\UL@putbox}\UL@word#1\global\UL@spfactor\spacefactor{} \UL@end* }

\let\UL@unegroup\@empty

\def\UL@word#1 {\UL@start.#1 \ifx\UL@end#1\egroup % must expand to nothing ...
                              % outside the ifs for syntactical spaces to work
      \unkern \unskip \unkern % remove extra leader at end
      \spacefactor\UL@spfactor \let\UL@word\egroup
   \else % not finished
      \ifmmode\else \ifdim\lastskip=\z@\else % this should allow syntactical
          \global\UL@skip\lastskip \unskip   %    ...spaces
          \UL@stop \UL@leaders
      \fi\fi
   \fi \UL@word}

\def\UL@start.{\setbox\UL@box\hbox\bgroup\everyhbox{\UL@hrest}%
%   the following are to cope with stops (\ ,\- etc) within extra braces
    \let\UL@start\@gobble \let\UL@putbox\@empty \let\UL@unegroup\bgroup
    \kern-3sp\kern3sp % kerns so I can test for beginning of list
    \if@ignore \global\@ignorefalse \expandafter\ignorespaces \fi} 

\def\UL@stop{\global\UL@penalty\lastpenalty
   \ifdim\lastkern=3sp \egroup % Nothing in hbox,
      \ifdim\wd\UL@box=\z@ % make doubly sure, and don't put on list
      \else \UL@putbox \fi
   \else
      \egroup \UL@putbox
   \fi\UL@unegroup}

\def\UL@putbox{{\UL@skip\wd\UL@box \advance\UL@skip\UL@pixel
   \vrule \@height\UL@lht \@depth\UL@ldp \@width\UL@skip
   \kern-\UL@skip}\box\UL@box 
   \ifnum\UL@penalty=\z@ \else \penalty\UL@penalty \fi}

%  With interword leaders, give some overlap to avoid gaps caused by
%  round-off errors in the printing program.  Needs \unkern \unskip \unkern
%  above.  This version assumes 300 dots per inch, but it still looks good
%  with higher resolution, and will still work down to ~150 dpi.  Change the
%  value of \UL@pixel if necessary.

\newdimen\UL@pixel \UL@pixel=0.0033333truein

\def\UL@leaders{{\kern-\UL@pixel \advance \UL@skip \tw@\UL@pixel
   \leaders\hrule \@height\UL@lht \@depth\UL@ldp \LA@hskip\UL@skip
   \kern-\UL@pixel}}

% restore some things for inside math or \mbox
\def\UL@hrest{\let\ \LA@space \let\-\@empty}

\def\UL@space{\LA@space \global\UL@skip\lastskip \unskip \UL@reskip}

\def\UL@dischyp{\penalty\z@ % zero penalty => no break (see \UL@stop)
   \UL@stop \discretionary{\copy\UL@hyphenbox}{}{}\UL@start.}

% The test \ifx\ \LA@space \else means we are neither in math mode nor an 
% \mbox, so it is safe to stop the current \UL@box.  \  and \- are common 
% enough that they are disabled directly (by \UL@hrest); \\, \newline, \hskip,
% \linebreak, and \nolinebreak are rare enough that the test is incorporated
% in their UL versions. This adds processing when they're used, but saves
% processing in \UL@hrest called by \everymath and \everyhbox.

\def\UL@hskip{\ifx\ \LA@space \else 
   \afterassignment\UL@reskip \global\UL@skip \fi}

\def\UL@reskip{\UL@stop \UL@leaders \UL@start.}

\def\UL@cr{\unskip \ifx\ \LA@space \else  \UL@stop \fi
   \unkern\unskip\unkern \@normalcr} 

\def\UL@newline{\ifx\UL@start\@gobble % (\UL@cr may have \UL@stop-ed already)
   \unskip \ifx\ \LA@space \else \UL@stop \fi \fi 
   \unkern\unskip\unkern \LA@hskip \z@ plus1fil\penalty -\@M \UL@start.}

\def\UL@lnbk[#1]{\ifx\ \LA@space \else 
   \unskip \UL@stop \unkern\unskip\unkern \penalty -\@getpen{#1}\UL@start.\fi}

\def\UL@nolnbk[#1]{\ifx\ \LA@space \else 
   \global\UL@skip\lastskip \unskip \UL@stop 
   \ifdim \lastkern=-\UL@pixel \unkern \global\UL@skip\lastskip \unskip\unkern
   \fi \penalty \@getpen{#1}\ifdim\UL@skip=\z@\else\UL@leaders\@ignoretrue\fi 
   \UL@start.\fi}

%--------begin optional features-------
% Remove the (first) % from each of the following lines to enable wavey
% underlines, crossing out, or other added features.
%
% %  For drawing a wavey underline instead of a straight one (under-wave)
% %  use tilde (\sim) as an underline character:
%
% \def\uwave{\bgroup\UW@set}
% \def\UWbf{\let\pbf\LA@bf
%    \expandafter\UW@set\expandafter{\ifnum0=`}\fi}%% -> \UW@set{
% \let\LA@bf\pbf
% \def\UWforbf{\let\pbf\UWbf}
% \def\normalbf{\let\pbf\LA@bf}
%
% %  To draw a line through text instead of under it (strike out)
% %  do `under' line with line height set higher:
%
% \def\sout{\bgroup \UL@ldp-.5ex \UL@lht-\UL@ldp\advance\UL@lht.4\p@\UL@on}
%
% %  To mark //// over text instead of underlining (x-out)
% \def\xout{\bgroup \markoverwith{/}\UL@on}
%
% \def\UW@set{\markoverwith{\lower1ex\hbox{\the\scriptfont2 \char"18}}\UL@on}
%
% \def\UW@leaders{\kern-\UL@pixel\UW@leadtwo}
% 
% \def\UW@leadtwo{{\advance\UL@skip\tw@\UL@pixel
%    \leaders\copy\UW@box\LA@hskip\UL@skip}\kern-\UL@pixel}
%
% \def\UW@putbox{\vrule\@width-\UL@pixel \penalty\@M 
%    {\UL@skip\wd\UL@box \UW@leadtwo}\kern-\wd\UL@box \box\UL@box 
%    \ifnum\UL@penalty=\z@ \else \penalty\UL@penalty \fi}
%
% \newbox\UW@box 
%
% \def\markoverwith#1{\let\UL@leaders\UW@leaders \let\UL@putbox\UW@putbox
%   \setbox\UW@box\hbox{\kern-.1em#1\kern-.1em}%
%   \UL@pixel.5\wd\UW@box} % character size acts like a pixel size
%-------end optional features--------

\UL@catcode % restore catcode of @ to starting value

\endinput
%
%  ULEM changes the behavior of \em to underline emphasized text; and it
%  also enables underlining with the syntax \ULine{some text}.  Although
%  ULEM is far from a perfect replacement for \em, it does allow line
%  breaks, and even primitive hyphenation, within the underlined text.
%  It is most suitable for simple text that may need to be underlined in
%  a manuscript submitted for publication.  It is *only* to be used when
%  emphasis is delimited by explicit braces.  Every word is typeset in 
%  an underlined box, so normally it cannot be hyphenated, but explicit 
%  discretionary hyphens (\-) can be used to allow hyphenation.  The 
%  underlines continue between words, and stretch just like ordinary 
%  spaces do.  Since spaces delimit words, there may be some difficulty 
%  with syntactical spaces (e.g. "2.3 pt"). Some effort is made to handle 
%  such spaces, but if that fails, you might solve the problem by enclosing 
%  the offending command in braces...
%
%  One important incompatability is with grouping: EVERYTHING IN BRACES
%  IS TREATED LIKE AN MBOX.  Thus, braces will suppress stretching and
%  linebreaks in the text they enclose.  Moreover, the specially-taken-
%  care-of commands \-, \\, \ , and \hspace (\hskip) are often ignored if they 
%  appear inside extra braces.  They succeed only if the enclosing braces are
%  preceded and followed by spaces, but then they have another undesireable
%  effect: they end the group and start a new one!  Thus, the only braces
%  you should use are those delimiting parameters to commands, or those
%  with simple text inside.  Syntactical spaces inside braces never cause
%  a problem, nor do spaces in math mode.
%
%  Text produced by expansion of a command word is boxed too, but the
%  commands \\, \  and \- still work; just as for braces surrounded by space:
%      \newcommand\iff{if and only if} {\em \iff}
%  does not allow any stretching or linebreaking between words, but
%      \newcommand\iff{if\ and\ only\ if} {\em \iff}
%  allows stretching and linebreaking.
%
%  Nested \em commands produce underlined italics, but heed the warnings
%  about braces above.  Since nested \em-phasis normally switches back to
%  roman type, this is another incompatability.  To get italics without
%  underlining, use \it.
%
%  HERE IS A SIMPLE EXAMPLE.
% 
%  \noindent 'Twas {\em brillig\/} and the {\em slithy~toves}
%  did {\em gyre\/} and {\em gim\-ble\/} in the {\em wabe,\\[2pt] }
%  All {\em mim\-sey\/} were the {\em boro\-goves\/} and
%  the {\em mome raths outgrabe}.
%
%  HERE IS A DIFFICULT EXAMPLE.
%
%  \documentstyle[12pt,ulem]{article}  % or [ulem]
%  \setlength\textwidth{3.3in}
%  \begin{document}
%  No, I did {\em not} act in the movie {\em {\em The} % <<<< Nested
%  {\em Persecu\-\em tion}  {\em and} {\em  Assassination} {\em of}
%  {\em Jean-Paul} {\em Marat}, as per\-formed by the Inmates
%  of the Asylum of Charenton under the Direc\-tion of the
%  Marquis de~Sade!} But I {\em did} see it.
%  \end{document}
%
%  In the nested emphasis, \em is given for each word so that the
%  interword spaces can be outside the braces.  Otherwise, there would be
%  no stretch to the line.  Note that the discretionary hyphen (\-) in
%  Persecution cancelled the inner \em, thus \-\em was needed, but the
%  hyphen in Direction was just fine because it was not in nested braces.
%  Whew!  This example illustrates that ULEM does not handle nested
%  emphasis well!  Nevertheless, it works great for simple things. 
%
%  Underlining can also be done according to \uline{this text}.  To use 
%  this type of underlining, but have \em still produce italics, put 
%  \normalem in the preamble of the document.
%  
%  Other variations are possible, including drawing a wavey underline
%  (\uwave{under-wave}) instead of a straight one, drawing a line through
%  text rather than under it (\sout{strike out}), crossing out text with
%  /////// (\xout{cross out, X out}), plus whatever variations you can
%  think of. These variations are commented-out in the standard 
%  distribution, so remove the % from the appropriate lines to enable
%  these features.  To get wavy underlines for {\bf  }, un-comment
%  the definitions for \uwave, and put \UWforbf at the beginning of
%  your document.
%
%  Send problem reports to asnd@triumfcl.bitnet or asnd@Jack.TRIUMF.CA
%
%  Version 1992: make " {} " give two spaces, as it should; support \newline
%      and \\, \nolinebreak and \linebreak; add definitions necessary for 
%      plain TeX use.  Support various types of `underline'.
%
%  Test transmission:
%  brackets: round, square, curly, angle:   () [] {} <>
%  backslash, slash, vertical, hat, tilde:   \ / | ^ ~
--
+----------------------------------+---------------------------------------+
| Francis Hartojo                  | Bitnet:   AUYXH@ASUACVAX.BITNET       |
| Comp. Science & Engr. Department | Internet: YHARTOJO@ENVMSA.EAS.ASU.EDU |
| Arizona State University         | Phone:    (602) 921-1342              |