17

The \underline macro built into LaTeX is pretty much not useable per Why does underlined text not get wrapped once it hits the end of a line ? I have been attempting to use the soul package to underline omitting the descenders but have run into numerous issues, and now considering alternatives.

Hence, am looking for a comparison of the various options for underlining text:

  1. the soul package
  2. the ulem package
  3. Tikz as per this answer on "Cool Text Highlighting in LaTeX".
  4. Any others?

Requirements:

Ideally I would like to be able to underline content

  1. across line breaks
  2. with hyphenation
  3. with math content

Working across page breaks would be a bonus but not something I am too concerned about.

Even if some packages can not meet all the requirements it would be good to have them listed so that one is aware of the potential issues of using it.

Notes:

  • I am aware that generally underlining is frowned upon typography circles. But, I don't have a good alternative for indicating hyperlinks. Using color to indicate a hyperlink has an issue when the link text has more than one word (or two subsequent links) in which case it is not clear exactly what is being linked. More details are in the notes section in Should Hyperlinks include trailing puctuation as part of the link text?. Futhermore I prefer to reserve colors for other purposes.

References:

  • For underlining hyperlinks, you might also want to consider hyperref's pdfborderstyle={/S/U/W 1} (see here for example). Beware that some viewers will still show a box. – Robert Aug 1 '13 at 1:06
  • 2
    Another way one could indicate hyperlinks are icons or small arrows that point “somewhere outside of the document”. – Qrrbrbirlbel Aug 1 '13 at 1:24
  • @Robert: That still requires the PDF viewer to interpret it and then display the link as it sees it sees fit. Hence am resorting to underling it. – Peter Grill Aug 1 '13 at 1:43
  • 1
    Ok, I understand what your problem is here but is it really a problem? (Is it that important to know where a hyperlink starts, what information does this fact contain?) Although, you also want to underline math content, so all bets are off. ;) … You can always use two symbols. I don’t know either one of the options (soul/ulem/TikZ) but in addition to an not-underlined space between hyperlinks (that can be missed easily) you can add a small hook at the end of the line. – Qrrbrbirlbel Aug 1 '13 at 2:03
  • 3
    Elaborating an implicit point of Qrrbrbirlbel's: the real difficulty with coloring hyperlinks is that it can be difficult to tell whether two adjacent colored words represent one hyperlink or two--I've tried to use useful-looking Wikipedia cross-references that turned out to be two adjacent links rather than the one I was looking for. This difficulty does not arise if you use something like a footnote symbol. – Charles Staats Aug 1 '13 at 14:15
6

This is a hack on the censor package to do that which was not intended. All things considered, not too bad. It makes space for descenders, with a user-settable gap (\maxkern)! The depth (\censorruledepth) and thickness (\censorruleheight) of the underrule are fully settable. It does linebreaking and page breaking. And with this EDITED version, it now does math underlining.

