# Alternative to the soul package?

## Context

One may want to highlight some text, or strike it. This can be done with either the soul package, or with the ulem package using a trick, although it does not work that well.

## Problem

The problem I have with soul is that it does not behave well on glossaries, new commands, or citations. Basically, you just cannot take your whole text and surround it with a \hl{} as it have been discussed elsewhere. And workaround with \mbox{\gls{toto}} or even just {\gls{toto}} do the trick.

For instance, see the following minimal working example:

\documentclass{article}

\usepackage{soul}
\usepackage[acronym]{glossaries}
\usepackage{color}

\newacronym{co2}{CO$_2$}{carbon dioxide}
\newcommand{\etal}{\textit{et al.}}

\begin{document}

Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.
Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.

\begin{thebibliography}{9}
\bibitem{latexcompanion}
Michel Goossens, Frank Mittelbach, and Alexander Samarin.
\textit{The \LaTeX\ Companion}.
\end{thebibliography}

\end{document}


which compiles fine while:

    [...]
\begin{document}

\hl{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.
Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}

[...]


gives a bunch of error messages, partially fixed by:

    [...]
\begin{document}

Lorem ipsum \mbox{\gls{co2}} \hl{dolor by Toto \mbox{\etal{}} {\cite{latexcompanion}} sit amet, consectetur adipiscing elit.
Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \mbox{\gls{co2}}.}

[...]


which fails if I include the first \mbox{\gls{co2}} in the \hl{} command. Maybe is it too long?

## Questions

• What exactly is the problem between soul's \hl{} command and the \cite{}, \etal{}or glossary entries?
• How to solve these issues? (the proposed solutions only work partially)
• Is there an alternative allowing something like \hl{} but which can surround an entire code block, possibly containing figures, text, etc. in other word is there a simple way to change the background color of a precise section?

The best I came up with is using a colour box (also see here), like this:

[...]
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
[...]
\begin{tcolorbox}[breakable,notitle,boxrule=0pt,colback=yellow,colframe=yellow]

Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.
Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.

\end{tcolorbox}
[...]


But it changes the text formatting...

• the best is to use lualatex and the lua-ul package. – Ulrike Fischer Sep 1 '20 at 12:46
• The xcolor and possibly the color package(s) solve(s) your highlight problem. I have no idea how to fix the strikeout problem, which is why this is a comment rather than an answer. – Someone Sep 1 '20 at 14:15
• @UlrikeFischer Yes indeed, I tried it and it worked perfectly, however I would like to stick to PdfLaTeX for legacy reasons if possible. @Someone Could you please be more explicit ? My question is actually about highlight but I am already using the color package. I tried xcolor as well with no positive outcome. – mranvick Sep 1 '20 at 14:23
• @mranvik The xcolor package has a \colorbox option, and it takes color name and text as input. eg. \colorbox{red}{hi} gives you the word hi in red highlight. – Someone Sep 1 '20 at 14:31
• either use lualatex or highlight only simple text. soul (and all other solutions) can't highlight arbitrary code. You can find work arounds for many cases, but as soon as things gets a bit more complicated they involve boxes, so you loose line breaking. – Ulrike Fischer Sep 1 '20 at 14:35

You can use the ulem package to achieve what you want, I guess. There is also a trick to make soul work.

EDIT: Concerning your second question whether there is a simple way to change the background color of a precise section possibly containing figures, text, etc., this is not so easy. You can use approaches that wrap your content into another environment, (like mdframed or tcolorbox), but this can affect your formatting and may also constrain certain commands/elements inside, especially floats. I've included an example based on tcolorbox below that works at least for simple content. Also, I added an alternative approach that uses afterpage to change the page background.

## ulem

