# Inconsistent ulem (with textbf and textit)

Using the latest (2012-05-18) ulem package gives inconsistent underlining after font changes respectively depending on the position of the underlined text:

\documentclass{article}
\usepackage{ulem}

\begin{document}
\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
\end{document}


with different a) counts of dashes and b) horizontal placement relative to the underlined word. Is there a way to get consistent (\dashuline{bar} looking exactly the same in all above positions) underlining?

• It seems that \dashuline uses a fixed grid. – karlkoeller Feb 8 '14 at 9:25

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}

\begin{document}
\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}


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}
`

• Perhaps you could add a small example to show this working in the context of this question. Otherwise what you have written so far is more a comment than an answer. – Andrew Swann Feb 8 '14 at 16:15
• Does the latest edit qualify? – CaringDev Feb 8 '14 at 18:24
• Yen indeed, it is now much more useful to future visitors. – Andrew Swann Feb 8 '14 at 18:31