18

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

2
  • Thanks, I had this problem with just standard \usepackage{xcolor}. The top answer fixed it. May 30, 2016 at 20:59
  • It seems to me that the issue no longer exists as of today's TeX Live. Can you confirm?
    – AlMa0
    Apr 9 at 18:55

2 Answers 2

21

Edit

The problem has been resolved. With soul 2023-02-18 v3.0 the fix is no longer needed.

Old 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).

11
  • Shouldn't \SOUL@dimen go also after \advance?
    – egreg
    Mar 18, 2012 at 16:40
  • Great solution! thx :)
    – ejoerns
    Mar 18, 2012 at 16:47
  • @egreg Yes I have corrected it (I had read \dimen@2 as a name instead of \dimen@ + 2\dimen@i.) Mar 18, 2012 at 16:50
  • 2
    This should probably go to Melchior Franz, the soul maintainer.
    – Daniel
    Mar 19, 2012 at 19:13
  • 2
    Since I'm still experiencing the same issue 4 years later I sense this hasn't been fixed in the package. But this fix worked. May 30, 2016 at 21:00
14

My solution is just to avoid soul package. soul package is very fragile and may be problematic in many situations. I would use more robust ulem package to define a new \hl command:

\documentclass{article}
\usepackage{xcolor}
\usepackage[normalem]{ulem} % use normalem to protect \emph
\newcommand\hl{\bgroup\markoverwith
  {\textcolor{yellow}{\rule[-.5ex]{2pt}{2.5ex}}}\ULon}

\begin{document}
This is a test \hl{for highlighting} text.
\end{document}

enter image description here

2
  • Interesting alternative! But I was wondering why some of my highlighted texts were underlined too. Found the solution in the ulem code. With \normalem it looks as I expected. ;)
    – ejoerns
    Mar 19, 2012 at 18:06
  • 1
    This is really not a good solution. Please see tex.stackexchange.com/questions/254333
    – user81070
    Jul 8, 2015 at 14:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .