8

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}

leads to

enter image description here

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?

1
  • 1
    It seems that \dashuline uses a fixed grid. Commented Feb 8, 2014 at 9:25

3 Answers 3

11

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>

0
3

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

3
  • 1
    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. Commented Feb 8, 2014 at 16:15
  • Does the latest edit qualify?
    – CaringDev
    Commented Feb 8, 2014 at 18:24
  • Yen indeed, it is now much more useful to future visitors. Commented Feb 8, 2014 at 18:31
0

And the reason that \leaders are used for \dashuline and \uwave is that each word and each space is underlined separately, and only \leaders keeps the parts synchronized, to ensure all the dashes are equally spaced and all the squiggles join properly.

One could make a variant version that uses only one segment, prohibiting line breaks and flexible spacing, that would work well with \cleaders and short arguments (where no line breaking is expected).

Try...

\let\origDUL\dashuline
\protected\def\dashuline#1{\leavevmode\begingroup
  \settowidth{\dimen0}{#1}\ifdim\dimen0>3em 
  \origDUL{#1}\else
  \let\ULleaders\cleaders\origDUL{\mbox{#1}}\fi
  \endgroup}

Test...

\let\ULleaders=\leaders % default
\texttt{\meaning\ULleaders}\par
f \dashuline{bar} and \dashuline{The box units are stretched to fill the space.}\par
fo \dashuline{bar} and \dashuline{The box units are stretched to fill the space.}\par
foo \dashuline{bar} and \dashuline{The box units are stretched to fill the space.}\par
foof \dashuline{bar} and \dashuline{The box units are stretched to fill the space.}\par
foofo \dashuline{bar} and \dashuline{The box units are stretched to fill the space.}\par
foofo \dashuline{bar} and \dashuline{The box units are stretched to fill the space.}\par

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .