3

I want to highlight text, but some text inside it need to be italic. The problem is, \hl{... \textit{...}...} cause all space inside \texit{...} removed. Using \ can solve the problem, but that is tiring, ugly, the resulted text still has some white color. Another solution is to use \mbox{...}, but for long text, it will not be moved to next line (the .cls file forbid hypen for change lines). Using ~ also removed and useless. Don't suggest to use \textit{...} for each words individually, that will be a nightmare. I just want \textit{...} behaves exactly the same as if it was outside \hl{}.

\documentclass[12pt]{report}
\usepackage{ragged2e}

\usepackage{soul}
\usepackage{xcolor}
\sethlcolor{yellow}

\begin{document}

\hl{This is an \textit{example of sentence} that I want to highlight. This is an \textit{example of sentence} that I want to highlight. This is an \textit{example of sentence} that I want to highlight.}

\hl{This is an \emph{example of sentence} that I want to highlight. This is an \emph{example of sentence} that I want to highlight. This is an \emph{example of sentence} that I want to highlight.}

\hl{This is an \textit{\mbox{example of sentence}} that I want to highlight. This is an \textit{\mbox{example of sentence}} that I want to highlight. This is an \textit{\mbox{example of sentence}} that I want to highlight.}

\hl{This is an \textit{example\ of\ sentence} that I want to highlight. This is an \textit{example\ of\ sentence} that I want to highlight. This is an \textit{example\ of\ sentence} that I want to highlight.}

\hl{This is an \textit{example~of~sentence} that I want to highlight. This is an \textit{example~of~sentence} that I want to highlight. This is an \textit{example~of~sentence} that I want to highlight.}

\end{document}

NOTE:

Many said that \ cause error and didn't compile on LaTeX. In using above piece of code, it is compiled but resulted in black box. Maybe that is my engine. BUT, funnily, it is compiled perfectly under custom cls created by old colleagues in my campus. I'm no expert on LaTeX, and will try to deleting line by line of use packages to find which packages delete the whitespace inside textit. Thanks for all the kindness.

NOTE2: Found out, it was ragged2e. Now, if possible, I don't want to delete this packages because I don't know what will happen. I also update the MWE to include this new package.

4
  • 1
    your example doesn't demonstrate the problem, it actually errors. If I remove the \ it compile but the spaces are there. Jan 20 at 10:16
  • 1
    @UlrikeFischer thanks for the notice. After further investigation, the same text is compiled differently in MWE and my actual documents. There must be a package that messing with textit maybe? I updated the MWE. Now, the space is works, but there is some white leftover. Also I still can't find who remove the white space. Jan 20 at 10:25
  • your new example still errors. Why don't you test your code before you ask such questions? Apart from this: if you want stable highlighting, use lualatex and lua-ul. With soul only simple text works. Jan 20 at 10:56
  • @UlrikeFischer I don't know your engine, but it is compiled both using pdlatex.exe (I'm using VSCode) and overleaf. Jan 22 at 2:04

3 Answers 3

5

Don't use soul, but soulpos package (which is based internally upon soulutf8, which is based upon soul, but soulpos uses other method for the highlight).

I use also \emph rather than \textit, because the former is more semantic (we use italic shape for emphasize).

If you look carefully, soul cuts some italic characters, and adds a little blanc after the italic text.

\documentclass{article}

\usepackage{xcolor}
\usepackage{soulpos}
%\sethlcolor{yellow} % by default, \hl highligh's color is yellow so I comment this line ; furthermore, \sethlcolor isn't available when only "soulpos" is called, but "\hl" is available, as this code shows (it's used in line 14 of this code)

\ulposdef{\myhl}{%
  \mbox{%
    \color{yellow}%
    \rule[-.8ex]{\ulwidth}{11pt}}}

\begin{document}

\hl{This is an \textit{example of sentence} that I want to highlight.} % "x" and "f" cut in the italic text, and white after "ce" 

\myhl{This is an \emph{example of sentence} that I want to highlight.} % all right
\end{document}

enter image description here

Here you see the characters cuts with \hl, but not with \myhl (customised command with soulpos).

enter image description here

And here, you see the little blank created by \hl but not with \myhl.

enter image description here

3
  • Your example works under \usepackage{ragged2e}! Thanks. Jan 22 at 2:56
  • Is it save to renew hl commands, to avoid changing all the \hl in the files? If yes, how to achieve that? Jan 22 at 2:58
  • 1
    @MuhammadYasirroni Use \renewcommand\hl{\myhl} just after \begin{document} (it doest't work in the preamble, in the preamble it creates the error Command \hl already defined.). But, the downside of using fixed commands like \myhl (or keeping \hl with \renewcommand) is that the highlight height is fixed (11pt in my code), and this is not ideal when the size of characters changes.
    – quark67
    Jan 22 at 12:32
3

With LuaLaTeX, you can use the lua-ul as an alternative to the soul package with much less difficulty.

\documentclass{article}
%\usepackage{soul}
\usepackage[soul]{lua-ul}\usepackage{luacolor}
%\sethlcolor{yellow}

\begin{document}

\hl{This is an \textit{example\ of\ sentence} that I want to highlight.}
%\hl{This is an \textit{\mbox{example of sentence}} that I want to highlight.}
\hl{This is an \textit{example of sentence} that I want to highlight.}
\end{document}
1
  • 1
    That is good, but I prefer if it is compileable via pdflatex. Jan 20 at 10:17
3

Hope you can use \emph instead of \textit

\hl{This is an \emph{example of sentence} that I want to highlight.}

\hl{This is an \emph{\mbox{example of sentence}} that I want to highlight.}

\hl{This is an \emph{example~of~sentence} that I want to highlight.}

Inside \hl tag, \ was not applicable.

1

You must log in to answer this question.

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