我正在尝试通过定义一个新命令来突出显示带有删除线的文本,如 的文档中所述ulem
。不幸的是,这会将删除线向下移动几个点,与未突出显示的文本相比看起来很奇怪。如何避免/修复此问题?
以下是 MWE:
\documentclass{article}
\usepackage{xcolor}
\usepackage{ulem}
\newcommand\hl{\bgroup\markoverwith{\textcolor{yellow}{\rule[-.5ex]{.1pt}{2.5ex}}}\ULon}
\begin{document}
Lorem Ipsum is simply dummy text of the printing \sout{and typesetting} industry.
\hl{Lorem Ipsum is simply dummy text of the printing \sout{and typesetting} industry.}
\end{document}
答案1
您可以\hl
重新定义\sout
宏(名称中的空格很重要)。
\documentclass{article}
\usepackage{xcolor}
\usepackage{ulem}
\newcommand\hl{%
\bgroup
\expandafter\def\csname sout\space\endcsname{\bgroup \ULdepth =-.8ex \ULset}%
\markoverwith{\textcolor{yellow}{\rule[-.5ex]{.1pt}{2.5ex}}}%
\ULon}
\begin{document}
Lorem Ipsum is simply dummy text of the printing \sout{and typesetting} industry.
\hl{Lorem Ipsum is simply dummy text of the printing \sout{and typesetting} industry.}
Lorem Ipsum is simply dummy text of the printing \sout{and typesetting} industry.
\end{document}
编辑:谢谢木子木志让我知道上述内容已经过时了。
ulem
如果您从 2019-11-28 开始使用较新版本,\sout
则上述宏不会更改,因为它现在被定义为\protected\def\sout{...}
。然后您可以使用以下命令:
\documentclass{article}
\usepackage{xcolor}
\usepackage{ulem}
\makeatletter
\newcommand\hl{%
\bgroup
\UL@protected\def\sout{\bgroup \ULdepth =-.8ex \ULset}%
\markoverwith{\textcolor{yellow}{\rule[-.5ex]{.1pt}{2.5ex}}}%
\ULon}
\makeatother
\begin{document}
Lorem Ipsum is simply dummy text of the printing \sout{and typesetting} industry.
\hl{Lorem Ipsum is simply dummy text of the printing \sout{and typesetting} industry.}
Lorem Ipsum is simply dummy text of the printing \sout{and typesetting} industry.
\end{document}
答案2
这是一个有趣的观察,下面是一个基于 Ti 的过度回答钾Z。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{tikzpagenodes}
\usepackage{ulem}
\newcommand\HighLight{\bgroup\markoverwith{\textcolor{yellow}{\rule[-.5ex]{.1pt}{2.5ex}}}\ULon}
\tikzset{StrikeOut/.style={thick,-}}
\makeatletter
\newcommand{\gettikzxy}[3]{% from https://tex.stackexchange.com/a/58590/121799
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\global\edef#2{\the\pgf@x}%
\global\edef#3{\the\pgf@y}%
}
\makeatother
\newcommand{\StrikeOut}[2][2pt]{%
\tikz[remember picture,overlay, baseline=(Begin.base)]{%
\node[anchor=base,inner sep=0pt,outer sep=0pt] (Begin) {\strut};}#2%
\tikz[remember picture,overlay, baseline=(End.base)]{%
\node[anchor=base,inner sep=0pt,outer sep=0pt] (End) {\strut};}%
\begin{tikzpicture}[overlay,remember picture]
\gettikzxy{($(Begin.north)-(current page.south west)$)}{\BeginxN}{\BeginyN}
\gettikzxy{($(End.north)-(current page.south west)$)}{\EndxN}{\EndyN}%\typeout{\BeginyN\space\EndyN}
\pgfmathtruncatemacro{\mytest}{\EndyN-\BeginyN}
\ifnum\mytest=0\relax% begin and and in the same line %\typeout{begin and end in the same line}
% \draw[thick,-] ($(Begin.north)-(#1,0)$) -- ($(Begin.south)-(#1,0)$)
% -- ($(End.south)+(#1,0)$) -- ($(End.north)+(#1,0)$) -- cycle;
\draw[StrikeOut] (Begin) -- (End);
\else% \typeout{end below begin}
\path (current page text area.north west) -- (current page text area.south west)
node(WestLine)[left]{};
\path (current page text area.north east) -- (current page text area.south east)
node(EastLine)[right]{};
\gettikzxy{($(End.north)-(current page.south west)$)}{\EndxN}{\EndyN}
\gettikzxy{($(Begin.south)-(current page.south west)$)}{\BeginxS}{\BeginyS}
\pgfmathtruncatemacro{\mytest}{\BeginyS-\EndyN+1pt}% \typeout{\mytest}
\ifnum\mytest<2\relax% \typeout{end in the next line after begin}%
\pgfmathtruncatemacro{\mytest}{\BeginxS-\EndxN}% \typeout{\mytest}
\ifnum\mytest>0\relax
% \draw[thick,-] (Begin.north -| EastLine) -- ($(Begin.north)-(#1,0)$) --
% ($(Begin.south)-(#1,0)$) -- (Begin.south -| EastLine);
\draw[StrikeOut] (Begin) -- (Begin -| EastLine);
% \draw[thick,-] (End.south -| WestLine) -- ($(End.south)+(#1,0)$) --
% ($(End.north)+(#1,0)$) -- (End.north -| WestLine);
\draw[StrikeOut] (End -| WestLine) -- (End);
\else
% \draw[thick,-] ($(Begin.north)-(#1,0)$) -- ($(Begin.south)-(#1,0)$) --
% (Begin.south -| WestLine) -- (End.south -| WestLine)
% -- ($(End.south)+(#1,0)$) -- ($(End.north)+(#1,0)$) -- (End.north -| EastLine)
% -- (Begin.north -| EastLine) -- cycle;
\draw[StrikeOut] (Begin) -- (Begin -| EastLine);
\draw[StrikeOut] (End -| WestLine) -- (End);
\fi
\else
\pgfmathsetmacro{\LineHeight}{\the\baselineskip}
\pgfmathtruncatemacro{\NumLines}{-1+(\BeginyN-\EndyN)/\LineHeight}
\typeout{\NumLines}
\foreach \X in {1,...,\NumLines}
{
\draw[StrikeOut] ($(Begin -| WestLine)+(0,-\X*\LineHeight pt)$) --
($(Begin -|EastLine)+(0,-\X*\LineHeight pt)$);
}
\draw[StrikeOut] (Begin) -- (Begin -| EastLine);
\draw[StrikeOut] (End -| WestLine) -- (End);
% \draw[thick,-] ($(Begin.north)-(#1,0)$) -- ($(Begin.south)-(#1,0)$) --
% (Begin.south -| WestLine) -- (End.south -| WestLine)
% -- ($(End.south)+(#1,0)$) -- ($(End.north)+(#1,0)$) -- (End.north -| EastLine)
% -- (Begin.north -| EastLine) -- cycle;
\fi
\fi
\end{tikzpicture}}
\begin{document}
Lorem Ipsum is simply dummy text of the printing \StrikeOut{and typesetting} industry.
\HighLight{Lorem Ipsum is simply dummy text of the printing \StrikeOut{and typesetting} industry.}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. \StrikeOut{Sed a leo ut
erat luctus scelerisque. Suspendisse auctor mauris sit amet fringilla
hendrerit.} Maecenas eu tortor eu diam fringilla scelerisque sit amet eget enim.
Nam vitae bibendum erat, sit amet scelerisque felis. \StrikeOut{Suspendisse placerat
vitae velit pharetra lobortis et marmottae.} Donec ut erat a erat porta aliquet. Nulla eget
augue sem. Proin orci neque, eleifend id eleifend id, venenatis sit amet diam.
\StrikeOut{Mauris lacus est, cursus eget sapien at, pharetra lobortis ante. Proin
tempus ipsum tellus, ut ultricies magna congue nec. Vestibulum ante ipsum primis
in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse potenti.
In posuere dapibus urna, vitae euismod magna iaculis at.} Pellentesque pretium,
lorem ut vehicula egestas, velit dolor porta nunc, id interdum ipsum purus vel
dolor.
\end{document}
这种解决方法有各种各样的缺点,例如无法处理分页符,并且当参数中的行高发生变化时不起作用\StrikeOut
。代码基于这个帖子。我在这里发布它只是因为我想说服自己,这在原则上是可以做到的,同时又不会破坏连字符。