Single link for natbib and hyperref in (Author, Year) citation style
natbib and hyperref create by default two links to the bibentry in the reference section|chapter of the document:
\begin{filecontents}{\jobname.bib}
@book{author00,
title = ,
publisher = {Alpha},
year = {2008},
author = {Author, A},
address = {London}
}
\end{filecontents}
\documentclass{article}
\usepackage{natbib}
\usepackage{hyperref}
\hypersetup{colorlinks,citecolor=red}
\begin{document}
\noindent
citet: \citet{author00}, \citet[see][p. 20]{author00} \\
citep: \citep{author00}, \citep[see][p. 20]{author00}
\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}
In order to avoid two links to the bibliography in the style [author][year], I took Audrey's solution to an older question and added it to my preamble.
\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
Which gets me almost where I want to be, but leaves me with two issues I would like to be solved:
while citet includes pre- and postnotes in the link, \citep doesn't. How can \citep be changed to match citet's appearance?
is there a way to color the brackets and comma's that are typeset in black in the unmodified first example also in black in the modified second example without remving them from the link?
natbib and hyperref create by default two links to the bibentry in the reference section|chapter of the document:
\begin{filecontents}{\jobname.bib}
@book{author00,
title = ,
publisher = {Alpha},
year = {2008},
author = {Author, A},
address = {London}
}
\end{filecontents}
\documentclass{article}
\usepackage{natbib}
\usepackage{hyperref}
\hypersetup{colorlinks,citecolor=red}
\begin{document}
\noindent
citet: \citet{author00}, \citet[see][p. 20]{author00} \\
citep: \citep{author00}, \citep[see][p. 20]{author00}
\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}
In order to avoid two links to the bibliography in the style [author][year], I took Audrey's solution to an older question and added it to my preamble.
\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
Which gets me almost where I want to be, but leaves me with two issues I would like to be solved:
while citet includes pre- and postnotes in the link, \citep doesn't. How can \citep be changed to match citet's appearance?
is there a way to color the brackets and comma's that are typeset in black in the unmodified first example also in black in the modified second example without remving them from the link?
No comments:
Post a Comment