1. Overview

In this tutorial, we’ll discuss how to sort a bibliography by order of appearance in a LaTeX document.

We’ll start by defining BibTeX and what it brought to the LaTeX environment. Next, we’ll outline the major types of bibliography styles and how to apply them. Then, we’ll identify ways to sort the references by order of appearance regarding the bibliography style in use. At the end of this tutorial, we’ll be able to display the bibliography in our LaTeX document by order of appearance while using the best-suited style for us.

2. LaTeX and BibTeX

LaTeX is a software program for document preparation that operates with a compilation process and an input (a source file with a .tex extension). It is a typesetting tool that accepts a plain text file containing a set of instructions and converts it to a formatted document before receiving a pdf file. Generally, it is used for writing a paper. However, it can also be used for making presentations or drawing a graph. The software produces a .pdf file, displayable on screen, and a .ps file, printable on machine:

 

latex compilation

Generally, when writing a LaTeX document, we’ll need to reference the work of other persons in our document. For this purpose, we present BibTeX as a bibliographic tool that works with LaTeX to help structure our citations and build a bibliography. To do so, we need to create a separate file having a .bib extension. It is where all the references are stored in a specific format. Each entry in the BibTeX file owns a unique key used to display it in the source .tex file:

Bibtex reference

Now, we should mention that styling or formatting the bibliography is an important aspect we need to consider when sorting the references in a LaTeX document. And doing it manually is far away from being practical. In this context, using BibTeX along with LaTeX is a very attractive solution. We shall mention that the choice of the bibliography style affects, in many cases, the sorting process. That is why it is important to have an overview of bibliography styling before sorting.

In the following, we’ll see how the BibTeX system brings in several tools to manage the style of citations and sort them by order of appearance in an effective way that avoids any potential conflict between bibliography styling and sorting.

3. Bibliography Styling with LaTeX

Let’s say we have downloaded a conference LaTeX template to write a paper. We’ll find these typical files extensions:

  • An example of an input file: document.tex
  • A style document file: style.cls
  • A style bibliography file: styleBiblio.bst

As we have mentioned earlier, we need to create a document file within the project folder that contains the bibliography sources, say a bibFile.bib. Then, we tend to choose the bibliography style, which represents the format of two major parts of the document. These two parts stand for the abbreviation citation in the main text content (document.tex) and the corresponding entry in the list of references (bibFile.bib). The latter admits various styles applied in multiple scientific fields. They can be loosely divided into a numerical referencing system (Vancouver) and a textual referencing system (Harvard).

How we style our bibliography always depends on the document type we’re writing and the template we’re using. For example, if we’re writing an academic/scientific paper, we’ll notice that each conference, journal, or institution has its own rules and style for bibliography.

In the following, we’ll present two main packages provided by LaTeX to style references, the Natbib, and the BibLaTeX packages.

3.1. Styling with Natbib Package

Natbib is a bibliography management package within LaTeX that allows us to customize citations. It supplies a pretty complete set of formats. To implement the package, we first need to insert the references entries in the bibFile.bib file. We’ll use three references in our .bib file to show the implementation results:

% ----- the bibFile.bib file -----

@article{einstein,
  author =       "Albert Einstein",
  title =        "On the electrodynamics of moving bodies",
  journal =      "Annalen der Physik",
  volume =       "322",
  number =       "10",
  pages =        "891--921",
  year =         "1905"
}

@book{latexcompanion,
    author    = "Michel Goossens and Frank Mittelbach and Alexander Samarin",
    title     = "The \LaTeX\ Companion",
    year      = "1993",
    publisher = "Addison-Wesley",
    address   = "Reading, Massachusetts"
}
 
@article{cohen,
  author   = "P. J. Cohen",
  title    = "The independence of the continuum hypothesis",
  journal  = "Proceedings of the National Academy of Sciences",
  year     = 1963,
  volume   = "50",
  number   = "6",
  pages    = "1143--1148",
}

Then,  we insert four code statements into the main document.tex file:

% ----- the document.tex file -----

\documentclass{article}

%% Imports the package natbib
\usepackage[square,numbers]{natbib}

%% Sets the bibliography style
\bibliographystyle{abbrvnat}

\title{An Article Title}
\begin{document}
\maketitle

%% Prints a reference to the citation entry
Lorem ipsum dolor sit amet ... \cite{latexcompanion}. 
Nulla ... \cite{einstein} donec scelerisque semper rutrum. 
Nunc nec lacus in nisi \cite{cohen}.

%% Imports the "bibFile.bib" file containing the bibliography sources
\bibliography{bibFile}

\end{document}

The output .pdf file will contain the three citations displayed with the numerical style. As we can see, the document references are ordered alphabetically, based on the alphabet in the citation keys:

natbib package

Natbib is compatible with the standard bibliographic style files, such as plain.bst, harvard, apalike, chicago, astron, authordate, etc. We should mention that this package offers multiple ways to manage textual and author-year type citations and provides style files for numerical citations such as plainnat, abbrvnat, and unsrtnat.

Also, many options can be added to \usepackage. We can benefit from a list of values such as square and numbers that enable squared brackets and numeric citations respectively.

3.2. Styling with BibLaTeX Package

