我有以下文本,我试图用包突出显示它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}
扩展包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}
为了方便起见,可以将其纳入到定义中\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}
如果只使用基础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}}
。