In today's installment we discuss how to add a bibliography file to $\mathrm{\LaTeX}$, and how to use the \cite{} command.

There are essentially two main ways to deal with citations in $\mathrm{\LaTeX}$. One can either:

• List all the references at the end of the document.
• Create a separate bibliography file (which you then use for all your documents).

The former becomes less efficient as soon as have written more than one document in $\mathrm{\LaTeX}$ (since it requires you to duplicate your references). As you all will surely end up writing (at least) 2 documents (eg. your BSc thesis and your MSc thesis), in these notes we will solely focus on the second option: creating a bibliography file.

##### Caveat Emptor

Before we get started, a little warning. There are two competing packages that allow you to work with bibliographies in $\mathrm{\LaTeX}$, and they have similar names:

1. bibtex.
2. biblatex.

Of the two, bibtex is the older[1] and more well-known. It is also (IMHO) inferior in practically every possible way—it is messier, less extensible, and harder to configure (particularly with respect to non-English languages). Meanwhile biblatex is a modern[2], actively maintained, and more sophisticated package.

Thus we will exclusively focus on the biblatex package[3]. If you prefer to use bibtex (for instance, because you have collaborators who stubbornly refuse to switch[4]), then start here.

Remarks:

• A potential disadvantage of biblatex over bibtex is that some journals refuse to accept submissions compiled with biblatex. This is unlikely to be an issue for most of you[5]. However if you are worried: it is relatively easy to switch back and forth (see the next bullet point), and in any case more and more journals support biblatex these days.
• If you already have a .bib file (more on this below) that you use with bibtex, then it only requires minimal effort to convert it to the biblatex format. For instance, the reference manager JabRef has a “Cleanup Entries” tool, which can perform the conversion for you. See the documentation here.

## Anatomy of a Bibliography

Bibliographies in $\mathrm{\LaTeX}$ work somewhat differently to anything we have discussed so far. The bibliography itself is a separate file, which is saved with a .bib extension (rather than a .tex extension). The advantage of this is that the same bibliography file can be reused in multiple documents. In fact, I encourage you to have a single “master” bibliography file that you use in all your $\mathrm{\LaTeX}$ files[6]. This saves you from having to enter the information for any given reference more than once (which is good, since adding reference data is rather time-consuming).

Not every reference listed in the bibliography file gets printed in the $\mathrm{\LaTeX}$ file. Roughly speaking, it works like this:

• Suppose your “master” .bib file contains 100 different references.
• Your BSc thesis might only use 15 of these references. To quote a reference, you use the \cite{name-of-reference} command inside your $\mathrm{\LaTeX}$ file.
• At the end of your $\mathrm{\LaTeX}$ file, you use the \printbibliography command. This instructs $\mathrm{\LaTeX}$ to search through the master .bib file and pick out all the references that were actually used in the current document (via the \cite{} command), and then print them in a list.

1. Older than me! ↩︎

2. Well, nothing about LaTeX is truly “modern”... So perhaps “less ancient” would be more accurate. ↩︎

3. Pro tip: The easy way to remember which one is better: LaTeX ≥ TeX, and thus also biblatexbibtex. ↩︎

4. Yes, R*****n, I am talking to you! ↩︎

5. Because, let's face it, you are unlikely to publish your Bachelors thesis in a journal. ↩︎

6. You can even share a single .bib file with your friends! ↩︎

### Getting Started

Let's see how this works in practice. If you are using Overleaf, to add a bibliography file you simply create a new file inside the project tree with a .bib extension. Alternatively, if you already have a bibliography file, then you upload it to your project. It should look something like this:

If you are using $\mathrm{\TeX}$ on your computer, then it is a bit more complicated. You need to tell your editor to use the biber backend. If for instance you are using the TeXShop, go to Preferences > Engine and change the default BibTeX engine to biber.  For other editors, see this post for detailed instructions.

Here is a very simple of a .bib file, with precisely one entry:

