%        File: gapped_ul.sty
%     Created: Sat Jul 25 10:00 PM 2015 E
%          By: H.A.Trujillo
% Last Change:     28 Jul 2015


%  Underlines text, with gaps for descending letters.  If the "osf" option
%  is used, descending digits will also create gaps.
%
%  Commands:
%	\ul{Text to underline}  
%  
%  Options:
%	osf	tells gapped_ul that a font with old-style digits is used.
%	lining	force non-osf treatment of digits
%
%  Caveats:
%    - Note that this macro is not robust: it does not take well to comamand
%      sequences within the underlined text.  
%    - Ligatures similarly disappear.  
%    - Accented  and other non-ascii characters are hit-or-miss.  Best to
%      enter as single latin-1 glyph.
%    - Dashes: For en-dashes, use "{ -- } or {\relax--\relax}" rather than "--".  
%      Em-dashes can similarly be made, but "|" will also work for OT1 encoding.
%

\ProvidesPackage{gapped_ul}[2015/07/28 v1.5 Underlining with Gaps around Descenders]
  
\newif\if@osf  \@osffalse


% Options to declare that font uses old-style digits.  
%	 Add additional options to match those of fonts used.
\DeclareOption{osf}{\@osftrue}		% mathpazo, newtx, newpx 
%
%	force "non-osf" digits
\DeclareOption{lining}{\@osffalse}
%
%	cfr-lm defaults to old-style digits
\@ifpackageloaded{cfr-lm}{\@osftrue}{}

\ProcessOptions\relax


% Top level command
\newcommand{\ul} [1] {%
  \let\r@peat=\chop@up
  \def\@sp{% 	underline spaces yet retain stretchability	(1)
      \leaders\hrule height -1.2pt depth 1.6pt \hskip \the\fontdimen2\font 
				plus \the\fontdimen3\font minus \the\fontdimen4\font}%
  \chop@up #1 \@elt}


% Break text into words and call underlining routine
\def\chop@up #1 #2{%
  \if #2\@elt
    \let\r@peat \@gobble
    \let\@sp \relax%
  \fi
  \@ul{#1}%		underline words
  \@sp%			underline spaces within the \ul block
  \r@peat #2}


\newcommand{\shortbar} [1] {\rule[-1.6pt]{#1}{0.4pt}} %    #1 = length of bar

% Underline the words
\if@osf 
  % if osf is set, include digits with non-zero depth
  \def\@ul#1{%
    \@tfor\tok:=#1 \do{%
      \expandafter\in@\expandafter{\tok}{gj,359;[]()}%
      \ifin@
	\tok%						gj359, etc      full-width
      \else
	\expandafter\in@\expandafter{\tok}{py/}%	py/	thin left descender
	\ifin@
	  \tok\llap{\shortbar{0.5ex}}%
	\else\if\tok 4%					4	fat right descender
	  \leavevmode\rlap{\shortbar{0.3ex}}\tok
	\else\if\tok 7%					7	fat left descender
	  \tok\llap{\shortbar{0.4ex}}%
	\else\if\tok q%					q	thin right descender
	  \leavevmode\rlap{\shortbar{0.5ex}}\tok
	\else\if\tok Q% 0.6ex for tx,ppl; 0.8 for cmr	Q 	wide character
	  \leavevmode\rlap{\shortbar{0.8ex}}\tok
	\else 
	  \underbar{\tok}%					everything else
	\fi\fi\fi\fi\fi%
      \fi}%
  }
\else
  % osf is not set: ignore digits
  \def\@ul#1{%
    \@tfor\tok:=#1 \do{%
      \expandafter\in@\expandafter{\tok}{gj,;[]()}%	gj	full char-width
      \ifin@
	\tok%
      \else
	\expandafter\in@\expandafter{\tok}{py/}%	py/	thin left descender
	\ifin@
	  \tok\llap{\shortbar{0.5ex}}%
	\else\if\tok q%					q	thin right descender
	  \leavevmode\rlap{\shortbar{0.5ex}}\tok
	\else\if\tok Q% 0.6ex for tx,ppl; 0.8 for cmr	Q 	wide character
	  \leavevmode\rlap{\shortbar{0.8ex}}\tok
	\else 
	  \underbar{\tok}%					everything else
	\fi\fi\fi%
      \fi}%
  }
\fi

%  (1)  Use of \leader to provide strechability inspired by Stephen
% 	Gildea's underline.tex.

%							  vim: sw=2
