使用 natbib 和 hyperref 时如何仅超链接年份部分

使用 natbib 和 hyperref 时如何仅超链接年份部分

我正在使用natbibhyperref包:

\usepackage{natbib}

\usepackage[pagebackref=true, plainpages=false, bookmarks, bookmarksnumbered,
colorlinks, linktocpage=true, linkcolor=blue, citecolor=blue, filecolor=black,
urlcolor=blue]{hyperref}

根据我当前的设置(以上),名称和年份都是超链接。

我的问题是,是否有办法超链接(或颜色)仅年份-不是作者。

读这篇文章时我有这样的印象超链接名称与 biblatex 作者年份biblatex 默认会这样做。但是目前我无法切换到 biblatex。

任何有用的评论都将受到高度赞赏。

答案1

使用etoolbox可以修补natbib的内部作者年份引用命令,这样当引用标签中同时包含年份和名称时,它只会超链接年份。以下代码演示了natbib.sty版本的修补程序2010/09/13 8.31b

\documentclass{article}
\usepackage{natbib}
\usepackage[colorlinks]{hyperref}
\usepackage{etoolbox}

\makeatletter

% Patch case where name and year are separated by aysep
\patchcmd{\NAT@citex}
  {\@citea\NAT@hyper@{%
     \NAT@nmfmt{\NAT@nm}%
     \hyper@natlinkbreak{\NAT@aysep\NAT@spacechar}{\@citeb\@extra@b@citeb}%
     \NAT@date}}
  {\@citea\NAT@nmfmt{\NAT@nm}%
   \NAT@aysep\NAT@spacechar\NAT@hyper@{\NAT@date}}{}{}

% Patch case where name and year are separated by opening bracket
\patchcmd{\NAT@citex}
  {\@citea\NAT@hyper@{%
     \NAT@nmfmt{\NAT@nm}%
     \hyper@natlinkbreak{\NAT@spacechar\NAT@@open\if*#1*\else#1\NAT@spacechar\fi}%
       {\@citeb\@extra@b@citeb}%
     \NAT@date}}
  {\@citea\NAT@nmfmt{\NAT@nm}%
   \NAT@spacechar\NAT@@open\if*#1*\else#1\NAT@spacechar\fi\NAT@hyper@{\NAT@date}}
  {}{}

\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:life,
  title = {Life, the Universe and Everything},
  author = {Adams, Douglas},
  series = {The Hitchhiker's Guide to the Galaxy},
  publisher = {Pan Macmillan},
  year = {1980}}
@book{adams:rest,
  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}

\newcommand{\cmd}[1]{\textbackslash\texttt{#1}}
\defcitealias{companion}{GMS}
\begin{document}
\noindent
\cmd{citet}: \citet[1--10]{companion,adams:rest,adams:life} \\
\cmd{cite}: \cite[e.g.][100]{adams:life}; \cite{adams:rest} \\
\cmd{citep}: \citep[e.g.][1--10]{adams:rest,adams:life} \\
\cmd{citetext}; \cmd{citealp}:
  \citetext{see \citealp[10]{adams:rest}; or even better \citealp{adams:life}} \\
\cmd{citeauthor}, \cmd{citeauthor*}: \citeauthor{adams:life}; \citeauthor*{companion} \\
\cmd{citeyear}, \cmd{citeyearpar}: \citeyear{adams:life}; \citeyearpar{adams:rest} \\
\cmd{citetalias}, \cmd{citepalias}: \citetalias{companion}; \citepalias{companion} \\
\cmd{citenum}: \citenum{adams:life,adams:rest}
\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}

在此处输入图片描述

相关内容