Alternative to the soul package?
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}.
Addison-Wesley, Reading, Massachusetts, 1993.
\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}.
Addison-Wesley, Reading, Massachusetts, 1993.
\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}.
Addison-Wesley, Reading, Massachusetts, 1993.
\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}.
Addison-Wesley, Reading, Massachusetts, 1993.
\end{thebibliography}
\end{document}