[Tex/LaTex] soul: broken highlighting with xcolor when using \selectcolormodel

colorhighlightingsoul

I try to highlight text using the soul package.
But I experience problems if I use it in combinations with the xcolor
command selectcolormodel, only spaces are highlighted as shown in the MWE:

\documentclass{standalone}
\usepackage{xcolor}
\selectcolormodel{rgb}

\usepackage{soul}

\begin{document}

Please, highlight \hl{this small text}.

\end{document}

Output: soul output

Best Answer

The source of the problem is that both xcolor and soul use \dimen@ as a temporary register. And soul uses it at a place where it can't be sure that other commands temper around with this dimen. You can redefine the internal soul command, or you can use \convertcolorsUfalse so that xcolor no longer recalculate color at usage:

\documentclass{article}
\usepackage{xcolor}
\selectcolormodel{rgb}
%\convertcolorsUfalse
\usepackage{soul}


\makeatletter
\newdimen\SOUL@dimen %new
\def\SOUL@ulunderline#1{{%
    \setbox\z@\hbox{#1}%
    %\dimen@=\wd\z@
    \SOUL@dimen=\wd\z@ %new
    \dimen@i=\SOUL@uloverlap
    \advance\SOUL@dimen2\dimen@i %\dimen@ exchanged too
    \rlap{%
        \null
        \kern-\dimen@i
        %\SOUL@ulcolor{\SOUL@ulleaders\hskip\dimen@}%
        \SOUL@ulcolor{\SOUL@ulleaders\hskip\SOUL@dimen}% new
    }%
    \unhcopy\z@
}}

\makeatother
\begin{document}

please, highlight \hl{this small text}.

\end{document}

If you don't want to copy the definition from soul.sty, here's a way to patch the command:

\usepackage{soul}

\usepackage{etoolbox}
\makeatletter
\patchcmd{\SOUL@ulunderline}{\dimen@}{\SOUL@dimen}{}{}
\patchcmd{\SOUL@ulunderline}{\dimen@}{\SOUL@dimen}{}{}
\patchcmd{\SOUL@ulunderline}{\dimen@}{\SOUL@dimen}{}{}
\newdimen\SOUL@dimen
\makeatother

This will work even if the soul package is updated (because the patch won't succeed).