4

Minimum working example:

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

\begin{document}

    \hl{\emph{irreducible}}
    
    \hl{{\emph irreducible}}

    \emph{irreducible}
\end{document}

Output: In the firs output, the first "e" is rendered as \varepsilon whereas the second "e" is rendered correctly. This is not an issue if \hl is not used.

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

1
  • 4
    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, 2021 at 2:47

3 Answers 3

4

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

2

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}

enter image description here

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

enter image description here

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

1
  • 2
    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, 2021 at 11:25
2

As a workaround, you could use the package soulpos (https://ctan.org/pkg/soulpos) which is based on soulutf8 (which is based on soul), but this works even if you don't use the utf8 encoding. But unlike soul or soulutf8, the package soulpos doesn't use repetitives small elements to "decorate" the text. As a consequence, the weird rendering of some characters is gone.

With a modified code from an example in the soulpos manual, we can recreate the \hl command (here named myhl). For comparison, I keep the original code, using the \hl command.

\documentclass{article}
\usepackage{xcolor}
\usepackage{soulpos}

% -.8ex is the depth and 11pt is the height of the rectangle around the text, 
% here for the default size of the text in the article class.
\ulposdef{\myhl}{%
  \mbox{%
    \color{yellow}%
    \rule[-.8ex]{\ulwidth}{11pt}}}

\begin{document}

    With \verb|\hl| from \verb|soul|:

    \hl{\emph{irreducible}}
    
    \hl{{\emph irreducible}}

    \emph{irreducible}
    
    \medskip  
    
    With \verb|\myhl| created with \verb|soulpos|:
    
    \myhl{\emph{irreducible}}
    
    \myhl{{\emph irreducible}}

    \emph{irreducible}
    
\end{document}

enter image description here

Notice a slightly more space after the last character in the italic text with the command created with soulpos. You can adjust this space with for example a negative value for the option xoffset-end for the created command (here, \ulposdef{\myhl}[xoffset-end=-0.5pt]{% almost suppress the blank after the last character in the line with the italic word).

You must log in to answer this question.

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