AFanthomme
AFanthomme

Reputation: 45

Making in-line citations link to doi (instead of bibliography item)?

I've been given by a friend a solution to adding links to doi (or url) into my bibliography items, which was done by modifying the format.title macro in apa.bst:

FUNCTION {format.title}
{ title empty$
    { "" }
    { title "t" change.case$
      doi empty$
        {url empty$
            { }
            { "\href{" url * "}{" * swap$ * "}" * }
         if$}
        { "\href{http://dx.doi.org/" doi * "}{" * swap$ * "}" * }
      if$
    }
  if$
}

That works very well, but now I would like to make it so that when I click the in-line citations (generated by \cite or \citep) it links to the same url (or doi) as the bibliography item (to be able to do "text-> doi in browser" instead of "text -> bibliography -> doi in browser", which loses position in the pdf everytime I want to check out a reference...)

Anyone knows where the natbib/hyperref interaction happens and how to "hack" it to link to my url/doi instead of the bib item label?

Upvotes: 1

Views: 2199

Answers (1)

As already said in the comments, if a bibtex style does not happen to produce exactly the result you are looking for, you are better off with biblatex - things are much easier to customise.

Here a quick example how to wrap the a link to the doi around the citation. The year will still point to the bibliography, so you'll have the choice where you want to go:

\documentclass{article}

\usepackage[style=apa]{biblatex}
\usepackage{hyperref}

\addbibresource{biblatex-examples.bib}

\makeatletter
\renewbibmacro*{cite}{%
  \iffieldequals{fullhash}{\cbx@lasthash}
% Multiple cites in one command
   {\setunit{\compcitedelim}%
    \usebibmacro{cite:plabelyear+extradate}}%
% Single cite
   {\ifnameundef{labelname}
% No author/editor
     {\usebibmacro{cite:noname}%
       \setunit{\printdelim{nameyeardelim}}%
       \usebibmacro{cite:plabelyear+extradate}%
       \savefield{fullhash}{\cbx@lasthash}}
% Normal cite
     {  \href{http://dx.doi.org/\thefield{doi}}{\ifnameundef{shortauthor}
       {\printnames{labelname}}%
       {\cbx@apa@ifnamesaved
         {\printnames{shortauthor}}
         {\ifnameundef{groupauthor}
           {\printnames[labelname]{author}}
           {\printnames[labelname]{groupauthor}}%
          \addspace\printnames[sabrackets]{shortauthor}}}%
       \setunit{\printdelim{nameyeardelim}}%
      \usebibmacro{cite:plabelyear+extradate}%
      \savefield{fullhash}{\cbx@lasthash}}}}%
   \setunit{\multicitedelim}}
\makeatother

\begin{document}

\cite{kastenholz,sigfridsson}

\printbibliography

\end{document}

enter image description here

Upvotes: 1

Related Questions