TransWikia.com

Changing the color of special underlines with ulem

TeX - LaTeX Asked on December 3, 2020

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:

newcommandreduline{bgroupmarkoverwith{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 Answers

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
  newcommandreduline{bgroupmarkoverwith{textcolor{red}{rule[-0.5ex]{2pt}{0.4pt}}}ULon}
  UL@protecteddefblueuwave{leavevmode bgroup 
    ifdim ULdepth=maxdimen ULdepth 3.5p@
    else advanceULdepth2p@ 
    fi markoverwith{lowerULdepthhbox{textcolor{blue}{sixly char58}}}ULon}
  UL@protecteddefyellowdotuline{leavevmode bgroup 
    UL@setULdepth
    ifxUL@onUL@onin advanceULdepth2p@fi
    markoverwith{begingroup
       %advanceULdepth0.08ex 
       lowerULdepthhbox{kern.06em textcolor{yellow}{.}kern.04em}%
       endgroup}%
    ULon}
  UL@protecteddefgreendashuline{leavevmode bgroup 
    UL@setULdepth
    ifxUL@onUL@onin advanceULdepth2p@fi
    markoverwith{kern.13em
    vtop{color{green}kernULdepth 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

Correct answer by schtandard on December 3, 2020

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]{%
        newcommandtemp@reduline{bgroupmarkoverwith
            {textcolor{#1}{rule[-0.5ex]{2pt}{0.4pt}}}ULon}%
        temp@reduline{#2}%
    }

    newcommand{coloruuline}[2]{%
        UL@protecteddeftemp@uuline{leavevmode bgroup
            UL@setULdepth
            ifxUL@onUL@onin advanceULdepth2.8p@fi
            markoverwith{textcolor{#1}{lowerULdepthhbox
                {kern-.03emvbox{hrule width.2emkern1p@hrule}kern-.03em}}}%
        ULon}%
        temp@uuline{#2}%
    }

    newcommand{coloruwave}[2]{%
        UL@protecteddeftemp@uwave{leavevmode bgroup 
        ifdim ULdepth=maxdimen ULdepth 3.5p@
        else advanceULdepth2p@ 
        fi markoverwith{textcolor{#1}{lowerULdepthhbox{sixly char58}}}ULon}
        fontsixly=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

Answered by mkpoli on December 3, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP