# Changing the color of special underlines with ulem

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.

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@
\fi \markoverwith{\lower\ULdepth\hbox{\textcolor{blue}{\sixly \char58}}}\ULon}
\UL@protected\def\yellowdotuline{\leavevmode \bgroup
\UL@setULdepth
\markoverwith{\begingroup
\lower\ULdepth\hbox{\kern.06em \textcolor{yellow}{.}\kern.04em}%
\endgroup}%
\ULon}
\UL@protected\def\greendashuline{\leavevmode \bgroup
\UL@setULdepth
\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}


• Thank you! The reason this wasn't transparent to me I think is just that it's unclear where the correct place to put the \textcolor{} call is in each of the definitions. In the ulem documentation's example, it's the first thing in the \markoverwith{} call, but in your definitions, I don't see a consistent location for it. Can you explain how you determined where the call goes in each different definition? Mar 2, 2020 at 17:20
• @JoshM Well, the argument to \markoverwith always consists of some spacing commands (adding horizontal space (like \kern) or shifting something vertically (like \lower)) together with one thing inserting the actual line (or dot). I just looked for that line insertion and put that into \textcolor. In the case of \dashuline putting the \hrule into \textcolor doesn't work, so I used \color instead (I could also have put the entire \vtop into \textcolor). I can see how these definitions can seem intimidating and incomprehensible, though. Don't worry about that for now. Mar 2, 2020 at 23:38

Just an simple wrap for convenience to support any predefined color:

\documentclass{article}

\usepackage{ulem}
\usepackage{xcolor}

\definecolor{customdeepgreen}{HTML}{006A71}

\makeatletter
\newcommand{\coloruline}[2]{%
\newcommand\temp@reduline{\bgroup\markoverwith
{\textcolor{#1}{\rule[-0.5ex]{2pt}{0.4pt}}}\ULon}%
\temp@reduline{#2}%
}

\newcommand{\coloruuline}[2]{%
\UL@protected\def\temp@uuline{\leavevmode \bgroup
\UL@setULdepth
\markoverwith{\textcolor{#1}{\lower\ULdepth\hbox
{\kern-.03em\vbox{\hrule width.2em\kern1\p@\hrule}\kern-.03em}}}%
\ULon}%
\temp@uuline{#2}%
}

\newcommand{\coloruwave}[2]{%
\UL@protected\def\temp@uwave{\leavevmode \bgroup
\ifdim \ULdepth=\maxdimen \ULdepth 3.5\p@
\fi \markoverwith{\textcolor{#1}{\lower\ULdepth\hbox{\sixly \char58}}}\ULon}
\temp@uwave{#2}%
}
\makeatother

\begin{document}

\coloruline{red}{I am underlined in red.}

\coloruwave{blue}{I am underlined in blue waves.}

\coloruuline{customdeepgreen}{I am double underlined in custom deep green.}

I am not underlined at all.

\end{document}


• I obtain 2errors: ! Undefined control sequence. \coloruwave #1#2->\UL@protected \def \temp@uwave {\leavevmode \bgroup \ifdim... l.39 ...wave{blue}{I am underlined in blue waves.} and ! Undefined control sequence. \coloruuline #1#2->\UL@protected \def \temp@uuline {\leavevmode \bgroup \UL@... l.41 ...m double underlined in custom deep green.} Errors: 2 Warnings: 0 Bad Boxes: 0 BUT.. after pulse twice ENTER I obtain the PDF. Other people No errors. Dec 26, 2021 at 19:27