5

Using no-break spaces seems to break interaction between xcolor and ulem:

\documentclass{article}

\usepackage{xcolor}
\usepackage[normalem]{ulem}

\begin{document}
\textcolor{green}{\uline{\textcolor{blue}{TeX~StackExchange}}}
\end{document}

What's going on? I tried wrapping the last line in \traceon…\traceoff, but even just that line produced about 900 lines of output, and I don't know what to make of them. The problem exists both in XeLaTeX and pdfLaTeX.

"StackExchange" is improperly colored

6

As the documentation says (emphasizing by me):

Several text-formatting commands are specially supported within the underlining: \-, \ , ~, \\, \newline, \linebreak, \nolinebreak, \penalty, \hskip, \hspace, \hfil, \hfill, \hss. Displayed math is not supported.

The special commands do have a problem: they end a group so any local assignments are lost.

If you want a colored text you should start the color outside \uline. You can get a colored rule with the commands described in the documentation too:

\documentclass{article}

\usepackage{xcolor}
\usepackage[normalem]{ulem}

\newcommand\coloruline{\bgroup\markoverwith
{\textcolor{ulinecolor}{\rule[-0.5ex]{2pt}{0.4pt}}}\ULon}
\colorlet{ulinecolor}{green}

\begin{document}
\textcolor{blue}{\coloruline{TeX StackExchange}}
\end{document}

enter image description here

| improve this answer | |
  • Thanks; very nice. However, this new underline doesn't behave exactly the same; in particular, the width and alignment are slightly different. This is visible in particular on monospace fonts: \makebox[0pt][l]{\textcolor{blue}{\texttt{\coloruline{v}}}}\textcolor{blue}{\texttt{\uline{v}}} – Clément Aug 12 '16 at 1:19
  • (the difference is particularly marked with thin characters, such as !; I tried adjusting the length of the \rule, but the "right" length for ! is too short for, say, W, and vice versa) – Clément Aug 12 '16 at 1:25

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.