underline omitting the descenders

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


Simply because I found it useful here is a modification of Stefans answer above that lets you control the depth, thickness and width of the underline. This might be useful, for example, with a larger or bolder font. The syntax is: \varul<width>[depth][thickness]{stuff} where:

  • <width> is optional and given as a multiple of 1/100 em, e.g. <10> would give a gap width of 0.1em. The default value is 5
  • [depth] is optional and is given as a positive value (including units) that specifies the distance below the baseline to draw the underline. The default value is 0.2ex.
  • [thickness] is optional and is given as a positive value (including units) specifying the thickness of the underline. The default value is 0.1ex
  • The fourth, mandatory, argument is what you want underlined.

This will work across paragraphs and linebreaks but may change the spacing very slightly.

enter image description here

\documentclass{article}
\usepackage{soul}
\usepackage{xcolor}
\usepackage{xparse}
\makeatletter

\ExplSyntaxOn
\cs_new:Npn \white_text:n #1
  {
    \fp_set:Nn \l_tmpa_fp {#1 * .01}
    \llap{\textcolor{white}{\the\SOUL@syllable}\hspace{\fp_to_decimal:N \l_tmpa_fp em}}
    \llap{\textcolor{white}{\the\SOUL@syllable}\hspace{-\fp_to_decimal:N \l_tmpa_fp em}}
  }
\NewDocumentCommand{\whiten}{ m }
    {
      \int_step_function:nnnN {1}{1}{#1} \white_text:n
    }
\ExplSyntaxOff

\NewDocumentCommand{ \varul }{ D<>{5} O{0.2ex} O{0.1ex} +m } {%
\begingroup
\setul{#2}{#3}%
\def\SOUL@uleverysyllable{%
   \setbox0=\hbox{\the\SOUL@syllable}%
   \ifdim\dp0>\z@
      \SOUL@ulunderline{\phantom{\the\SOUL@syllable}}%
      \whiten{#1}%
      \llap{%
        \the\SOUL@syllable
        \SOUL@setkern\SOUL@charkern
      }%
   \else
       \SOUL@ulunderline{%
         \the\SOUL@syllable
         \SOUL@setkern\SOUL@charkern
       }%
   \fi}%
    \ul{#4}%
\endgroup
}

\makeatother
\begin{document}

\varul{\Huge jumping quickly}\par\smallskip
\varul[1pt][1pt]{\Huge jumping quickly}

\end{document}

Edit by Tobi: \fp_mul:Nn doesn’t work in a current TeXlive, so I replaced the two lines with just \fp_set:Nn \l_tmpa_fp {#1 * .01}.


Here is a solution using contour and ulem packages.

IMO this produces a very good looking underline, specialy for title underlining:

\documentclass{article}

\usepackage[outline]{contour}

\usepackage{ulem}
\normalem % use classical emph

\newcommand \myul[4]{%
  \begingroup%
  \renewcommand \ULdepth {#1}%
  \renewcommand \ULthickness {#2}%
  \contourlength{#3}%
  \uline{\phantom{#4}}\llap{\contour{white}{#4}}%
  \endgroup%
}

\newcommand \myulline[4]{%
  \begingroup%
  \renewcommand \ULdepth {#1}%
  \renewcommand \ULthickness {#2}%
  \contourlength{#3}%
  \noindent\rlap{\uline{\hspace{\textwidth}}}\contour{white}{#4}%
  \endgroup%
}

\begin{document}
\bfseries

\Huge
\myulline{5pt}{1pt}{2pt}{Chapter Eight}

\vspace{1cm}

\normalsize
\normalfont

Test. \myul{2pt}{0.5pt}{1pt}{Quit jumping quickly!} Test.

Test. \myul{1.2pt}{0.5pt}{0.5pt}{Quit jumping quickly!} Test.

\end{document}

Output:

example output

Close up:

example detail

As drawback, the underline depth cannot be very close to baseline because it can be hidden by the contour.