In academia one of the most important points in any work that you want to have published is citing other works that you have used to reach your conclusions, or works whose conclusions that you have sought to disprove in your work. Doing this (hopefully) confers credibility to your work as well. As with most other things in academia, there is an approach to cite and reference works that you use. Namely, the approach is that pretty much everyone is doing their own thing. This means that the same work may have to be formatted radically different depending on whether you are publishing your work with publisher A, B, or C, nevermind it is, in fact, the same work you are citing.

To most people, this situation is, of course, untenable, and since there usually are rules from each publisher that describe how references should be written down, it is almost apparent that you should be able to use some kind of software that automates this process for you. In fact, there are many software packages doing exactly this, with EndNote and RefWorks being some of the more well-known commercial packages if you are a Microsoft Word user. Fortunately, we are not! So for us the main contender to do bibliographies with LaTeX is BibTeX (there are several other tools, but BibTeX is the most often-used tool for this purpose, so we will focus on that).

Before we take a look at BibTeX, let us, for good measure and for those not too corrupted by academia yet, do a quickly summarise some of the different ways to create a bibliography and the corresponding ways to cite this work inside the text. Our example work will be the book Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, published by Addison-Wesley in 1995 with an ISBN 0-201-63361-2. Each pair of lines in the image below will show how it is (or can be) referenced inside the text, and the other line will show how it is listed in the bibliography—the list at the end of your work that gives a thorough list of the works you have cited in your text.

Chicago Manual of Style, 13th Edition style:
(Gamma, Helm, Johnson, and Vlissides 1995)
Gamma, E., R. Helm, R. Johnson, and J. Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley.

Plain LaTeX style:
[1]
[1] Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995.

Alpha LaTeX style:
[GHJV95]
[GHJV95] Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995.

Abbreviated LaTeX style:
[1]
[1] E. Gamma, R. Helm, R. Johnson, and J. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995.

And there are many, many, many more variants than these to choose from, but it gets a bit tiresome to write them all out. To the first of these (the Chicago Manual of Style styled citation) there is of course also an alternative contextual representation that goes like this: ‘Gamma, Helm, Johnson, and Vlissides (1995)’ so that you can use it as part of a normal sentence. And there are more rules governing these things when you add more information to the citation, like a page number/range or a chapter. Verily it is much better to let a program take care of all these technicalities than have to remember them yourself.

Before we move on to BibTeX and how it works, it might be prudent to take a quick look at how we can create bibliographies manually in LaTeX, as BibTeX actually just automates creating bibliographies for us.

The environment for a bibliography in LaTeX is simply called thebibliography. It works very much like an enumerate environment, but with a few more bells and whistles and some slightly different command names. To see how it works, let us just look at a full example up-front:

\begin{thebibliography}{1} \bibitem{DP} Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. \textit{Design Patterns: Elements of Reusable Object-Oriented Software}. Addison-Wesley, 1995. \end{thebibliography}

So let us cover this one step at a time. The {1} after the thebibliography is the visual key of the widest element in the bibliography list. Since we only have a single entry and we're creating a bibliography after the plain LaTeX style above, this is 1. If we were using the alpha style, it would have been {GHJV95} instead. Instead of \item we have \bibitem and it takes a key that you will be using to refer to that work when you cite something. Citing is done with the \cite command, so to cite the Design Patterns book, you would write \cite{DP}. The rest of the text is merely a standard list text and nothing fancy happens to that. We will cover the \cite command in more detail later on.

With this in mind, it is time to move on to BibTeX. BibTeX uses a bibliography file that is provided by the user with a .bib suffix, a style file that ends in a .bst suffix, this file is typically chosen from the large set of existing style files, and finally the actual BibTeX program that understands the .bib file format and the .bst file format and processes them together to create a .bbl file. On a subsequent run, LaTeX reads in the .bbl file, which contains a finished thebibliography environment. This may seem convoluted, but remember that TeX is an old program from long before we measured system resources in gigabytes, so this was the only really tenable solution for integrating new tools back then.

Let us start at the .bib file format and take a look at how this is organised.

@worktype{reference-key, key_1 = value_1, key_2 = value_2, ... key_n = value_n } ...

Here the worktype specifies the type of work, for example article, phdthesis, inproceedings, and many other types. It is the .bst file that defines what worktypes are supported. Likewise the keys are dependent on the worktype and are also defined by the .bst file. Normally a lot of these worktypes and keys will be the same across different .bst files, so it is quite normal that all of them has support for an article and support for keys such as author, title, publisher, year, month, isbn, etc. The values are, on the other hand, are a lot less well-defined and there are several ways to write them. For our initial purposes, though, it will most likely be sufficient to write values like this: {value test}. So to get a feeling of how this all will work out for our book reference above, let us write it up in BibTeX:

