[Tex/LaTex] highlighting strikeout text with ulem shifts the line downwards

highlightingstrikeoutulem

I'm trying to highlight a text with a strikeout word by defining a new command as explained in the docs of ulem. Unfortunately, that moves down the strikeout line a few points, which looks strange compared to non-highlighted text. How to avoid/fix this?

Here is an 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}

enter image description here

Best Answer

You could let \hl redefine the \sout marco (the space in the name is important).

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

enter image description here


EDIT: thanks to muzimuzhiZ for letting me know that the above became outdated.

If you're using a newer version of ulem starting from 2019-11-28, the \sout macro isn't changed by the above, since it is now defined as \protected\def\sout{...} instead. You can use the following then:

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