@book
{
harrypotter,
author     = {J. K. Rowling},
title      = {Harry Potter and the Philosopher's Stone},
date       = {1997},
publisher  = {Bloomsbury},
isbn       = {0-7475-3269-9},
}

And here is a $\mathrm{\LaTeX}$ file that uses this bibliographic information.

\documentclass{article}
\usepackage{biblatex}
\begin{document}
We refer the reader to \cite{harrypotter} for an informal introduction to Algebraic Number Theory.
\printbibliography
\end{document}


The salient points are:

• We invoked the biblatex package via the now familiar \usepackage{biblatex} command in the preamble.
• We then told $\mathrm{\LaTeX}$ to load the file sample.bib. This file should be in the same directory as the .tex file—if you are using Overleaf then it should be as pictured above.
• To cite an entry from the bibliography, we use the command \cite{internal-name}, where internal-name is the “internal name” for that particular entry (more on this shortly).
• Just before the \end{document} line, we added the \printbibliography command.

Now let us turn back to the file sample.bib again:

@book
{
harrypotter,
author     = {J. K. Rowling},
title      = {Harry Potter and the Philosopher's Stone},
date       = {1997},
publisher  = {Bloomsbury},
isbn       = {0-7475-3269-9},
}
• The first line begins with @book. This tells $\mathrm{\LaTeX}$ that this entry is a book. If instead we were referencing a journal article, we would use @article instead. A table of all the common options is below.
• The rest of the entry is wrapped in a pair of braces { ... }.
• The next line of text contains the “internal name” for this reference. The comma at the end is important![1] Just as with cross-references, this can be anything you want, and it will not get printed. It is generally useful to choose an internal name that helps you remember which reference you are quoting—particularly if your .bib file ends up with hundreds of entries. The internal name must be unique.

The next five lines contain the actual bibliographic information. The syntax is mostly self-explanatory. The important points to remember are:

• You must use an = key to separate the property name (eg. author) from the value the property should take (eg. {J. K. Rowling}).
• You must wrap the property value inside a pair of braces: thus it is {J. K. Rowling}, and not J. K. Rowling.[2]
• You must end each line with a comma.
• If there are multiple authors, you must separate each one with the word and.

Other than that, the order of the properties and the spacing is completely irrelevant. As far as $\mathrm{\LaTeX}$ is concerned, the following file is identical to the previous one:

1. FACT: 95% of all biblatex errors come from forgetting commas. ↩︎

2. Actually instead of braces { ... } you could also use quote marks " ... " here. I prefer braces however, since selecting the wrong type of quote mark can lead to errors: " is not the same key as ”! ↩︎

@book{
harrypotter,
publisher  = {Bloomsbury},

author= {J. K. Rowling},
date  = {1997},
title      = {Harry Potter and the Philosopher's Stone},  isbn      =           {0-7475-3269-9},}

You can even put everything on one line if you want:

@book{harrypotter,publisher={Bloomsbury},isbn={0-7475-3269-9},author={J. K. Rowling},date={1997},title={Harry Potter and the Philosopher's Stone}}

Therefore the only reason to line things up neatly is so that they are easier to read. If you use a reference manager (more on this shortly) then it will typically do this automatically for you.

In our example we had five properties: title, author, date, publisher, isbn. Of these the first three are required properties, and the last two are optional.

The date field is usually just the year. If you want to be more precise, use the YYYY-MM-DD format.

As mentioned above, if there are multiple authors you must always use the word and to separate them. How the authors are actually printed depends on the bibliography style chosen—for example, if an article has four or more authors, some styles will print only the name of the first author, and then “et. al.

Moreover depending on which bibliography style you choose, some properties may not get printed at all. For example, another property you can use is keywords. Most bibliography styles (including the default one we used above) will not print these out.

Here is a table summarising the standard reference types, and some of the common properties that go with them:

Reference type What it's used for Required properties Common optional properties
article An article in a journal. author, title, journaltitle, date volume, number, pages, doi
book A book. author, title, date series, publisher, volume, isbn, edition
thesis A thesis. author, title, type, institution, date url
online An online resource. author/editor, title, date, url urldate
collection A book with multiple contributions by different people. Has an editor. editor, title, date series, publisher, volume, isbn, edition
incollection A single chapter in a collection. author, title, booktitle, date series, publisher, volume, isbn, edition
proceedings Proceedings from a conference. title, date eventtitle, editor, location/venue
inproceedings A single paper in a conference proceedings. author, title, booktitle, date eventtitle, editor, location/venue
unpublished Something that has not been published. author, title, date url

This is not remotely an exhaustive list, and we refer you to the official  documentation for more details (see §2.2.1).

##### Reference Manager Software

Curating a large .bib file takes a long time. The (free) reference manager JabRef is quite good, and supports biblatex. There are other options too, such as BibDesk (which is macOS only). That said, I personally prefer to work with the .bib file directly, using some helpful shortcuts and scripts to speed up the process. I'll discuss my approach further in the last of these $\mathrm{\LaTeX}$ tutorials.

Here is a slightly more interesting sample bibliography file—called sample2.bib—with four entries, which we will use in our examples for the rest of the lecture.

@article
{
wiles,
author        = {Andrew John Wiles},
title         = {Modular elliptic curves and Fermat's Last Theorem},
date          = {1995},
journaltitle  = {Annals of Mathematics},
volume        = {141},
pages         = {443--551},
}

@book
{
harrypotter,
author       = {J. K. Rowling},
title        = {Harry Potter and the Philosopher's Stone},
date         = {1997},
publisher    = {Bloomsbury},
isbn         = {0-7475-3269-9},
}

@online
{
xkcd,
author       = {Randall Munroe},
date         = {2014-08-04},
title        = {Thesis Defense},
url          = {https://xkcd.com/1403/},
}

@online
{
solutions,
author      = {Will J. Merry},
title       = {Solutions to the December 2019 Exam},
date        = {2019-11-29},
url         = {https://mry.re/solutions},
}

Here is an example of a $\mathrm{\LaTeX}$ file that uses this bibliography file:

\documentclass{article}
\usepackage{biblatex}
\begin{document}
The paper \cite{wiles} is quite tricky to understand, and we refer the reader to \cite{harrypotter} for an informal introduction to Algebraic Number Theory.
\printbibliography
\end{document}

Note we only used two of the four entries of sample2.bib.

Let us now look at ways to customise the appearance of the printed bibliography file. The important point to understand is that these changes are all done in the $\mathrm{\LaTeX}$ file, and not in the .bib file. This is why you can use the same .bib file for multiple documents.

\documentclass{article}
\usepackage[style=alphabetic]{biblatex}
\begin{document}
The paper \cite{wiles} is quite tricky to understand, and we refer the reader to \cite{harrypotter} for an informal introduction to Algebraic Number Theory.
\printbibliography[title=Sources]
\end{document}


• We added the option style=alphabetic when calling the package biblatex. Remember that the general syntax is \usepackage[options]{name}.  This changed the appearance of the references from the default numbers [1], [2], etc, to include the first three letters of the author's surname, and the year.
• We changed the default name References to Sources by adding the option title=Sources to the \printbibliography command at the end.

There are many different styles you can choose (see §3.3.2 of the official documentation), but I recommend you stick to either the default one (which is also called numeric) or the alphabetic one displayed above, since these are the most commonly used in mathematics.

You can also customise the order that the bibliography entries appear in. By default, the entries are sorted by name, then title, then year. If instead you wanted them to be sorted first by year, then name, then title, add the option [sorting=ynt].

\documentclass{article}
\usepackage[style=alphabetic,sorting=ynt]{biblatex}
\begin{document}
The paper \cite{wiles} is quite tricky to understand, and we refer the reader to \cite{harrypotter} for an informal introduction to Algebraic Number Theory.
\printbibliography
\end{document}

Note how this time [Wil95] appeared before [Row97] in the References section.

Some common options for sorting include:

Sorting option What it does
nyt Sorts by name, year, then title. (This is the default.)
ynt Sorts by year, name, then title.
ydnt Sorts by year (depending), name, then title.
nyvt Sorts by name, year, volume then title.
none No sorting. Entries appear in the order they are referenced.

A full list can be found in Appendix C of the official documentation.

Often you want to explicitly reference a particular section of a given paper. This is done by modifying the \cite{} command by inserting a pair of square brackets like so: \cite[Theorem 1]{reference-name}.

\documentclass{article}
\usepackage{biblatex}
\begin{document}
The paper \cite{wiles} is quite tricky to understand. Many readers are no doubt already familiar with \textit{Fermat's Last Theorem} (stated here as \cite[Theorem 0.5]{wiles}). However the lynchpin of Wiles's proof is \cite[Theorem 0.4]{wiles}, which is concerned with modularity of semistable elliptic curves over the rational numbers. We expect this to be a somewhat less familiar subject to most readers.

To this end, we recommend the monograph \cite{harrypotter} for an informal introduction to Algebraic Number Theory. See \cite[Chapter 11: Quidditch]{harrypotter} in particular.
\printbibliography
\end{document}


Finally, it is sometimes nice to split up the bibliography into multiple sections. For example, you might want to list all the published papers in one section, and all the preprints in another. This is easily done by modifying the \printbibliography command.

Consider the following snippet.

\printbibheading
\printbibliography[options-2,heading=subbibliography,title=name-2]

The first line \printbibheading tells $\mathrm{\LaTeX}$ to print out References like usual. The next line tells $\mathrm{\LaTeX}$ to create a sub-bibliography with the title Name-1, and then print out all the references that satisfy the conditions specified by options-1. The third line does this again for a second sub-bibliography.

This is easiest to understand with an example:

\documentclass{article}
\usepackage[style=alphabetic]{biblatex}
\begin{document}
\section{Introduction}
The paper \cite{wiles} is quite tricky to understand, and we refer the reader to \cite{harrypotter} for an informal introduction to Algebraic Number Theory.
\section{Concluding Remarks}
If you are worried about presenting your thesis, we recommend you follow Randall Munroe's excellent advice in \cite{xkcd}. If instead you are worried about the final exam, we recommend \cite{solutions}, where you can find the solutions to all the Winter 2019 exams in advance.
\end{document}


Here the first sub-bibliography selected all references that were not online sources using the syntax nottype=online and the second sub-bibliography selected those references which were online sources. Again, many more options are available to you—see the official documentation for more details.

Alternatively, perhaps you'd like to have one bibliography at the end of each section. This can be achieved by adding the option refsection=section, as the following example demonstrates.

\documentclass{article}
\usepackage[refsection=section]{biblatex}
\end{document}