[Tex/LaTex] Changing the color of special underlines with ulem

colormacrosulem

I'm using the ulem package to produce different kinds of underlining, and want each underline style to be in a different color (so that the difference is visible whether you're viewing the document in black and white or not). The package documentation gives an example for how to define a colored version of the basic \uline command, which works just fine:

\newcommand\reduline{\bgroup\markoverwith{\textcolor{red}{\rule[-0.5ex]{2pt}{0.4pt}}}\ULon}

What I'm having trouble with is creating new commands to give the other types of underlining colors, namely \dashuline, \dotuline, and \uwave, i.e. make one of them blue, one green, etc. The definition of these commands in ulem.sty is more complex than I understand, and I'm not sure how to plug them into the above format. Other underlining packages would be theoretically fine, but I do need a variety of underline shapes, which aren't predefined in soul for example, as far as I can tell.

Best Answer

This is actually a lot easier than you may think. As the documentation suggests, just look in ulem.sty and copy and modify the definitions (starting on line 211). Just insert a color changing command in the argument to \markoverwith in the definition you want to modify and that's it.

\documentclass{article}

\usepackage{ulem}
\usepackage{xcolor}

\makeatletter
  \newcommand\reduline{\bgroup\markoverwith{\textcolor{red}{\rule[-0.5ex]{2pt}{0.4pt}}}\ULon}
  \UL@protected\def\blueuwave{\leavevmode \bgroup 
    \ifdim \ULdepth=\maxdimen \ULdepth 3.5\p@
    \else \advance\ULdepth2\p@ 
    \fi \markoverwith{\lower\ULdepth\hbox{\textcolor{blue}{\sixly \char58}}}\ULon}
  \UL@protected\def\yellowdotuline{\leavevmode \bgroup 
    \UL@setULdepth
    \ifx\UL@on\UL@onin \advance\ULdepth2\p@\fi
    \markoverwith{\begingroup
       %\advance\ULdepth0.08ex 
       \lower\ULdepth\hbox{\kern.06em \textcolor{yellow}{.}\kern.04em}%
       \endgroup}%
    \ULon}
  \UL@protected\def\greendashuline{\leavevmode \bgroup 
    \UL@setULdepth
    \ifx\UL@on\UL@onin \advance\ULdepth2\p@\fi
    \markoverwith{\kern.13em
    \vtop{\color{green}\kern\ULdepth \hrule width .3em}%
    \kern.13em}\ULon}
\makeatother

\begin{document}

\reduline{I am underlined in red.}

\blueuwave{I am underlined with blue waves.}

\yellowdotuline{I am underlined with yellow dots.}

\greendashuline{I am underlined with green dashes.}

I am not underlined at all.

\end{document}

MWE output

Related Question