[Tex/LaTex] How to sometimes suppress the printing of the URL field

apa-stylebiblatex

Alan's answer to a previous question gave me the idea to try something similar with a different field, but it's not working out quite like I expected.

Based upon this APA guidance (specifically, Chelsea wrote, "…only one piece of 'source information' is necessary per reference (the publisher name and location, the retrieval URL, or the DOI), so APA recommends you follow this practice."), I'd like to have the option of sometimes not printing the contents of the 'url' field for a book (or any reference type for that matter). But I don't want to delete the URL from my .bib file to accomplish that goal. I'd like to keep that data in my .bib file for other styles or just for my own use outside of LaTeX.

So following Alan's example, I added the following to my input file:

\def\PrintURL{PrintURL}
\DeclareFieldFormat[book]{url}{\iffieldequals{pubstate}{\PrintURL}{#1}{}}

I don't use the 'pubstate' field, so I thought I would use it as Alan used the 'type' field. From this code sample, I'm sure it will be obvious that I'm no programmer in any language, but I thought his example was perhaps something that I could generalize, thus my attempt above.

But as I said, it didn't work out like I thought it would. When I don't want the URL to show up (and have the 'pubstate' field blank), it does suppress the printing of the URL itself, but it still prints the, "Retrieved from" as if it were printing the URL. And when I do want to print the URL, having the string, "PrintURL" in the 'pubstate' field does cause the URL to print like I want, but it doesn't do proper line-wrapping with the URL.

I have a feeling this is a really stupid mistake on my part because I don't have even the basics of programming in a LaTeX input file. If anyone can point me to an introductory guide on this subject, I'd be very grateful.

And of course if anyone would like to point out my mistake here I'd be very grateful for that too.

PS. Thanks @matth for the comment. I think it's a great comment because reading it and experimenting with my input file in light of that question showed me that @PLK seems to have already implemented the solution (either that of @JosephWright or @lockstep or yet another unknown to me) to that question into the biblatex-apa style, and I never realized it before now. I say this because in my experimentation, I added a URL to one of my .bib entries that already had a DOI, and found that for such entries containing data in both 'url' and 'doi' fields, only the doi appears in my generated list of references, whereas if I remove the DOI from the 'doi' field, then the URL shows up. Very nice!!

However, for APA style compliance, the same kind-of solution should (I think, based only on Chelsea's comment and my limited understanding) also address the presence or absence of data in (one of? both of?) the 'publisher' and 'location' fields somehow. Because I think she said in that blog comment that only one of the three pieces of 'source information' ((1) publisher name and location, or (2) the retrieval URL, or (3) the DOI) should be present in each source entry of APA-style compliant reference lists.

What I'm finding, though, is that I get both (1) publisher/location and (2) DOI or URL (whichever has data in it) appearing in my reference lists, which is why I asked this question in the first place. In asking it, I never imagined that the logic to handle at least a part of this issue was already in place. Props (and my thanks) to @PLK for having anticipated this problem and already implemented a solution to that part of it.

Best Answer

This is also quite easy to do. I'm assuming that the doi and url fields trump the publisher and location field, so the way to implement this is to only print these fields if the doi and url fields are undefined. So we add the following:

\renewbibmacro*{location+publisher}{%
  \iffieldundef{doi}{
     \iffieldundef{url}{
         \printlist{location}%
         \setunit*{\addcolon\space}%
         \printlist{publisher}%
         \newunit}{}}{}
  }

I must say that I find this an odd rule on the APA's part: publisher and location are usually only printed with books, and in my opinion the publisher is an important piece of information that has very little to do with "source" information, and more to do with e.g. providing information about the reliability of the source. If a book is published by a major university press that fact tells me a lot compared to if the publisher is "Joe's Spiralbound Book Barn". Somehow just having a doi doesn't help with that.

Here's a sample document with three bib entries with different amounts of source information included.

\documentclass[endnotes]{apa6e}
\usepackage[american]{babel}
\usepackage{csquotes} 
\usepackage[style=apa]{biblatex} 
\DeclareLanguageMapping{american}{american-apa}
\title{A title}
\author{An Author}
\shorttitle{A title}
\authornote{}
\abstract{An abstract}

\begin{filecontents}{\jobname.bib}
@book{Chomsky1981,
    Address = {Dordrecht},
    Author = {Noam Chomsky},
    Booktitle = {Lectures on Government and Binding},
    Publisher = {Foris Publications},
    Title = {Lectures on Government and Binding},
    Url = {http://url.url.url},
    Year = {1981}}

@book{Chomsky1986,
    Address = {Cambridge, MA},
    Author = {Chomsky, Noam},
    Booktitle = {Barriers},
    Doi = {http://doi.doi.doi},
    Publisher = {{MIT} Press},
    Title = {Barriers},
    Url = {http://url.url.url},
    Year = {1986}}

@book{Chomsky1982,
    Address = {Cambridge, MA},
    Author = {Noam Chomsky},
    Booktitle = {Some Concepts and Consequences of the Theory of Government and Binding},
    Publisher = {{MIT} Press},
    Title = {Some Concepts and Consequences of the Theory of Government and Binding},
    Year = {1982}}

\end{filecontents}
\addbibresource{\jobname.bib}
\renewbibmacro*{location+publisher}{%
  \iffieldundef{doi}{
     \iffieldundef{url}{
         \printlist{location}%
         \setunit*{\addcolon\space}%
         \printlist{publisher}%
         \newunit}{}}{}
  }

\begin{document}
\begin{itemize}
\item An entry with Publisher and URL:  \cite{chomsky1981}
\item An entry with only Publisher. \cite{chomsky1982}
\item An entry with Publisher, DOI and URL: \cite{chomsky1986}
\end{itemize}

\urlstyle{rm}
\printbibliography
\end{document}

output of code