Define your own hl command like this (cmp. https://tex.stackexchange.com/a/48549/223749):

\usepackage[normalem]{ulem}
\newcommand\hl{\bgroup\markoverwith
{\textcolor{yellow}{\rule[-.5ex]{.1pt}{2.5ex}}}\ULon}


With this, your MWE compiles fine. (Have a look at https://tex.stackexchange.com/a/254337/223749 for possible issues). Using the sout command from ulem you can also strikethrough the same text in the MWE without errors.

Full MWE (your MWE was missing a \makeglossaries command):

\documentclass{article}

\usepackage[normalem]{ulem}
\newcommand\hl{\bgroup\markoverwith
{\textcolor{yellow}{\rule[-.5ex]{.1pt}{2.5ex}}}\ULon}
\usepackage[acronym]{glossaries}
\usepackage{color}

\newacronym{co2}{CO$_2$}{carbon dioxide}
\newcommand{\etal}{\textit{et al.}}
\makeglossaries

\begin{document}

\hl{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}
\sout{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}

\begin{thebibliography}{9}
\bibitem{latexcompanion}
Michel Goossens, Frank Mittelbach, and Alexander Samarin.
\textit{The \LaTeX\ Companion}.
\end{thebibliography}

\end{document}


## soul

If you want to stick with soul, there is also a workaround for your issues using \soulregister following this answer (https://tex.stackexchange.com/a/139500/223749). This also provides a strikethrough possibility.

Full MWE:

\documentclass{article}

\usepackage{soul}
\soulregister\cite7
\soulregister\gls7
\soulregister\etal7

\usepackage[acronym]{glossaries}
\usepackage{color}

\newacronym{co2}{CO$_2$}{carbon dioxide}
\newcommand{\etal}{\textit{et al.}}
\makeglossaries

\begin{document}

\hl{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}
\st{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}

\begin{thebibliography}{9}
\bibitem{latexcompanion}
Michel Goossens, Frank Mittelbach, and Alexander Samarin.
\textit{The \LaTeX\ Companion}.
\end{thebibliography}

\end{document}


## tcolorbox

With some tricks, you can make a custom highlighting environment with tcolorbox work, that changes the background color of a precise section possibly containing figures, text, etc. Using a tcolorbox environment with all margins set to 0, and then restoring \parindent, you basically should get the text as it would be outside of the box. Additionally, by using the float package and specifying the H placement option you can include float environments like table or figure within -- although they will not actually float. I used ulem in the MWE for strikethrough.

The following MWE contains a figure and a table, as well as a page break.

\documentclass{article}

\usepackage{mwe}

\usepackage[acronym]{glossaries}
\usepackage{color}

\usepackage{float}
\usepackage[normalem]{ulem}

\newlength\defaultparindent
\AtBeginDocument{\setlength\defaultparindent{\parindent}}

\usepackage[breakable]{tcolorbox}
\newenvironment{highlighttcb}%
{\begin{tcolorbox}[colback=yellow,
colframe=white,
boxsep=0pt,
left=0pt,
right=0pt,
top=0pt,
bottom=0pt,
center,
valign=top,
before skip=0pt,
after skip=0pt,
width=\textwidth,
breakable,
boxrule=0pt,
frame empty,
sharp corners
]
\setlength{\parindent}{\defaultparindent}
}%
{\end{tcolorbox}}

\newacronym{co2}{CO$_2$}{carbon dioxide}
\newcommand{\etal}{\textit{et al.}}
\makeglossaries

\begin{document}
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext

\begin{highlighttcb}%
Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.

\sout{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}

\begin{figure}[H]
\includegraphics[width=.4\textwidth]{example-image-a}\hfill
\includegraphics[width=.4\textwidth]{example-image-b}
\caption{This is a figure caption within tcolorbox.}
\end{figure}

\blindtext
\blindtext
\blindtext
\blindtext
\begin{table}[H]
\centering
\begin{tabular}{lcr}
a & b & c \\
a & b & c \\
a & b & c \\
\end{tabular}
\caption{This is a table caption within tcolorbox.}
\end{table}
\blindtext

\end{highlighttcb}

\begin{thebibliography}{9}
\bibitem{latexcompanion}
Michel Goossens, Frank Mittelbach, and Alexander Samarin.
\textit{The \LaTeX\ Companion}.
\end{thebibliography}

\end{document}


## pagecolor / afterpage

Finally, an interesting approach is to partially color the page background using the color and afterpage packages (taken from https://tex.stackexchange.com/a/237427/223749). This has the advantage that you do not need to change your content at all and formatting will not be affected. A possible disadvantage (depending on your purpose) may be that the highlighting will include the left and right margins as well. Also, if you have floats inside your highlighting environment and they float out of it, they will not be highlighted.

Full MWE, including a figure and a page break:

\documentclass{article}

\usepackage{mwe}

\usepackage[acronym]{glossaries}
\usepackage{color}

\usepackage[normalem]{ulem}
\usepackage{afterpage}

\makeatletter
% Macro \changepagecolor has the same syntax as \pagecolor or \color
% with an optional argument and a mandatory argument.
\newcommand*{\changepagecolor}{%
\@ifnextchar[\@changepagecolor@i\@changepagecolor@ii
}
% Case: \changepagecolor[...]{...}
\def\@changepagecolor@i[#1]#2{%
\@changepagecolor@do{[{#1}]{#2}}%
}
% Case: \changepagecolor{...}
\newcommand*{\@changepagecolor@ii}[1]{%
\@changepagecolor@do{{#1}}%
}
\newcommand*{\@changepagecolor@do}[1]{%
% Fill the remaining space with a colored rule
\begingroup
\offinterlineskip
\hbox to 0pt{%
\kern-\paperwidth
\vtop to 0pt{%
\color#1%
\hrule width 2\paperwidth height \paperheight
\vss
}%
\hss
}%
\endgroup
% Set page color for the next page
\afterpage{\pagecolor#1}%
}
\makeatother

\newenvironment{highlight}%
{\changepagecolor{yellow}}%
{\changepagecolor{white}}

\newacronym{co2}{CO$_2$}{carbon dioxide}
\newcommand{\etal}{\textit{et al.}}
\makeglossaries

\begin{document}
\blindtext
\blindtext
\blindtext
\blindtext

\begin{highlight}
Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.

\sout{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}

\blindtext

\begin{figure}[!h]
\includegraphics[width=.4\textwidth]{example-image-a}\hfill
\includegraphics[width=.4\textwidth]{example-image-b}
\caption{This is a figure caption within the custom highlight environment}
\end{figure}
\end{highlight}

\blindtext

\begin{thebibliography}{9}
\bibitem{latexcompanion}
Michel Goossens, Frank Mittelbach, and Alexander Samarin.
\textit{The \LaTeX\ Companion}.
\end{thebibliography}

\end{document}


• Thank you very much for your answer. The hl solution is the best to my opinion since it allows the hyphenation of consectetur, which the ulem solution does not allow. Also I do not understand your point with \makeglossaries, the compilation works fine without it. Is there a particular necessity to add it? – mranvick Sep 2 '20 at 12:29
• You're right, the \makeglossaries is not necessary in the sense that it will cause the LaTeX compilation to fail. However, without it, the makeglossaries command will return an error (non-zero return code). But most of the time this is something you can ignore. – buddemat Sep 2 '20 at 12:42

An alternative is to use the "proofread" package and the "\hilite" command. However, you must use the command "\protec" before "\gls" and close the braces right after "\gls{}". Then use "\hilite" again to highlight the rest of the text.

\documentclass{article}
\usepackage{soulutf8} % Only necessary if there are accented characters inside the curly braces
\usepackage[xindy]{glossaries}
\makeglossaries

\newglossaryentry{euismod}{
name={euismod},
description={euismod sodales, sollicitudin vel, wisi. Morbi auctor lorem non justo.}
}

\begin{document}

Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, \hilite{accumsan eleifend.

Nam dui ligula, fringilla a, \protect\gls{euismod}}\hilite{ sodales, sollicitudin vel, wisi. Morbi auctor lorem non justo}. Nam lacus libero, pretium at.

\printglossary
\end{document}

• This does not work 100%, because long sentences containing \gls and \cite will have parts not highlighted. But it is the first approach that works at all. Thank you for your answer! – Marcel Hofgesang Mar 17 at 22:20