Inconsistent ulem (with textbf and textit)

Package ulem uses \leaders for the repeated underline elements. For example, \leaders is used in the table of contents for the dotted lines. Then the dots are vertically aligned on the page.

The package file ulem.sty says in a comment:

% That concludes the basic underlining. To put various other objects
% (characters) under (or over) text we need to define \markoverwith
% to set the overlay material in a box, and use leaders of that box for
% overlaying the text. Here, the meaning of \UL@pixel is changed so
% that `pixel' size = box size. Note that we generally need \leaders
% (not \cleaders) for text, because an underline will be a patchwork
% of small \leaders, and the characters must stay in registration.
% However, we "hook" the leaders command so specific applications can
% reassign it (\let\ULleaders\xleaders or \let\ULleaders\cleaders).

Comparison of the three variants:

  1. \leaders: box units are vertically aligned on the page.
  2. \cleaders: the box units are centered, remaining space is put at the left and right side of the space to be filled.
  3. \xleaders: The box units are stretched to fill the space. That means, the distance between the lines can vary.

Test file:

\documentclass{article}
\usepackage{ulem}

%\let\ULleaders=\leaders % default
%\let\ULleaders=\cleaders
%\let\ULleaders=\xleaders

\begin{document}
  \texttt{\meaning\ULleaders}\par
  \textbf{foo} \dashuline{bar}\par
  \textit{foo} \dashuline{bar}\par
  f \dashuline{bar}\par
  fo \dashuline{bar}\par
  foo \dashuline{bar}\par
  foof \dashuline{bar}\par
  foofo \dashuline{bar}\par
  foofo \dashuline{bari}\par
\end{document}

<code>\leaders</code>     <code>\cleaders</code>     <code>\xleaders</code>


Using TikZ (see How do I write \underline text but with a dotted line) solves the problem and also allows for fancy customizations (but prohibits linebreaks within an underlined word):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing} % only for squigglies

\newcommand{\dashuline}[1]{%
  \tikz[baseline=(todotted.base)]{
    \node[inner sep=1pt,outer sep=0pt,draw=none] (todotted) {#1};
    \draw[densely dashed] (todotted.south west) -- (todotted.south east);
  }%
}%

\newcommand{\squigglies}[1]{%
  \tikz[baseline=(squiggled.base)]{
    \node[inner sep=1.25pt,outer sep=0pt,draw=none] (squiggled) {#1};
    \draw[decorate,decoration={snake,amplitude=0.75pt,segment length=1ex}] (squiggled.south west) -- (squiggled.south east);
  }%
}%

\begin{document}
  \textbf{foo} \dashuline{bar} \squigglies{baz}\par
  \textit{foo} \dashuline{bar} \squigglies{baz}\par
  f \dashuline{bar} \squigglies{baz}\par
  fo \dashuline{bar} \squigglies{baz}\par
  foo \dashuline{bar} \squigglies{baz}\par
  foof \dashuline{bar} \squigglies{baz}\par
  foofo \dashuline{bar} \squigglies{baz}\par
\end{document}

typeset output of accompanying example

Tags:

Ulem