natbib 和 hyperref 的(作者,年份)样式产生两个链接

natbib 和 hyperref 的(作者,年份)样式产生两个链接

我使用的是bioinfo带有和的样式natbibhyperref使用(作者,年份)样式的引文。 pdflatex为每个引文生成两个链接框:一个用于作者,一个用于年份。我更希望获得整个 [(作者,年份)] 的单个链接,而不是 ([作者],[年份])。我可以关闭 PDF 链接框并使用颜色来稍微改善其外观,但我仍然希望只有一个链接。

提前感谢有关如何解决此问题的任何建议。

这是一个简单的例子:

\documentclass{article}

\usepackage{natbib}
\usepackage{hyperref}
\begin{filecontents}{\jobname.bib}
@book{adams80,
  title = {The Restaurant at the End of the Universe},
  author = {Douglas Adams},
  series = {The Hitchhiker's Guide to the Galaxy},
  publisher = {Pan Macmillan},
  year = {1980}
}
\end{filecontents}
\begin{document}

A theory about the inexplicability of the Universe has been proposed by \citet{adams80}.

\bibliographystyle{plainnat}
\bibliography{\jobname}

\end{document}

答案1

这两个链接由命令创建\hyper@natlinkbreak,该命令由 定义hyperref.sty。要删除中断,您可以通过natbib.sty在序言中插入以下几行来恢复到临时定义:

\makeatletter
\renewcommand\hyper@natlinkbreak[2]{#1}
\makeatother

这个简单的修复结果并不理想;\cite和生成的链接\citet不包含后记和右括号,这看起来很奇怪。这可以通过natbib使用修补内部宏来解决etoolbox。在紧凑引用中避免使用虚假的右括号是很麻烦的。下面的补丁通过抑制和其变体中的紧凑标签来避开这个问题\citet

\documentclass{article}
\usepackage{natbib}
\usepackage{hyperref}
\usepackage{etoolbox}

\makeatletter

\pretocmd{\NAT@citex}{%
  \let\NAT@hyper@\NAT@hyper@citex
  \def\NAT@postnote{#2}%
  \setcounter{NAT@total@cites}{0}%
  \setcounter{NAT@count@cites}{0}%
  \forcsvlist{\stepcounter{NAT@total@cites}\@gobble}{#3}}{}{}
\newcounter{NAT@total@cites}
\newcounter{NAT@count@cites}
\def\NAT@postnote{}

% include postnote and \citet closing bracket in hyperlink
\def\NAT@hyper@citex#1{%
  \stepcounter{NAT@count@cites}%
  \hyper@natlinkstart{\@citeb\@extra@b@citeb}#1%
  \ifnumequal{\value{NAT@count@cites}}{\value{NAT@total@cites}}
    {\ifNAT@swa\else\if*\NAT@postnote*\else%
     \NAT@cmt\NAT@postnote\global\def\NAT@postnote{}\fi\fi}{}%
  \ifNAT@swa\else\if\relax\NAT@date\relax
  \else\NAT@@close\global\let\NAT@nm\@empty\fi\fi% avoid compact citations
  \hyper@natlinkend}
\renewcommand\hyper@natlinkbreak[2]{#1}

% avoid extraneous postnotes, closing brackets
\patchcmd{\NAT@citex}
  {\ifNAT@swa\else\if*#2*\else\NAT@cmt#2\fi
   \if\relax\NAT@date\relax\else\NAT@@close\fi\fi}{}{}{}
\patchcmd{\NAT@citex}
  {\if\relax\NAT@date\relax\NAT@def@citea\else\NAT@def@citea@close\fi}
  {\if\relax\NAT@date\relax\NAT@def@citea\else\NAT@def@citea@space\fi}{}{}

\makeatother

\begin{filecontents}{\jobname.bib}
@Book{companion,
  author = {Goossens, Michel and Mittelbach, Frank and Samarin, Alexander},
  title = {The LaTeX Companion},
  edition = {1},
  publisher = {Addison-Wesley},
  location = {Reading, Mass.},
  year = {1994}}
@Book{adams,
  title = {The Restaurant at the End of the Universe},
  author = {Douglas Adams},
  series = {The Hitchhiker's Guide to the Galaxy},
  publisher = {Pan Macmillan},
  year = {1980}}
\end{filecontents}

\renewcommand{\baselinestretch}{1.25}

\begin{document}
\noindent
cite: \cite{adams}, \cite{companion} \\
citet: \citet{adams}, \citet[see][p. 20]{adams} \\
multi citet: \citet{companion,adams} \\
citep: \citep{adams}, \citep[see][p. 20]{companion} \\
multi citep: \citep{companion,adams} \\
citetext, citealp: \citetext{see \citealp{companion}, or even better \citealp{adams}} \\
citeauthor: \citeauthor{adams}, \citeauthor{companion} \\
citeyear: \citeyear{adams}, \citeyear{companion} \\
\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}

在此处输入图片描述

相关内容