16

The root question here is how can one print a bibliography only when there are sources to be cited?

BACKGROUND
I'm working on expanding a small package (originally by Jackson Taylor and Becca Funke) that formats a paper according to the MLA 13 standard. (This format is commonly used in literature-based classes.)

The package currently provides basic formatting goodies like (something that looks like) a customized /maketitle and the proper running header (Last \thepage). I'm trying to make it a bit more functional/robust. This includes a growing list of features:

  • Natural use of \author
  • \date support
  • Optional title page and abstract
  • More robust bibliography handling
  • General customization of 'customizable' things, such as "Works Cited" / "References" / etc.

MEATY BACKGROUND
Currently, mla13 will print the bibliography whenever you tell it to. Since this is always at the end of a MLA paper, I've just used the appropriate commands to execute it at \end{document}. This little bit of automation has caused a problem -- how can the references be intelligently printed? That is, if there are no sources to be cited, it's a little awkward for there to be a blank page called 'Works Cited.'

MEAT
Using biblatex, is it possible to check and see if any source has been cited within the document? I've taken a look at the job.bbl file, and it seems that if one could check to see if the string \entry is not found in the file, this would suffice as a switch. I've checked into the primitives \read and \readline, but I honestly don't have much of a clue as to how to apply the information (I don't really understand it all that well, either).

Is the way I described possible, and if so, how? Is there a better way for this to be done?

2
  • 3
    Just use bibliography categories. The \printbibliography[category=<category name>,...] command doesn't print anything if the category is empty and the additional options in ... exclude the check and filter keys. Under this approach your question is duplicate of tex.stackexchange.com/q/6967/4483.
    – Audrey
    Jan 23, 2013 at 0:18
  • I'm not sure that's what I need though. There is content being generated by the command that is not part of \printbibliography at all (eg a new page). If you can give an MWE as an answer, I'll see if it works. (I'll still be trying it on my own time throughout the day though, but I've found that the presumption of 'I'm being dumb' is always a safe one.) Jan 23, 2013 at 9:19

1 Answer 1

11

The command

\printbibliography[heading=<heading>,...]

doesn't print anything when the bbl file contains no entries. If it's cited entries in the bbl file you want to consider, then use a category. The command

\printbibliography[heading=<heading>,category=<category>,...]

won't print anything provided that <category> is empty and the additional options settings in ... exclude check and filter. The example below considers the article document class. Its default bibliography heading doesn't issue a page break, but we can redefine it using \defbibheading. Default heading definitions for the standard, KOMA-script and memoir document classes can be found in biblatex.def.

\documentclass{article}
\usepackage[defernumbers]{biblatex}

% add every cited article to a category
% (nb: this excludes entries accessed via \nocite)
\DeclareBibliographyCategory{cited}
\AtEveryCitekey{\addtocategory{cited}{\thefield{entrykey}}}

% define bibliography heading that issues a page break
\defbibheading{bibliography}[\refname]{%
  \clearpage
  \section*{#1}%
  \markboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}}

\addbibresource{biblatex-examples.bib}
\begin{document}
Filler text.
% More filler text \parencite{companion}.
\printbibliography[heading=bibliography,title={Works cited},category=cited]
\end{document}

Use of \AtEveryCitekey prints a bibliography only for explicitly cited entries, excluding those accessed via \nocite. To consider all entries in the bbl file that would otherwise be printed use:

\makeatletter
\AtDataInput{\iftoggle{blx@skipbib}{}{\addtocategory{cited}{\thefield{entrykey}}}}
\makeatother

An empty \printbibliography generates a warning in the log. To avoid this you can use a boolean flag to indicate citations.

\documentclass{article}
\usepackage[defernumbers]{biblatex}

\newbool{anycited}
\AtEveryCitekey{%
  \ifbool{anycited}{}{\global\booltrue{anycited}}}

\newrobustcmd*{\printworkscited}{%
  \ifbool{anycited}{\printbibliography}{\printnothing}}
\def\printnothing[#1]{}

\defbibheading{bibliography}[\refname]{%
  \clearpage
  \section*{#1}%
  \markboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}}

\addbibresource{biblatex-examples.bib}
\begin{document}
Filler text.
% More filler text \parencite{companion}.
\printworkscited[heading=bibliography,title={Works cited}]
\end{document}

With this document, biber will issue a warning about no citations. There isn't much you can do about that. Hooking \printbibliography into \end{document} might seem convenient, but you are forcing a biber run onto your users. This wouldn't make much sense to me when writing an essay that doesn't use any bibliographic data. You can probably side-step some of these issues with BibTeX, at the cost of many biblatex features.

5
  • You raise a really good point about forcing the biber run. I've run into similar problems myself as a user, and it frustrates me to no end... (I've also forgotten about those countless little essays in middle/high school that used MLA without refs, as well.) sigh Jan 23, 2013 at 14:31
  • @vermiculus I'd probably just add a load-time option to the package that indicates whether or not the user intends to use bibliographic data.
    – Audrey
    Jan 23, 2013 at 15:45
  • Gotcha. Probably default to not include it automatically, correct? As to not have new users scratching heads? (Never taken on a project like this before -- going to be fun!) Jan 23, 2013 at 17:56
  • @vermiculus Up to you. Based on the option setting you can define commands differently. There are many talented package writers you can borrow from. (BTW: Sorry about all these edits. I just noticed a bad typo so I'm going to make one more...)
    – Audrey
    Jan 23, 2013 at 18:13
  • Don't even worry about it. I'll have to take a look into my own texmf and see what'll be handy. Jan 23, 2013 at 18:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .