Using soul and xcolor to highlight \emph renders some letters incorrectly

Minimum working example:

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

\begin{document}

\hl{\emph{irreducible}}

\hl{{\emph irreducible}}

\emph{irreducible}
\end{document}


Output:

Could you please let me know why the "e" is rendered like a \varepsilon?

• This is due to how highlight is implemented by soul. Essentially, when you say \hl{...}, it draws a bunch of thin vertical colored lines to create the illusion of backgroud highlight. Sometimes it happens to block part of some letter(s). In your case, that is not an \epsilon, but rather just an italic e but with its right half blocked (you can see the same blocking with the first r, not as severe as the e of course). Jul 28 '21 at 2:47

There is another package with highlighting capabilities: lua-ul. Applying it to your MWE would look like so:

\documentclass{article}
\usepackage{xcolor}
\usepackage{luacolor}
\usepackage{lua-ul}

\begin{document}

\highLight{\emph{irreducible}}

\highLight{{\emph irreducible}}

\emph{irreducible}
\end{document}


Downside for some would be that it requires luaTeX (or luaLaTeX) for compilation and package luacolor for coloring (has to be loaded after xcolor, or instead of it of course).

lua-ul has also option for compatibility with soul (as a drop-in replacement), but I have never tested that. Personally, I would redefine or search & replace all soul macros and use lua-ul.

As a side note, there are other approaches, like using tikz to do highlighting, but I would consider that very advanced process; and it requires at least 3 compilations.

You can find that code in this site, but you will have to (possibly) adapt it to changed hook interface in new LaTeX3 kernel and maybe also some pdf primitives (\savepos).

In some cases, you can overcome by interrupting the normal kerning:

\hl{\emph{irre\mbox{}ducible}}


by sticking an \mbox{} in the middle of a word. But even that will not work on, for example, ending a penultimate word on an italic f.

As a real hacky workaround that could get you out of a bind, you could try this if the problem arises just once or twice in the span of a longer passage. Note that the argument to \althl will not linebreak, so it is only to be used for the word that needs help, not the whole passage.

\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}
\fboxsep=.5pt
\newcommand\althl[1]{\colorbox{yellow}{\rule
[-.75\dp\strutbox]{0pt}{.81\baselineskip}\itshape #1}}
\begin{document}
\hl{\emph{This is an irre\mbox{}ducible test }}%
\althl{of }\hl{\emph{things.}}
\end{document}


Compare this to, for example, that which naturally arises just from using \hl on the passage:

\hl{\emph{This is an irreducible test of things.}}


In this case, the first r and the e in irreducible are clipped as well as the f in of.

• As I see in althl command implementation \colorbox, I guess this wont work with linebreaks. If that is true, it might be good to note that in your answer. Jul 28 '21 at 11:25