[Tex/LaTex] underline omitting the descenders

formattingtext-decorationstypography

I'm very well aware that it is considered best practice not to use underlining other than on typewriters and in handwriting. But sometimes it is needed nonetheless.

In my opinion the typographically least offending way to underline in print is to leave out the descenders. This is also how I learned to do it in handwriting.
Very few fonts offer a pre-made underlined variant, but some text-processors (e.g. OS X text-engine) do — depending on the font used — an acceptable job at emulating this effect:

OS X TextEdit underline

How would I tweak any of the existing ways/packages for underlining to do the same?

As a bonus, I'd like to be able to tweak the behaviour (extent of the omitted part, apply to which letters) per font and text style, preferably working with XeLaTeX.

Best Answer

We could do for each letter:

  • Produce the underlining
  • Overwrite the line with the same but bolder letter or with a left and right shifted one, but in background or white color
  • Write the letter

So we would get an underline with matching gaps.

Here I modified the soul approach of Marco in this way, just for a demonstration what I mean, which could be improved (boldness, loop):

\setcounter{errorcontextlines}{999}
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{soul}
\usepackage{color}
\makeatletter
\newcommand*{\whiten}[1]{\llap{\textcolor{white}{{\the\SOUL@token}}\hspace{#1pt}}}
\DeclareRobustCommand*\myul{%
    \def\SOUL@everyspace{\underline{\space}\kern\z@}%
    \def\SOUL@everytoken{%
     \setbox0=\hbox{\the\SOUL@token}%
     \ifdim\dp0>\z@
        \raisebox{\dp0}{\underline{\phantom{\the\SOUL@token}}}%
        \whiten{1}\whiten{0}%
        \whiten{-1}\whiten{-2}%
        \llap{\the\SOUL@token}%
     \else
        \underline{\the\SOUL@token}%
     \fi}%
\SOUL@}
\makeatother
\begin{document}
\sffamily\Huge
\myul{jumping quickly}
\end{document}

underlining with gaps