I am using natbib for citations and often have to
- reference specific page numbers of sources, and
- reference sources in the possessive.
I am using hyperref for internal hyperlinks and I have not been able to find a way for hyperref to link either the possessive or the page numbers (in some cases - it seems to work with citet). MWE shown below, with a partial solution found on this site. Any suggestions?
\documentclass{article}
\usepackage{natbib}
\def\citeaposay#1{\citeauthor{#1}'s (\citeyear{#1})}
\usepackage{etoolbox}
% This code is from
%http://tex.stackexchange.com/questions/24136/natbib-and-hyperref-for-author-year-style-produces-two-links
\makeatletter
% Current citation \cite or \citet?
\newbool{NAT@bool@patch}
% Closing parenthesis already printed in hyperlink?
\newtoggle{NAT@tog@close}
% Flag \cite and \citet
\pretocmd{\citet}
{\global\booltrue{NAT@bool@patch}}
{}% Do nothing if patch works
{}% Do nothing if patch fails
\pretocmd{\cite}
{\global\booltrue{NAT@bool@patch}}
{}% Do nothing if patch works
{}% Do nothing if patch fails
% Don't print postnote if already printed in hyperlink
\newcommand\NAT@postnote[1]{%
\iftoggle{NAT@tog@close}
{}
{#1}}
% Don't print closing bracket if already printed in hyperlink
% NB: We have to reset this so that all brackets are printed in multi-citations
\renewcommand\NAT@@close{%
\ifbool{NAT@bool@patch}
{\iftoggle{NAT@tog@close}
{\global\togglefalse{NAT@tog@close}}
{}}
{\ifNAT@par\NAT@close\fi}}
% Patch main cite command to issue new postnote command
\patchcmd{\NAT@citex}
{\else\NAT@cmt#2}
{\else\NAT@postnote{\NAT@cmt#2}}
{}% Do nothing if patch works
{}% Do nothing if patch fails
% Patch main cite command to include postnote/closing bracket in hyperlink
\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@hyper@{%
\NAT@nmfmt{\NAT@nm}%
\hyper@natlinkbreak{\NAT@spacechar\NAT@@open\if*#1*\else#1\NAT@spacechar\fi}%
{\@citeb\@extra@b@citeb}%
\ifbool{NAT@bool@patch}
{\global\toggletrue{NAT@tog@close}%
\NAT@date%
\ifNAT@swa\else\if*#2*\else\NAT@cmt#2\fi\fi%
\NAT@close}
{\NAT@date}}}
{}% Do nothing if patch works
{}% Do nothing if patch fails
% Reset flags after citation is printed
\apptocmd{\NAT@citex}
{\global\boolfalse{NAT@bool@patch}%
\global\togglefalse{NAT@tog@close}}
{}% Do nothing if patch works
{}% Do nothing if patch fails
% Don't break hyperlinks between name and year
\renewcommand\hyper@natlinkbreak[2]{#1}
\makeatother
\usepackage{hyperref}
\hypersetup{
colorlinks=true,
citecolor=red
}
\begin{filecontents}{\jobname.bib}
@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}
\begin{document}
\bibpunct[]{(}{)}{;}{a}{,}{,}
\noindent
citet with page numbers: \citet[:23-25]{adams} is a good book. (here both parentheses should be hyperlinked) \\
citep with page numbers: As suggested by him \citep[:23-25]{adams}. (here :23-25 should be hyperlinked)\\
citeyear with page numbers: As suggested by him (\citeyear[:23-25]{adams}). (here :23-25 should be hyperlinked)\\
citeaposay with possessive: \citeaposay{adams} book suggests... (here 's should be hyperlinked)
\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}
