# Interaction between xcolor, ulem, and no-break spaces

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.

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}


• 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