# Strikeout in different color appears behind letters, not on top of them

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?

-
Using ulem it seems like you're not concerned about line-breaking. Is this the case? –  Werner Sep 11 '12 at 5:37
I was using ulem mainly because I like its underlining facilities (because its \uline doesn't increase the line spacing, which is what I usually want). Of course if anyone has other pointers to things that work and are compatible with many packages, I'll be glad to know. (That is, I never really thought about line-breaking. I am always glad about further insights.) –  Lover of Structure Sep 11 '12 at 7:39
@werner why do you say that? -- ulem was the first underline package (iirc) that offered breakable underlines (etc.). it's listed in the uk faq as a solution in a question "underlined text won't break"; there has never been a complaint about that answer, to the faq maintainers... –  wasteofspace Sep 11 '12 at 9:36
@wasteofspace: You're right. The construction in the MWE led me to believe otherwise. The nested use of \textcolor and \sout removes the line-breaking capability. –  Werner Sep 11 '12 at 14:01

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.

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

-
Thank you. Does the example work with equally well with color instead of xcolor, or is the latter package strictly speaking necessary in some non-obvious way? –  Lover of Structure Sep 12 '12 at 7:52
Oh, do you think you could add code so that I can also vary the color of the text? –  Lover of Structure Sep 12 '12 at 8:19
It should work equally well with either color or xcolor. I added a second optional argument for the color of the text. –  Scott H. Sep 12 '12 at 14:56
Buried in the ulem scanner (\ULon) is a matching \egroup that is inserted after the argument is read and dealt with. Something like \newcommand\mystuff[1]{do stuff\egroup} which could then be used like {\bgroup\mystuff{argument stuff}} –  Scott H. Sep 12 '12 at 20:04