# Print url only if doi not present

this question inspired me to make the printing of the Url and the Eprint field conditional, so that it only prints if there's no Doi field. Any pointers would be appreciated,

If I use the code posted below, I would like it to remove what I've striked out with red here,

\documentclass{article}

\usepackage[backend=bibtex, style=authoryear-comp, natbib=true]{biblatex} %for digital version
\bibliography{\jobname}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@article{Holland1986,
Author = {Paul W. Holland},
Doi = {10.1080/01621459.1986.10478354},
Eprint = {http://www.tandfonline.com/doi/pdf/10.1080/01621459.1986.10478354},
Journal = {Journal of the American Statistical Association},
Keywords = {Applied_Economics},
Number = {396},
Pages = {945-960},
Title = {Statistics and Causal Inference},
Url = {http://www.tandfonline.com/doi/abs/10.1080/01621459.1986.10478354},
Volume = {81},
Year = {1986}}
@article{Heckman1990,
Author = {James Heckman},
Issn = {00028282},
Journal = {The American Economic Review},
Keywords = {_MSc},
Number = {2},
Pages = {313-318},
Publisher = {American Economic Association},
Title = {Varieties of Selection Bias},
Url = {http://www.jstor.org/stable/2006591},
Volume = {80},
Year = {1990}}
\end{filecontents}

\begin{document}

\citet{Holland1986} and \citet{Heckman1990} went for a swim.
\printbibliography
\end{document}

• The inverse problem is over at tex.stackexchange.com/q/154864/35864 – moewe Apr 4 '18 at 12:17
• I don't think Eprint = {http://www.tandfonline.com/doi/pdf/10.1080/01621459.1986.10478354}, is an eprint field. eprint is normally used for online repositories such as arXiv, JSTOR, Google Books where the URL can be shortened using some kind of unique identifier. – moewe Apr 4 '18 at 12:48
• For example you may replace Url = {http://www.jstor.org/stable/2006591}, with eprint = {2006591}, eprinttype = {jstor}, – moewe Apr 4 '18 at 12:49
• @moewe, thanks for linking that thread. I did see it, but I'm not really any good at writing functions in LaTeX/TeX. What do you use as the authoritative source in regard to what bibtex fields is meant for what? Like what is actually suppose to go in eprint and what about translators etc? Thanks. – Eric Fail Apr 5 '18 at 13:56
• The biblatex documentation has a short explanation about what each field does and what it means. The file biblatex-examples.bib contains good example usages of many fields. – moewe Apr 5 '18 at 13:58

To conditionally print the URL you could use the following:

\documentclass{article}

\usepackage[backend=bibtex, style=authoryear-comp, natbib=true,url=false]{biblatex} %for digital version
\bibliography{\jobname}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@article{Holland1986,
Author = {Paul W. Holland},
Doi = {10.1080/01621459.1986.10478354},
Journal = {Journal of the American Statistical Association},
Keywords = {Applied_Economics},
Number = {396},
Pages = {945-960},
Title = {Statistics and Causal Inference},
Url = {http://www.tandfonline.com/doi/abs/10.1080/01621459.1986.10478354},
Volume = {81},
Year = {1986}}
@article{Heckman1990,
Author = {James Heckman},
Issn = {00028282},
Journal = {The American Economic Review},
Keywords = {_MSc},
Number = {2},
Pages = {313-318},
Publisher = {American Economic Association},
Title = {Varieties of Selection Bias},
Url = {http://www.jstor.org/stable/2006591},
Volume = {80},
Year = {1990}}
\end{filecontents}

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

\begin{document}

\citet{Holland1986} and \citet{Heckman1990} went for a swim.
\printbibliography
\end{document}


• You may want to protect a few line-ends there with % to avoid unwanted space. You could also add an answer using soucemaps similar to tex.stackexchange.com/a/154875/35864 – moewe Apr 4 '18 at 12:17
• @moewe Thanks for your suggestion about the %! I'll leave the the sourcemap for you, our biblatex expert! – samcarter_is_at_topanswers.xyz Apr 4 '18 at 12:30

Your MWE uses BibTeX, but if you are willing to switch to Biber (you really should look into it, Biber offers more features and BibTeX is officially considered a legacy backend now), you can use Biber's sourcemap feature.

\documentclass{article}

\usepackage[backend=biber, style=authoryear-comp]{biblatex}

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

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@article{Holland1986,
Author = {Paul W. Holland},
Doi = {10.1080/01621459.1986.10478354},
Eprint = {http://www.tandfonline.com/doi/pdf/10.1080/01621459.1986.10478354},
Journal = {Journal of the American Statistical Association},
Keywords = {Applied_Economics},
Number = {396},
Pages = {945-960},
Title = {Statistics and Causal Inference},
Url = {http://www.tandfonline.com/doi/abs/10.1080/01621459.1986.10478354},
Volume = {81},
Year = {1986}}
@article{Heckman1990,
Author = {James Heckman},
Issn = {00028282},
Journal = {The American Economic Review},
Keywords = {_MSc},
Number = {2},
Pages = {313-318},
Publisher = {American Economic Association},
Title = {Varieties of Selection Bias},
Url = {http://www.jstor.org/stable/2006591},
Volume = {80},
Year = {1990}}
\end{filecontents}

\begin{document}

• @EricFail See §3.15 Using the fallback BibTeX backend of the biblatex docs: "To utilise all of the features described here, biblatex must be used with the Biber program as a backend. Indeed, the documentation in general assumes this. However, for a limited subset of use cases it is possible to use the long-established BibTeX program [...] as the supporting backend. This works in much the same way as for Biber with the only proviso being that BibTeX is much more limited as a backend." – moewe Apr 5 '18 at 14:02