3

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.

2

2 Answers 2

4

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

2
  • 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?
    – Josh M
    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.
    – schtandard
    Mar 2, 2020 at 23:38
3

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
            \ifx\UL@on\UL@onin \advance\ULdepth2.8\p@\fi
            \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@
        \else \advance\ULdepth2\p@ 
        \fi \markoverwith{\textcolor{#1}{\lower\ULdepth\hbox{\sixly \char58}}}\ULon}
        \font\sixly=lasy6 % does not re-load if already loaded, so no memory drain.
        \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}

Result

1
  • 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.
    – Mika Ike
    Dec 26, 2021 at 19:27

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.