使用 ulem 更改特殊下划线的颜色

使用 ulem 更改特殊下划线的颜色

我正在使用该ulem包来生成不同类型的下划线,并希望每种下划线样式都有不同的颜色(这样无论您是否以黑白方式查看文档,都可以看到差异)。包文档提供了一个如何定义基本命令的彩色版本的示例\uline,该命令运行良好:

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

我遇到的问题是创建新命令来赋予其他类型的下划线颜色,即\dashuline\dotuline\uwave,即使其中一个为蓝色,一个为绿色等。这些命令的定义比我理解的要复杂,我不确定如何将它们插入上述格式。其他下划线包理论上没问题,但我确实需要各种下划线形状,据我所知,ulem.sty这些形状在例如中没有预定义。soul

答案1

这实际上比您想象的要容易得多。正如文档所建议的,只需查看并复制和修改定义(从第 211 行开始)。只需在要修改的定义中ulem.sty的参数中插入一个颜色更改命令即可。\markoverwith

\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 输出

答案2

只需简单包装即可方便地支持任何预定义的颜色:

\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}

结果

答案3

略有改善mkpoli 的回答通过改变命令可以避免一些错误\colorluline

\documentclass{article}

\usepackage{ulem}
\usepackage{xcolor}

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

\makeatletter
% -------Changes made here-----------
\newcommand{\coloruline}[2]{%
        \UL@protected\def\temp@uline{\leavevmode \bgroup
            \markoverwith{\textcolor{#1}{\rule[-0.5ex]{2pt}{0.4pt}}}%
        \ULon}%
        \temp@uline{#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}

相关内容