[Tex/LaTex] Biblatex: use doi only if there is no URL

biblatexdoiurls

Is there a way to insert a doi of an entry only in case it is missing a URL field? It happens a lot that the doi and URL are essentially the same, and inserting them both yields a redundancy. I am using biblatex with biber.

PS: If you think this goal doesn't make sense please share your thoughts!

Best Answer

You can redefine the bibmacro doi+eprint+url so that the doi field is printed only if the url field is not defined, that is, add the following lines in the preamble:

\renewbibmacro*{doi+eprint+url}{%
  \iftoggle{bbx:doi}
    {\iffieldundef{url}{\printfield{doi}}{}}
    {}%
  \newunit\newblock
  \iftoggle{bbx:eprint}
    {\usebibmacro{eprint}}
    {}%
  \newunit\newblock
  \iftoggle{bbx:url}
    {\usebibmacro{url+urldate}}
    {}}

MWE

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[backend=biber]{biblatex}

\addbibresource{\jobname.bib}

\renewbibmacro*{doi+eprint+url}{%
  \iftoggle{bbx:doi}
    {\iffieldundef{url}{\printfield{doi}}{}}
    {}%
  \newunit\newblock
  \iftoggle{bbx:eprint}
    {\usebibmacro{eprint}}
    {}%
  \newunit\newblock
  \iftoggle{bbx:url}
    {\usebibmacro{url+urldate}}
    {}}

\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@article{kastenholz1,
  hyphenation = {american},
  author = {Kastenholz, M. A. and H{\"u}nenberger, Philippe H.},
  indextitle = {Computation of ionic solvation free energies},
  title = {Computation of methodology\hyphen independent ionic solvation free
    energies from molecular simulations},
  subtitle = {I. The electrostatic potential in molecular liquids},
  journaltitle = jchph,
  volume = {124},
  eid = {124106},
  date = {2006},
  url = {http://dx.doi.org/10.1063/1.2172593},
  urldate = {2006-10-01},
  doi = {10.1063/1.2172593},
}
@article{kastenholz2,
  hyphenation = {american},
  author = {Kastenholz, M. A. and H{\"u}nenberger, Philippe H.},
  indextitle = {Computation of ionic solvation free energies},
  title = {Computation of methodology\hyphen independent ionic solvation free
    energies from molecular simulations},
  subtitle = {I. The electrostatic potential in molecular liquids},
  journaltitle = jchph,
  volume = {124},
  eid = {124106},
  date = {2006},
  doi = {10.1063/1.2172593},
}
\end{filecontents}


\begin{document}
  \nocite{*}
  \printbibliography
\end{document}

Output:

enter image description here

As you can see the two entries are identical except for the url field, and if this field is defined, the doi field is not printed.