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?

Michel Fioc
  • 1,257
  • 9
  • 14
  • Use {\itshape \uline{bbb_lll}}. In my opinion don't use underlines anyway. – percusse Sep 25 '13 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. – Michel Fioc Sep 25 '13 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 Sep 25 '13 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 Sep 25 '13 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 Sep 25 '13 at 13:08
  • Given your use for highlighting, does this Q/A help: http://tex.stackexchange.com/questions/125162/rounded-box-around-placeholder-text-that-supports-line-breaking/ ? – Steven B. Segletes Sep 25 '13 at 13:10
  • @ChrisH No problem at all. I have a few font selection switches for that purpose, {\myemphfont }, {\mycorrfont ...} etc. The names are shorter of course but I either change the font to something completely different so there is no way it can be ignored or use changes package which is fortunately received positively by my collaborators, an example: http://tex.stackexchange.com/questions/65453/track-changes-in-latex/65466#65466 – percusse Sep 25 '13 at 13:12
  • @percusse the font options sound like a good idea - I'll have to try that. Not sure about changes (though +1 for your nice writeup) but I might give it a go. Thanks – Chris H Sep 25 '13 at 13:16
  • @percusse I need it for code fragments and italic is not appropriate for that: it is overloaded with so many meanings; I already use it within the highlighted text for placeholders; it does not delimit well, so you never know whether surrounding dots or commas are inside or outside; in particular, single letters (think of "o") and symbols do not stand out clearly enough. The same is true with most font changes, except maybe bold, but it is already used for titles, so how do you highlight code within them? Another possibility is to use non standard delimiters like corners or guillemets. – Michel Fioc Sep 25 '13 at 13:54

2 Answers2

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

  • 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. –  Sep 25 '13 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. – Michel Fioc Sep 27 '13 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. – Michel Fioc Sep 27 '13 at 11:20