[Tex/LaTex] Strikeout in different color appears behind letters, not on top of them

colorstrikeoutulem

When using package ulem's strikeout facilities (macro \sout) and also color in my text, the strikethrough appears behind the letter string:

\documentclass{article} % I also often work with the memoir package (in case this matters for the range of possible fixes)
\usepackage[normalem]{ulem} % option normalem not needed for example but often loaded by me (mentioned for the unlikely case it matters for the fix)
\usepackage{color}

\begin{document}
Hello \textcolor{red}{\sout{\textcolor{black}{Welt }}}World!
\end{document}

(If I specify a different color combination, like green or magenta for the text, the effect persists – I just didn't want the compiled example to look ugly or unrealistic.)

How can I have strikethrough in a color different from the text appear on top of the text?

Best Answer

Here is a solution using the SOUL package. Linebreaks etc. are respected although spacing may change very slightly if prior experience holds true. I tried with ulem but likely because of my unfamiliarity of the package, I couldn't get a solution that would work for more than a single word. I'm not sure if the SOUL package is any less compatible than ulem when it comes to conflicts etc.

I believe that both packages first draw the line, and the place the text which is why the line is in the background. There may be other ways around that problem, but my "soul"ution was to typeset zero-width text (using \rlap), and then strikeout a phantom version of the text on top of it. The command takes two optional arguments: the first sets the color of the strike (default red) and the second sets the color of the text. enter image description here

\documentclass{article}
\usepackage{xparse}
\usepackage{soul}
\usepackage{xcolor}

\makeatletter
\NewDocumentCommand{\sotwo}{O{red}O{black}+m}
    {%
        \begingroup
        \setulcolor{#1}%
        \setul{-.5ex}{.4pt}%
        \def\SOUL@uleverysyllable{%
            \rlap{%
                \color{#2}\the\SOUL@syllable
                \SOUL@setkern\SOUL@charkern}%
            \SOUL@ulunderline{%
                \phantom{\the\SOUL@syllable}}%
        }%
        \ul{#3}%
        \endgroup
    }
\makeatother
\begin{document}
Hello \sotwo{Welt} World!

\sotwo[green]{Here is a long sentence that will span across a number of lines to force a linebreak}

\sotwo[green][blue]{Here is a long sentence that will span across a number of lines to force a linebreak}

Here is a long sentence that will span across a number of lines to force a linebreak

\end{document}
Related Question