1

Here is an example:

\documentclass{article}

\begin{filecontents}{example.bib}
@article{doe-2015,
    Author = {J. Doe and M. Mouse and D. Duck and W. Disney},
    Doi = {ija.foo/bar},
    Journal = {International Journal of Articles},
    Title = {Title of the article},
    Year = {2015}}
\end{filecontents}

\usepackage[ backend=biber, style=authoryear-comp, natbib ]{biblatex}
\bibliography{example.bib}

\usepackage[ colorlinks, citecolor=red ]{hyperref}

\begin{document}
\citep{doe-2015}
\printbibliography
\end{document}

In the output pdf (I'm using XeLaTeX), text citations have links which point to the references section page. I would like to change these in-text links so that they point to the DOI url (if present). Any suggestion would be welcome.

Mathieu
  • 559

1 Answers1

1

We can redefine the bibhyperref format (which produces the linked bits of the citation) to prefer linking to DOIs, URLs eprints etc.

\DeclareFieldFormat{bibhyperreforig}{\bibhyperref{#1}}

\DeclareFieldFormat{bibhyperref}{%
  \ifhyperref
    {\iffieldundef{doi}
       {\iffieldundef{eprint}
           {\iffieldundef{url}
             {\printtext[bibhyperreforig]{#1}}
             {\href{\thefield{url}}{#1}}}
           {\mkhrefeprint{#1}}}
       {\href{http://dx.doi.org/\thefield{doi}}{#1}}}
    {\printtext[bibhyperreforig]{#1}}}

Links to eprints are quite tricky, so we need a helper function (which is completely due to Audrey).

\makeatletter
\newrobustcmd*{\mkhrefeprint}[1]{%
  \iffieldequalstr{eprinttype}{arxiv}
    {\href{http://arxiv.org/\abx@arxivpath/\thefield{eprint}}{#1}}
    {\iffieldequalstr{eprinttype}{hdl}
       {\href{http://hdl.handle.net/\thefield{eprint}}{#1}}
       {\iffieldequalstr{eprinttype}{jstor}
          {\href{http://www.jstor.org/stable/\thefield{eprint}}{#1}}
          {\iffieldequalstr{eprinttype}{pubmed}
             {\href{http://www.ncbi.nlm.nih.gov/pubmed/\thefield{eprint}}{#1}}
             {\iffieldequalstr{eprinttype}{googlebooks}
                {\href{http://books.google.com/books?id=\thefield{eprint}}{#1}}
                {#1}}}}}}
\makeatother

Now citation links are to DOIs, eprints, URLs or to the bibliography (in that order).

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[backend=biber, style=authoryear-comp, natbib]{biblatex}
\addbibresource{biblatex-examples.bib}

\usepackage[colorlinks, citecolor=red ]{hyperref}

\DeclareFieldFormat{bibhyperreforig}{\bibhyperref{#1}}

\DeclareFieldFormat{bibhyperref}{%
  \ifhyperref
    {\iffieldundef{doi}
       {\iffieldundef{eprint}
           {\iffieldundef{url}
             {\printtext[bibhyperreforig]{#1}}
             {\href{\thefield{url}}{#1}}}
           {\mkhrefeprint{#1}}}
       {\href{http://dx.doi.org/\thefield{doi}}{#1}}}
    {\printtext[bibhyperreforig]{#1}}}

\makeatletter
\newrobustcmd*{\mkhrefeprint}[1]{%
  \iffieldequalstr{eprinttype}{arxiv}
    {\href{http://arxiv.org/\abx@arxivpath/\thefield{eprint}}{#1}}
    {\iffieldequalstr{eprinttype}{hdl}
       {\href{http://hdl.handle.net/\thefield{eprint}}{#1}}
       {\iffieldequalstr{eprinttype}{jstor}
          {\href{http://www.jstor.org/stable/\thefield{eprint}}{#1}}
          {\iffieldequalstr{eprinttype}{pubmed}
             {\href{http://www.ncbi.nlm.nih.gov/pubmed/\thefield{eprint}}{#1}}
             {\iffieldequalstr{eprinttype}{googlebooks}
                {\href{http://books.google.com/books?id=\thefield{eprint}}{#1}}
                {#1}}}}}}
\makeatother

\begin{document}
\citep{kastenholz,cicero,baez/article,ctan}

\printbibliography
\end{document}

enter image description here

moewe
  • 175,683