Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question
1  
I asked a very similar question a couple of years ago, and got some excellent answers: tex.stackexchange.com/questions/5660/… –  Peter LeFanu Lumsdaine Jan 19 at 17:30

2 Answers 2

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.

share|improve this answer

You can do this via mapping of datasources. The \DeclareSourcemap command

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[fieldsource=url,final]
      \step[fieldset=doi,null]
    }  
  }
}

processes any bibtex input data and for each item in the bibliograhpy file it executes a number of commands given by \step:

  • the first step fieldsource checks the url field

    • the instruction final means processing of the current entry is terminated if this field is empty or does not exist
  • otherwise the next step fieldset=doi,null is executed; this clears the doi field, in fact it makes it look as if there was never a doi field in the input.

The result of this is that all biber/biblatex sees is an entry with either a url field or a doi field, but not both.

This has the advantage that you don't need to know how the particular biblatex style chooses to process these fields.

Sample output

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

\addbibresource{\jobname.bib}

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[fieldsource=url,final]
      \step[fieldset=doi,null]
    }  
  }
}

\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@Article{Akbulut-S:deformations-G2,
  author =   {Akbulut, Selman and Salur, Sema},
  title =    {Deformations in {$G_2$} manifolds},
  journal =  {Adv. Math.},
  volume =   217,
  year =     2008,
  number =   5,
  pages =    {2130--2140},
  issn =     {0001-8708},
  doi =      {10.1016/j.aim.2007.09.009},
  url =      {http://dx.doi.org/10.1016/j.aim.2007.09.009}
}
@Article{Akbulut-S:deformations-G22,
  author =   {Akbulut, Selman and Salur, Sema},
  title =    {Deformations in {$G_2$} manifolds},
  journal =  {Adv. Math.},
  volume =   217,
  year =     2008,
  number =   5,
  pages =    {2130--2140},
  issn =     {0001-8708},
  url =      {http://dx.doi.org/10.1016/j.aim.2007.09.009}
}
@Article{Akbulut-S:deformations-G23,
  author =   {Akbulut, Selman and Salur, Sema},
  title =    {Deformations in {$G_2$} manifolds},
  journal =  {Adv. Math.},
  volume =   217,
  year =     2008,
  number =   5,
  pages =    {2130--2140},
  issn =     {0001-8708},
  doi =      {10.1016/j.aim.2007.09.009},
}

\end{filecontents}

\begin{document}
  \nocite{*}
  \printbibliography
\end{document}
share|improve this answer
1  
@Dror Thanks for asking. Is it clearer now? –  Andrew Swann Jan 19 at 16:48
1  
Almost ;) If in the first \step url does not exist, then the entry is skipped, the next \step is not executed and the \map ends. Next, the \map is started again for the next entry (if one exists). Is this correct? How do you understand this from the manual? –  Dror Jan 19 at 16:53
    
@Dror Your interpretation is correct. Most of my understand is from the discussion of the \step command in the manual. It is not the most lucid, but there are a number of examples given afterwards that can help - however there are almost too many examples. A little experimenting with small examples is also effective. –  Andrew Swann Jan 19 at 16:57
    
Thank you Andrew, this is such an excellent and elegant solution. I've been looking for hours for this and didn't dare to try the 100-lines scripts provided elsewhere. This one is a beauty! –  noshky Aug 15 at 19:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.