13

I am using the natbib and hyperref packages:

\usepackage{natbib}

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

With my current settings (above) both the name and the year are hyperlinked.

My question is, whether there is a way to hyperlink (or color) only the year-not the author(s).

I have the impression reading this post hyperlink name with biblatex authoryear that biblatex does it by default. However changing to biblatex is not an option for me at this point.

Any helpful comments will be greatly appreciated.

15

Using etoolbox you can patch natbib's internal author-year citation command so that it hyperlinks only the year whenever both the year and name are included in the citation label. The code below demonstrates patches for natbib.sty, version 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}

enter image description here

  • It works very nicely. Thank you very much Audrey! – Bonnie Feb 16 '12 at 21:34
  • How to do the same in case of \citepalias ? – Abdelhak Elfengour Jan 31 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.