However, it does not do hyphenation. For text, it has two forms, using the censor syntax, \blackout (which skips over spaces and periods, and \xblackout which attempts, through the use of carefully selected measures, to fill in underlines over the blanks and periods. As the parameters are set in my MWE, \xblackout has a negative side effect of spilling slightly into the left margin.

\documentclass{article}
\usepackage[none]{hyphenat}
\usepackage{lipsum}
\usepackage{censor}
\usepackage{xcolor}
\usepackage{stackengine}
\usepackage{scalerel}
\censorruledepth=-.25ex
\censorruleheight=.1ex
\newlength\maxkern
\setlength{\maxkern}{.14ex}
\newlength\nextcharwidth
\makeatletter
\renewcommand\@cenword[1]{%
  \setlength{\nextcharwidth}{\widthof{#1}}%
  \censorrule{\nextcharwidth}%
  \kern -\nextcharwidth%
  \color{white}%
  \kern -.5\maxkern #1\kern .5\maxkern%
  \kern -\nextcharwidth%
  \kern -\maxkern #1\kern \maxkern%
  \kern -\nextcharwidth%
  \kern .5\maxkern #1\kern -.5\maxkern%
  \kern -\nextcharwidth%
  \kern \maxkern #1\kern -\maxkern%
  \kern -\nextcharwidth%
  \color{black}%
  #1}
\long\def\xblackout#1{\rule{0ex}{0ex}%
  \def~{-}%
  \def\@justpar{F}%
  \def\@justperiod{F}%
  \def\@justspace{F}%
  \protected@edef\save@arg{#1}%
  \expandafter\xcensor@Block\save@arg\stringend%
  \let~\sv@tilde%
 }
\long\def\blackout#1{%
  \def~{-}%
  \protected@edef\save@arg{#1}%
  \expandafter\censor@Block\save@arg\stringend\let~\sv@tilde}
\newcommand\singlelipsum[1]{%
  \begingroup\let\lips@par\relax\csname lipsum@\@roman{#1}\endcsname
\endgroup }
\let\sv@cenword\@cenword
\newcommand\m@cenword[1]{\ThisStyle{%
  \stackengine{\mcensorruledepth}{$\SavedStyle#1$}%
    {\rule{\widthof{$\SavedStyle#1$}}{\the\censorruleheight}}{U}{c}{F}{T}{L}}}
\newcommand\mblackout[2][\dp\strutbox]{%
  \let\@cenword\m@cenword%
  \def\mcensorruledepth{#1}%
  \blackout{{#2}}%
  \let\@cenword\sv@cenword%
}
\makeatother
\periodrlap=1ex
\afterperiodlap=.4ex
\lletterlap=.0ex
\rletterlap=.6ex
\afterspacelap=.6ex
\begin{document}
\blackout{\singlelipsum{4}}

{\raggedright\xblackout{\singlelipsum{4}}}

\[ \mblackout{y = A x^2} + Bx + \mblackout{C} \]

\[ \mblackout[3ex]{y = \frac{a^3 + b^2 + c}{d_3}}\]

\blackout{\singlelipsum{4}}
\end{document}

If space is desired between words, \blackout works real nice.

enter image description here

If you want the spaces to be underlined, use \xblackout. With my selection of the parameters to tune \xblackout, the underline extends slightly into both margins. Also, I am using \raggedright here, because stretching glue can make gaps form in the underline.

enter image description here

For math mode, the approach is slightly different. First difference is that it cannot make a gap for descenders (because I don't know how to convert a length variable into mu's). So, as a result, I place, by default, the math underline (named \mblackout, keeping with the censor syntax) at the depth of the \strutbox. However, for math that drops below this depth, it takes an optional argument of how far below the baseline to do the underline.

Thus,

\[ \mblackout{y = A x^2} + Bx + \mblackout{C} \]

\[ \mblackout[3ex]{y = \frac{a^3 + b^2 + c}{d_3}}\]

gives the following:

enter image description here

If you are afraid the underline might be confused for math division, you could always thicken it up with a change in \censorruleheight.

4

Underlining math is a bit of an issue - how do you choose where to draw the line if you have subscripts for example.

I use [normalem]{ulem} to show changes text during reviewing, precisely because it would never be used in the final output. I haven't had to face this with math.

The following code

\documentclass[12pt,oneside,openany,a4paper,english]{memoir}
\usepackage{babel}
\usepackage{graphicx}
\usepackage[normalem]{ulem}
\usepackage{hyperref}
\hypersetup{colorlinks=false,pdfborderstyle={/S/U/W 1}}

\begin{document}
\tiny{
    \url{http://tex.stackexchange.com/questions/126291/list-of-underlining-packages-pros-and-cons}
    \href{http://tex.stackexchange.com/questions/126291/list-of-underlining-packages-pros-and-cons}{\TeX.stackexchange.com}
    \uline{\TeX.stackexchange.com}
}

\end{document}

produces screengrab of above code (note I had to use printscreen to get this, Acrobat's snapshot removes the blue underlines on the urls)

ulem will try to underline math, subscripts work OK but fractions don't - with the preamble of above, \uline{test $y=mx+c$ and $y_n=y_{n-1}+1^2+\frac{1}{2}$} produces ulem math example

It can handle forced line breaks with \\ but not double carriage returns.

\href will also try to handle math, and can underline simple fraction OK by setting the underline a little lower, but superscripts in inline math clash with the underline of the line above.

In conclusion I would use hyperref for underlining hyperlinks (probably with blue rather than cyan) mainly because of its other benefits (links that work), and ulem for drawing attention to areas of text (on the rare occasions when an underline is useful).

  • But although ulem's \uline can handle math mode, it's unhappy if the first character of its argument is a $ - hence the 'test' at the beginning of my example above. – Chris H Aug 1 '13 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.