如何突出显示包含词汇表和引文的文本?

如何突出显示包含词汇表和引文的文本?

我有以下文本,我试图用包突出显示它soul。下面给出的是未突出显示的 MWE

MWE(未突出显示)

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

在此处输入图片描述

当我使用以下方法突出显示它时:

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

我得到的是:

在此处输入图片描述

如何在 LaTeX 中突出显示包含词汇表、引文和参考文献的文本?

更新

Nicola Talbot 的回答对于单次引用,它可以很好地工作;然而,对于多次引用,它却不起作用。如何获得Nicola Talbot 的回答使用多个引用(下面的 MWE)?

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

答案1

\hl您需要对无法处理的部分进行分组,但不幸的是,即使这样,第一次使用 也会失败\gls。这可以通过以下代码演示,它之所以有效,是因为第一次使用 位于 部分之前\hl

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

突出显示文本的图像

不幸的是甚至添加也\mbox失败:

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

这会导致错误:

! Package soul Error: Reconstruction failed.

部分问题似乎是第一次使用标志未设置,可以通过以下方式证明:

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

添加后\protect会删除错误消息:

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

不幸的是,虽然\protect\gls消除了错误,但它也消除了突出显示:

\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) 是一名联邦调查局 (FBI) 特工。

扩展包glossaries-extra从 1.30 版开始, 可以临时缓冲 待处理的 实例\glsunset。此功能通过 开启\GlsXtrStartUnsetBuffering或关闭\GlsXtrStopUnsetBuffering(适用\glsunset于已保存的标签):

\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) 是一名联邦调查局 (FBI) 特工。

为了方便起见,可以将其纳入到定义中\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}

不幸的\mbox是,仍然需要,这可能会导致段落对齐问题,因为的内容\mbox不能跨行。另一个问题是,如果\gls{fbi}在同一个参数中多次使用,\added它们都将完全展开,因为第一次使用标志不能取消设置,直到\GlsXtrStopUnsetBuffering

v1.30+的另一种可能性glossaries-extra是移出\gls\added使用textformat键进一步推动\added内部运作\gls以帮助\hl工作:

\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) 是一名联邦调查局 (FBI) 特工。

如果只使用基础glossaries包或者早期版本glossaries-extra,我能想到的唯一部分解决方案在这种情况下就有点敷衍了事了:

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

突出显示文本的图像

对于多次引用,似乎需要\mbox。例如,\mbox{\cite{einstein,knuthwebsite}}

相关内容