3

There is a strange interaction between the underscore and ulem packages. With \uline{\itshape bbb\_lll}, "bbb" is italic but "lll" is upright. However, with \underline instead of \uline, or when the underscore package is not loaded, "lll" is italic, as expected.

I really want to load both the underscore and uline packages:

  • underscore to be able to use _ instead of \_;
  • ulem to have breakable underlined text (or text highlighted with \markoverwith).

Is there any systematic fix to the problem?

9
  • Use {\itshape \uline{bbb_lll}}. In my opinion don't use underlines anyway.
    – percusse
    Commented Sep 25, 2013 at 12:33
  • My example was a minimal one. In fact, I use ulem not to underline but to highlight text with a colored background (as for inline code fragments on TeX.SE). The \itshape must be inside the \uline (or equivalent highlighting command) because it does not apply to the whole argument, but only to part of it. Commented Sep 25, 2013 at 12:47
  • @percusse underlines are sometimes useful precisely because they shouldn't be used in finished text - they're a really useful way of drawing attention to things like changes to be discussed with co-authors.
    – Chris H
    Commented Sep 25, 2013 at 12:50
  • @ChrisH I can't relate to that. I don't know any cases where underlining didn't cause any difficulty in terms of readability. Especially with lots of characters with descenders. It's worse if it runs multi lines. But it's a matter of taste
    – percusse
    Commented Sep 25, 2013 at 13:05
  • @percusse I'm not talking about several lines, generally, but about a few words. How would you draw attention to changed text in a pdf (assuming it's going to be printed B+W, and not assuming the reader will use Adobe), especially if bold text is in use as well as italics. (It's a serious question, I'm currently giving quite a lot of though to collaborative editing, where I'm the only LaTeX user)
    – Chris H
    Commented Sep 25, 2013 at 13:08

2 Answers 2

1

As I mentioned in my comment, if you are ultimately interested in highlighting text, the page Rounded box around placeholder text that supports line breaking could be of use. Along those lines, I checked to see whether my adaptation of the censor package to this problem was compatible with the underscore package. It turns out that censoring across multiple lines (\marktext on the cited page) does not work well with the underscores.

However, a plain \colorbox accomplishes a highlight, while honoring the italics following the underscores. Of course, the downside is that a box can not break across lines Here's the MWE:

\documentclass{article}
\usepackage{ulem}
\usepackage{underscore}
\usepackage{xcolor}\makeatletter
\def\mystrut{\rule[-.2\baselineskip]{0pt}{.9\baselineskip}}
\fboxsep=.5pt
\parskip 1em
\begin{document}
ULINE: \uline{\itshape bbb\_lllg}
\par
COLORBOX: \colorbox{cyan}{\mystrut\itshape bbb\_lllg}
\end{document}

enter image description here

0

You can try that. But then don't load package underscore, and you still must use \_ if not in the argument of \uline.

\documentclass{article}
\usepackage{ulem}

\catcode`\_\active
\DeclareRobustCommand*{\uline}{\relax \ifmmode \expandafter \underline
  \else \bgroup \catcode`\_ \active\let_\_\expandafter \ULset \fi} 
\catcode`\_ 8

\begin{document}

\uline{\itshape bbb_lll}

\uline{bbb_lll}

\uline{\bfseries\itshape bbb_lll}

\end{document}

ulemunderscore

As you need more than \uline you may try rather:

\documentclass{article}
\usepackage{color}
\catcode`\_\active
\def\ActiveUnderscore {\bgroup\catcode`\_\active\let_\_\let\next= }
\catcode`\_ 8


\begin{document}

\ActiveUnderscore{\colorbox{green}{\itshape bbb_lll}}

\end{document}

activeunderscore

3
  • my second proposal of course is not for material in math mode, but then if you want to highlight stuff in math mode I guess that the _ doesn't cause a problem anyhow.
    – user4686
    Commented Sep 25, 2013 at 15:01
  • @Steven and jfbu: Interesting answers, but they do not solve the problem, as, because of the \colorbox, the text can not be broken. I have had an explanation from Donald Arseneau, the author of the two packages (I should have read the doc more carefully). The problem is with ulem and would also occur with a space or a discretionary instead of _ or \_. ulem boxes any text between possible break points, so local font changes do not leak out of these boxes. Commented Sep 27, 2013 at 10:50
  • I forgot to mention that underscore inserts a \discretionary after _ and \_... To avoid the problem, one must either put the text in an (unbreakable!) \mbox, or repeat the font change at every possible break point. Commented Sep 27, 2013 at 11:20

You must log in to answer this question.

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