答案1
这里我使用\ooalign
获取规则,并使用 tokencycle 来实现换行(但是,连字符丢失了,sloppypar
在某些情况下可能需要补救,例如 MWE)。
\discretionary
已编辑以在 中使用\Spacedirective
,以避免在换行时悬挂空格。
重新编辑以添加一些胶水来协助论证。
\documentclass[10pt]{article}
\usepackage{xcolor,tokcycle}
\newlength\replength
\newcommand\ruleht{-1.5pt}% ELEVATION OF RULE
\newcommand\rulewidth{4pt}% THICKNESS OF RULE
\def\rulecolor{purple!30}% COLOR OF RULE
\newcommand\sphl[1]{\sbox0{#1}\ooalign{\makebox[0pt][l]{%
\smash{\color{\rulecolor}\rule[\ruleht]{\wd0}{\rulewidth}}}\cr#1}}
\newcommand\myul[2]{%
\resettokcycle
\Characterdirective{\addcytoks{\nobreak\hspace{0pt minus.6pt}%
\sphl{##1}}}%
\Spacedirective{\addcytoks{\nobreak\hspace{0pt minus .6pt}%
\discretionary{}{}{\sphl{\ }}}}%
\cytoks{}%
\def\rulecolor{#1}%
\tokcyclexpress{#2}%
\the\cytoks%
}
\begin{document}
\begin{sloppypar}
Now I will try my special underlining \myul{purple!25}{%
This is a test, we will
see if word wrapping occurs automatically or not
This is a test, we will
see if word wrapping occurs automatically or not
This is a test, we will
see if word wrapping occurs automatically or not
This is a test, we will
see if word wrapping occurs automatically or not
This is a test, we will
see if word wrapping occurs automatically or not
This is a test, we will
see if word wrapping occurs automatically or not} back to normal text.
\end{sloppypar}
\noindent\hrulefill
\end{document}
答案2
tcolorbox
如果您以后寻求扩展,可以说是最强大的选择之一。
\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\usetikzlibrary{calc}
\tcbset{twilight/.style={enhanced,hbox,colback=white,
underlay={\begin{tcbclipinterior}
\fill[#1] let \p1=($(interior.north)-(interior.south)$) in
([yshift={0.5*\y1}]interior.south west) rectangle
(interior.south east);
\end{tcbclipinterior}}},twilight/.default=purple!50,
tight/.style={on
line,boxsep=0pt,left=#1,right=#1,top=#1,bottom=#1,arc=0pt,boxrule=0pt,colframe=white},
tight/.default=0pt}
\begin{document}
One wishes to highlight
\begin{tcolorbox}[tight,twilight]
this expression
\end{tcolorbox} in a sentence.
\end{document}