@book{DP, author = {Erich Gamma and Richard Helm and Ralph Johnson and John Vlissides}, title = {Design Patterns: Elements of Reusable Object-Oriented Software}, publisher = {Addison-Wesley}, year = {1995}, isbn = {0-201-63361-2} }

Authors here are merely delimited with the keyword and, which allows BibTeX to automatically figure out the things it needs about the authors. Do note that BibTeX has some weaknesses in guessing where to break some names, like Domonique Galouzeau de Villepin, whose first name is Dominique and last name is Galouzeau de Villepin. If entered as ‘Dominique Galouzeau de Villepin’ in BibTeX as-is, it would see ‘Dominique Galouzeau’ as first name, ‘de’ as a delimiter for the last name and ‘Villepin’ as the last name. To disambiguate this you can also enter names in BibTeX like this: ‘Galouzeau de Villepin, Dominique’. For many more details on the finer trickeries of BibTeX you may want to refer to Xavier Décoret's excellent summary.

For now, let us return to the problem at hand: using this .bib file with our text in LaTeX. As before, DP is the key of the work, so inside our LaTeX text we can just refer to the work using the command \cite{DP}. So what do we use to hook us up to the .bib and .bst files? This is, in fact, very easy, it just requires two commands:

\bibliographystyle{plain} \bibliography{myfile}

So the argument for \bibliographystyle is the .bst file without the .bst suffix, and the argument for \bibliography is the name of your .bib file, likewise without the .bib suffix. If we presume that you use pdflatex to compile your LaTeX documents to PDF, this is a typical run of what you would do when you use BibTeX:

  1. pdflatex mytexfile.tex

    This generates mytexfile.aux with special bibliography commands that BibTeX will process.

  2. bibtex mytexfile

    This is done without any suffix to mytexfile. What it does is that it reads the generated .aux file for bibliography commands in order to figure out what .bst and .bib files to use. Once it finds these, it generates a .bbl file with a finished thebibliography environment.

  3. pdflatex mytexfile.tex

    This includes the contents of the .bbl file at the location where you have the \bibliography command and writes citation location information to the .aux file.

  4. pdflatex mytexfile.tex

    Like the previous step, except now the citation location informations are available in the .aux file, so the \cite commands can be translated to an actual reference with the relevant text.

In some rarer cases you may need another run of pdflatex to sort out some kinds of references, but the TeX engine will generally let you know if it needs to be run again to resolve references. There is, of course, no real need to understand all these technicalities. In practice you will just run pdflatex once, then bibtex and then pdflatex a number of times until LaTeX stops complaining about references. The important thing is that you can now just replace \bibliographystyle{plain} with \bibliographystyle{alpha} if that is what suits your fancy, and run pdflatex and bibtex again as above. Do note that in some cases you may need to delete the .aux and .bbl files in order to be able to compile again after you have changed to a new bibliography style, as some bibliography styles use special-purpose commands that may be removed by your changes. This will, however, usually not be a problem in practice unless you vacillate endlessly between bibliography styles, but who does that, really? (The author looks around suspiciously).

Citing works is fairly easy by just doing \cite{work}, but what if we want to indicate that we are only really referencing a single page or chapter in the work. We could of course write something like: ‘See chapter 1 in \cite{work}’, but unfortunately for us, there are also guidelines governing how to write these things in relations to citations by the different publishers, so we will need to come up with another approach. Fortunately, \cite takes an optional argument that tells us exactly this: \cite[info]{work}. So, for instance, referring to page 120 of Design Patterns would be written like this: \cite[p.~120]{DP}. Note the non-breaking space, ~, which makes sure that there isn't a line break between the ‘p.’ and the ‘120’, which would look fairly ugly in most cases and disrupt the general flow of reading. It is also customary to place a non-breaking space before the \cite command if it isn't the first thing in a sentence, e.g.: ‘... as described in~\cite[p.~120]{DP}’.

In order to get really going with using BibTeX now, you only really need to know the standard worktypes and keys. Fortunately, Wikipedia provides a nice, if short, overview of them here.

That's basically what there is to using BibTeX for most purposes. Hopefully I will find the time to write something on creating custom .bst files later on, as those are fairly interesting as well.