BibLateX is a complete and flexible bibliography management package that enables high customization of the bibliography section with minimum effort. To implement it, we need to specify the location of the .bib file in the preamble. Also, we should not forget to insert the extra command of \printbibliography to display the references section:

% in the preamble 
%-----------------------------------
\documentclass{article}

%% Imports the biblatex package
\usepackage[
backend=biber,
style=alphabetic,
sorting=ynt
]{biblatex}

%% Imports the bibliography file "bibFile.bib" containing the sources of the citations
\bibliography{bibFile}
%-----------------------------------

\begin{document}

Lorem ipsum dolor sit amet ... \cite{latexcompanion}. 
Nulla ... \cite{einstein} donec scelerisque semper rutrum. 
Nunc nec lacus in nisi \cite{cohen}. 

%% Prints the list of cited references
\printbibliography

\end{document}

The citations in the output document are displayed with a textual style:

biblatex package

In fact, many options can be added when importing the BibLaTeX package. They should be inside brackets and comma-separated. For instance, the style option determines the bibliography and citation styles. The “alphabetic” value in the example defines an author-year referencing system. As for the sorting option, it specifies the measure used to sort the bibliography entries. It will be the key to reaching our main goal.

Let’s present, in the next section, three different ways to reference sorting by appearance criteria.

4. Sorting the References by Appearance

If we use a numerical referencing style, LaTeX will apply by default a bibliographic sorting based on the alphabetic order. To change it by order of appearance, i.e., the references are displayed chronologically, we have three possible alternatives.

4.1. Choosing a Specific Bibliography Style (unsrt)

The simplest way to order the references by appearance is to use a bibliography style that already adopts “no sorting”. More specifically, instead of using a plain style, we should use an unsrt style. This latter prints the citations in the format of numbers with, automatically, an ascending order.

To demonstrate it, let’s use the same files from the previous section, i.e., document.tex as the main file and bibFile.bib as the BibTeX database. Our document.tex file is a basic one, which uses no specific packages such as Natbib or BibLaTeX for bibliography management. All we need to do is to insert the code line \bibliographystyle{unsrt} at the end of the document.tex file, just before the \end{document} line:

% in the preamble 
%-----------------------------------
\documentclass{article}

%-----------------------------------

\begin{document}

%% The added code line
\bibliographystyle{unsrt}

\bibliography{bibFile}
\end{document}

It is as simple as that, and our bibliography will be sorted by appearance:

unsrt bibliography

However, the problem with this method is within the format. Being obliged to use a specific style to achieve the sorting criteria limits our choice of the best convenient citation style. The format offered by the unsrt is not suitable for all types of documents. In the case of academic writing, it fits well with ACM publication. Meanwhile, if we’re writing a scientific paper for IEEE publication, we can ideally consider the ieeetr style.

In general, when combining styling and sorting, we should consider style values that don’t include sorting. For example, if were using the Natbib package for styling, it is the unsrtnat style value that fulfills the condition.

However, if we’re still not content with the formatting style, let’s continue exploring the rest of the tutorial. We’ll probably find better alternatives for a wider choice of bibliography style while resolving the sorting issue.

4.2. The Makebst Tool

The makebst tool is a LaTeX package that generates customized bibliography styles. It allows us to design our own bibliography style while managing the sorting preferences. For this alternative, we’ll have a .bst file within the project folder, such as plain.bst.

In the case of a numerical bibliography style, the default sorting (alphabetic) will be executed. So, we need to access the .bst file and look for the piece of code containing SORT instruction and comment it out. In this way, there will be no sorting, which precisely means sorting by order of appearance:

% ----- the plain.bst file -----
%% ITERATE {presort}
%% SORT

If our .bst file is quite complicated, which is mostly the case, or if we’re not using this tool for bibliography styling, let’s consider the next alternative for reference sorting.

4.3. The BibLaTeX Package Tool

Another possible solution lies within the use of the BibLaTeX package. As we have mentioned in the previous section, this tool provides us with a large and flexible choice of bibliography styles. To apply the chronological order to the references, all we need to do is define the option sorting=none within the \usepackage line code. It will disable the alphabetic sorting and display an appearance-based sorting.

The advantage of this method is the non-limitation of the format choice. In fact, we can change the style of the bibliography by defining a different value for the option style when uploading the BibLaTeX package. Many examples for numerical referencing can be found, such as chem-acs, ieee, nature, nejm, etc. We just have to maintain the none value for the sorting option.

For example, we can use “nature” as a bibliography style value:

\documentclass[12pt]{article} 

\usepackage[sorting=none, style=nature]{biblatex} 
\bibliography{bibFile} 

\begin{document} 

Lorem ipsum dolor sit amet ... \cite{latexcompanion}. 
Nulla ... \cite{einstein} donec scelerisque semper rutrum. 
Nunc nec lacus in nisi \cite{cohen}. 

\printbibliography 
\end{document}

The output document shows numerical references that are ordered by appearance. Also, we can clearly notice that the bibliography style has changed compared to the previous example:

bibtex

5. Conclusion

In this tutorial, we discussed how to order references by order of appearance in a LaTeX document. Also, we covered several bibliography style management tools that go along with the sorting option.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.