7

I have the following text, which I am trying to highlight with the package soul. Given below is the unhighlighted MWE

MWE (unhighlighted)

\documentclass[a4paper,12pt]{report}
\usepackage[usenames, dvipsnames]{color}
\usepackage{hyperref}
\usepackage{soul}
\usepackage{glossaries}
\newacronym{fbi}{FBI}{Federal Bureau of Investigation}

\definecolor{added}{RGB}{161,217,155}
\newcommand{\added}[1]{{\sethlcolor{added}\hl{#1}}}

\begin{document}
Fox Mulder is a \gls{fbi} special agent educated
at Oxford~\cite{latexcompanion} who believes in the existence of
extraterrestrials and a government conspiracy~\cite{einstein}
to hide the truth regarding them~\cite{knuthwebsite}.

Dana Scully is an \gls{fbi} special agent, a medical doctor,
and scientist who is Mulder's partner. In contrast to his
credulity, Scully is a skeptic, basing her beliefs on scientific
explanations.

\begin{thebibliography}{9}
\bibitem{latexcompanion} 
Michel Goossens, Frank Mittelbach, and Alexander Samarin. 
\textit{The \LaTeX\ Companion}. 
Addison-Wesley, Reading, Massachusetts, 1993.

\bibitem{einstein} 
Albert Einstein. 
\textit{Zur Elektrodynamik bewegter K{\"o}rper}. (German) 
[\textit{On the electrodynamics of moving bodies}]. 
Annalen der Physik, 322(10):891–921, 1905.

\bibitem{knuthwebsite} 
Knuth: Computers and Typesetting,
\\\texttt{http://www-cs-faculty.stanford.edu/\~{}uno/abcde.html}
\end{thebibliography}

\end{document}

enter image description here

When I highlight it using:

\added{Fox Mulder is a \gls{fbi} special agent educated
at Oxford~\cite{latexcompanion} who believes in the existence of
extraterrestrials and a government conspiracy~\cite{einstein}
to hide the truth regarding them~\cite{knuthwebsite}.

Dana Scully is an \gls{fbi} special agent, a medical doctor,
and scientist who is Mulder's partner. In contrast to his
credulity, Scully is a skeptic, basing her beliefs on scientific
explanations.}

All I get is:

enter image description here

How can I highlight text which contains glossaries, citations and references in LaTeX?

UPDATE

Nicola Talbot's answer works fine with single citations; however, it does on work with multiple citations. How to get Nicola Talbot's answer to work with multiple citations (MWE below)?

\documentclass[a4paper,12pt]{report}
\usepackage[usenames, dvipsnames]{color}
\usepackage{hyperref}
\usepackage{soul}
\usepackage{glossaries}

\newacronym{fbi}{FBI}{Federal Bureau of Investigation}

\definecolor{added}{RGB}{161,217,155}
\makeatletter
\newcommand{\added}[1]{{\let\glsunset\@gobble\sethlcolor{added}\hl{#1}}}
\makeatother

\begin{document}

\added{Fox Mulder is a \mbox{\gls{fbi}} special agent educated
at Oxford~{\cite{latexcompanion}} who believes in the existence of
extraterrestrials and a government conspiracy~{\cite{einstein,knuthwebsite}}
to hide the truth regarding them~{\cite{knuthwebsite}}.}

\glsunset{fbi}

\added{Dana Scully is an {\gls{fbi}} special agent, a medical doctor,
and scientist who is Mulder's partner. In contrast to his
credulity, Scully is a skeptic, basing her beliefs on scientific
explanations.}

\begin{thebibliography}{9}
\bibitem{latexcompanion} 
Michel Goossens, Frank Mittelbach, and Alexander Samarin. 
\textit{The \LaTeX\ Companion}. 
Addison-Wesley, Reading, Massachusetts, 1993.

\bibitem{einstein} 
Albert Einstein. 
\textit{Zur Elektrodynamik bewegter K{\"o}rper}. (German) 
[\textit{On the electrodynamics of moving bodies}]. 
Annalen der Physik, 322(10):891–921, 1905.

\bibitem{knuthwebsite} 
Knuth: Computers and Typesetting,
\\\texttt{http://www-cs-faculty.stanford.edu/\~{}uno/abcde.html}
\end{thebibliography}

\end{document}
0

1 Answer 1

5

You need to group the parts that \hl can't deal with, but unfortunately even this fails with the first use of \gls. This can be demonstrated with the following which only works because the first use is comes before the \hl part:

\documentclass[a4paper,12pt]{report}
\usepackage[usenames, dvipsnames]{color}
\usepackage{hyperref}
\usepackage{soul}
\usepackage{glossaries}

\newacronym{fbi}{FBI}{Federal Bureau of Investigation}

\definecolor{added}{RGB}{161,217,155}
\newcommand{\added}[1]{{\sethlcolor{added}\hl{#1}}}

\begin{document}
First use: \gls{fbi}.

\added{Fox Mulder is a {\gls{fbi}} special agent educated
at Oxford~{\cite{latexcompanion}} who believes in the existence of
extraterrestrials and a government conspiracy~{\cite{einstein}}
to hide the truth regarding them~{\cite{knuthwebsite}}.

Dana Scully is an {\gls{fbi}} special agent, a medical doctor,
and scientist who is Mulder's partner. In contrast to his
credulity, Scully is a skeptic, basing her beliefs on scientific
explanations.}

\begin{thebibliography}{9}
\bibitem{latexcompanion} 
Michel Goossens, Frank Mittelbach, and Alexander Samarin. 
\textit{The \LaTeX\ Companion}. 
Addison-Wesley, Reading, Massachusetts, 1993.

\bibitem{einstein} 
Albert Einstein. 
\textit{Zur Elektrodynamik bewegter K{\"o}rper}. (German) 
[\textit{On the electrodynamics of moving bodies}]. 
Annalen der Physik, 322(10):891–921, 1905.

\bibitem{knuthwebsite} 
Knuth: Computers and Typesetting,
\\\texttt{http://www-cs-faculty.stanford.edu/\~{}uno/abcde.html}
\end{thebibliography}

\end{document}

image of highlighted text

Unfortunately even adding \mbox fails:

\documentclass[a4paper,12pt]{report}
\usepackage[usenames, dvipsnames]{color}
\usepackage{hyperref}
\usepackage{soul}
\usepackage{glossaries}

\newacronym{fbi}{FBI}{Federal Bureau of Investigation}

\definecolor{added}{RGB}{161,217,155}
\newcommand{\added}[1]{{\sethlcolor{added}\hl{#1}}}

\begin{document}

\added{Fox Mulder is a \mbox{\gls{fbi}} special agent.}

\end{document}

This results in the error:

! Package soul Error: Reconstruction failed.

Part of the problem seems to be the unsetting of the first use flag, which can be demonstrated with:

\documentclass[a4paper,12pt]{report}
\usepackage[usenames, dvipsnames]{color}
\usepackage{hyperref}
\usepackage{soul}
\usepackage{glossaries}

\newacronym{fbi}{FBI}{Federal Bureau of Investigation}

\definecolor{added}{RGB}{161,217,155}
\newcommand{\added}[1]{{\sethlcolor{added}\hl{#1}}}

\begin{document}

\added{Fox Mulder is a \mbox{\glsunset{fbi}} FBI special agent.}

\end{document}

Adding \protect removes the error message:

\documentclass[a4paper,12pt]{report}
\usepackage[usenames, dvipsnames]{color}
\usepackage{hyperref}
\usepackage{soul}
\usepackage{glossaries}

\newacronym{fbi}{FBI}{Federal Bureau of Investigation}

\definecolor{added}{RGB}{161,217,155}
\newcommand{\added}[1]{{\sethlcolor{added}\hl{#1}}}

\begin{document}

\added{Fox Mulder is a \mbox{\protect\glsunset{fbi}} FBI special agent.}

\end{document}

Unfortunately, although \protect\gls removes the error, it also removes the highlighting:

\documentclass[a4paper,12pt]{report}
\usepackage[usenames, dvipsnames]{color}
\usepackage{hyperref}
\usepackage{soul}
\usepackage{glossaries}

\newacronym{fbi}{FBI}{Federal Bureau of Investigation}

\definecolor{added}{RGB}{161,217,155}
\newcommand{\added}[1]{{\sethlcolor{added}\hl{#1}}}

\begin{document}

\added{Fox Mulder is a \protect\gls{fbi} special agent.}

\end{document}

Fox Mulder is a Federal Bureau of Investigation (FBI) special agent.

The extension package glossaries-extra, as from version 1.30, has a way of temporarily buffering pending instances of \glsunset. This is switched on with \GlsXtrStartUnsetBuffering and switched off with \GlsXtrStopUnsetBuffering (which applies \glsunset to the saved labels):

\documentclass[a4paper,12pt]{report}
\usepackage[usenames, dvipsnames]{color}
\usepackage{hyperref}
\usepackage{soul}
\usepackage{glossaries-extra}

\setabbreviationstyle[acronym]{long-short}% glossaries-extra.sty
\newacronym{fbi}{FBI}{Federal Bureau of Investigation}

\definecolor{added}{RGB}{161,217,155}
\newcommand{\added}[1]{{\sethlcolor{added}\hl{#1}}}

\begin{document}

\GlsXtrStartUnsetBuffering % glossaries-extra.sty v1.30+
\added{Fox Mulder is a \mbox{\gls{fbi}} special agent.}
\GlsXtrStopUnsetBuffering % glossaries-extra.sty v1.30+

\end{document}

Fox Mulder is a Federal Bureau of Investigation (FBI) special agent.

For convenience, this can be incorporated into the definition of \added:

\documentclass[a4paper,12pt]{report}
\usepackage[usenames, dvipsnames]{color}
\usepackage{hyperref}
\usepackage{soul}
\usepackage{glossaries-extra}

\setabbreviationstyle[acronym]{long-short}% glossaries-extra.sty
\newacronym{fbi}{FBI}{Federal Bureau of Investigation}

\definecolor{added}{RGB}{161,217,155}
\newcommand{\added}[1]{%
 \GlsXtrStartUnsetBuffering % glossaries-extra.sty v1.30+
 {\sethlcolor{added}\hl{#1}}%
 \GlsXtrStopUnsetBuffering % glossaries-extra.sty v1.30+
}

\begin{document}

\added{Fox Mulder is a \mbox{\gls{fbi}} special agent.}

\end{document}

Unfortunately \mbox is still needed, which can cause paragraph justification problems as the content of \mbox can't be broken across a line. The other problem is that if \gls{fbi} is used multiple times within the same \added argument they will all be fully expanded as the first use flag can't be unset until \GlsXtrStopUnsetBuffering.

Another possibility with glossaries-extra v1.30+ is to move \gls outside of \added and use the textformat key to push \added further inside the internal workings of \gls to help \hl work:

\documentclass[a4paper,12pt]{report}
\usepackage[usenames, dvipsnames]{color}
\usepackage{hyperref}
\usepackage{soul}
\usepackage{glossaries-extra}

\setabbreviationstyle[acronym]{long-short}% glossaries-extra.sty
\newacronym{fbi}{FBI}{Federal Bureau of Investigation}

\definecolor{added}{RGB}{161,217,155}
\newcommand{\added}[1]{{\sethlcolor{added}\hl{#1}}}

% command for use in `textformat` (glossaries-extra v1.30+):
\newrobustcmd{\gadded}[1]{%
  {%
   \def\glsxtrabbreviationfont##1{\GlsXtrExpandedFmt{\added}{##1}}%
   \def\glsxtrregularfont##1{\GlsXtrExpandedFmt{\added}{##1}}%
   #1%
  }%
}

\begin{document}

\added{Fox Mulder is a }\gls[textformat=gadded]{fbi}\added{ special agent.}

\end{document}

Fox Mulder is a Federal Bureau of Investigation (FBI) special agent.

With just the base glossaries package, or with earlier versions of glossaries-extra. the only partial solution I can think of in this case is a bit of a fudge:

\documentclass[a4paper,12pt]{report}
\usepackage[usenames, dvipsnames]{color}
\usepackage{hyperref}
\usepackage{soul}
\usepackage{glossaries}

\newacronym{fbi}{FBI}{Federal Bureau of Investigation}

\definecolor{added}{RGB}{161,217,155}
\makeatletter
\newcommand{\added}[1]{{\let\glsunset\@gobble\sethlcolor{added}\hl{#1}}}
\makeatother

\begin{document}

\added{Fox Mulder is a \mbox{\gls{fbi}} special agent educated
at Oxford~{\cite{latexcompanion}} who believes in the existence of
extraterrestrials and a government conspiracy~{\cite{einstein}}
to hide the truth regarding them~{\cite{knuthwebsite}}.}

\glsunset{fbi}

\added{Dana Scully is an {\gls{fbi}} special agent, a medical doctor,
and scientist who is Mulder's partner. In contrast to his
credulity, Scully is a skeptic, basing her beliefs on scientific
explanations.}

\begin{thebibliography}{9}
\bibitem{latexcompanion} 
Michel Goossens, Frank Mittelbach, and Alexander Samarin. 
\textit{The \LaTeX\ Companion}. 
Addison-Wesley, Reading, Massachusetts, 1993.

\bibitem{einstein} 
Albert Einstein. 
\textit{Zur Elektrodynamik bewegter K{\"o}rper}. (German) 
[\textit{On the electrodynamics of moving bodies}]. 
Annalen der Physik, 322(10):891–921, 1905.

\bibitem{knuthwebsite} 
Knuth: Computers and Typesetting,
\\\texttt{http://www-cs-faculty.stanford.edu/\~{}uno/abcde.html}
\end{thebibliography}

\end{document}

image of highlighted text

For multiple citations it seems you need \mbox. For example, \mbox{\cite{einstein,knuthwebsite}}.

6
  • Not tested, but could we let the content of highlighted text to expand inside a vbox and use it after that? This was my idea when show the question
    – koleygr
    Apr 8, 2018 at 11:59
  • @koleygr It works with a savebox: \newsavebox\mysbox\sbox\mysbox{\gls{fbi}}\added{Fox Mulder is a {\usebox\mysbox}...}. I don't know how well it would work with a vbox. Apr 8, 2018 at 21:01
  • thanks, Already tried but failed to use the full text in a vbox ... I don't know why yet... but my idea doesn't work (to make a newcommand that will save and use the content)
    – koleygr
    Apr 8, 2018 at 21:22
  • @NicolaTalbot The solution works fine, but I forgot to consider another scenario: multiple citations. With multiple citations I get a black square.
    – nxkryptor
    Apr 9, 2018 at 5:31
  • @nxkryptor It seems to work if you added \mbox: \mbox{\cite{einstein,knuthwebsite}}. Apr 12, 2018 at 10:49

You must log in to answer this question.

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