Manage bibliographies with Emacs BibTeX mode and associated packages. This software can help you cite, read and annotate literature.

Manage Literature with Emacs BibTeX Mode

Peter Prevos

Peter Prevos |

2110 words | 10 minutes

Share this content

Authors need three resources: imagination, writing skills and the work of other writers. A common advice for authors to improve their writing is to read a lot. Keeping track of all the books and articles you read can be daunting. This article describes how to manage references and bibliographies with Emacs BibTeX mode. The biblio package helps searching internet databases and add literature, and the citar package provides access to your bibliography.

These Emacs packages let you create and manage bibliographies, read electronic documents and take notes, turning Emacs into a fully-featured literature management system.

This article is part of Emacs Writing Studio, a book that explains how to use Emacs to undertake research and write and publish articles, books, and websites. Emacs Writing Studio is also available as an e-book from your favourite retailer.

Emacs Writing Studio

Emacs Writing Studio

A comprehensive guide for writers seeking to streamline their workflow using Emacs. The book covers everything from organising ideas and writing distraction-free to publishing in multiple formats. It’s perfect for both beginners and experienced Emacs users, offering practical tips and a tailored configuration to enhance your writing process.

The source files of the book and EWS configuration are also freely available on GitHub.

Getting started with Emacs BibTeX mode

Emacs ships with bibtex-mode, a major mode to create and manage bibliographies. This mode, as the name suggests, uses the BibTeX file format as your default literature database. BibTeX is a plain text format to manage literature and citations and is typically used for LaTeX typesetting, but also perfectly integrates with Org mode. A typical entry for a book will look like this:

@article{stallman_1981_emac,
  title      = {{{EMACS}} the Extensible, Customizable Self-Documenting Display Editor},
  author     = {Stallman, Richard M.},
  year       = 1981,
  journal    = {ACM SIGOA Newsletter},
  volume     = 2,
  number     = {1-2},
  pages      = {147--156},
  doi        = {10.1145/1159890.806466},
  keywords   = {Emacs}
  file       = {computing/stallman-1981-emacs.pdf}
}

Each entry starts with an @-sign and the entry type, followed by a curly brace and a unique citation key. The following lines contain the relevant data about this entry, in this example, the earliest publication about Emacs.

BibTeX can process different types of literature, such as articles and conference papers, each of which has its own field types. BibTeX ignores any field names that are not recognised, which provides opportunities to use the file for other purposes, such as attaching files and keywords.

BibTeX is a plain text file with the .bib extension. You can store one or more bibliography files in a central folder to refer to them from anywhere in Emacs. You can also attach specific BibTeX files to an Org mode file to set a bibliography for a project.

You don’t need to install any packages to get started. Just created an empty file with a .bib extension, and Emacs enables bibtex-mode when you open the file.

Adding new entries

Emacs bibtex-mode uses templates to add new entries. To add a reference, use the bibtex-entry function (C-c C-b). Use the minibuffer completion to select the relevant type. Emacs also provides a shortcut for every kind of literature, which you can see with C-c C-e C-h.

Each entry type has at least one compulsory field. Any optional fields start with OPT. For fields that begin with ALT, such as author or editor, you must complete at least one of them.

Jump from field to field with C-j (bibtex-next-field) and complete all required fields and one of the ALT fields. When you’re done, press C-c twice (bibtex-clean-entry) to check the syntax and remove empty fields. This function also assigns a citation key to the entry using some configurable rules, but you can also set one manually.

To enter names, place the family name first, followed by a comma and the first name or initials. Any additional authors are separated by “and”, e.g. "Hawking, S. and Penrose, R.".

If you copy and paste an author name, or it is automatically inserted using the biblio tool discussed below, the first and family name might be the wrong way around. A nice Emacs hack is to use the org-transpose-words function, bound to M-t. This function swaps the order of two words, for example from "Richard Stallman" to "Stallman Richard".

To align the lines of each entry, you can also use the bibtex-fill-entry function (C-c C-q). The table below summarises the most salient keyboard shortcuts and functions in bibtex-mode.

Shortcut Function Description
C-c C-e a bibtex-article Add an article
C-c C-e b bibtex-book Add a book
C-c d bibtex-empty-field Empty the current field
C-j bibtex-next-field Jump to next field
C-down bibtex-next-entry Jump to the next entry
C-up bibtex-previous-entry Jump to the previous entry
C-c C-c bibtex-clean-entry Clean the entry
C-c C-q bibtex-fill-entry Align the fields
- bibtex-reformat Reformat all entries in region or buffer
- bibtex-sort-buffer Sort all entries in the buffer by their keys
Overview of keyboard shortcuts to add and edit entries.

Importing existing BibTeX files

You can also link another bibliographic database, such as Zotero, to Emacs. The Better BibTeX package in Zotero can export parts of or your complete bibliography as a BibTeX file every time it changes, keeping both version in sync.

Any existing bib file created with other software might not be fully compatible with bibtex-mode because some high-level features are unavailable in BibTeX mode. The bibtex-convert-alien function checks whether bibtex-mode can parse an ’alien’ BibTeX file. Files generated with Zotero do not seem to require this conversion step.

 

Fine-tuning BibTeX mode

BibTeX mode works out of the box, but you can fine-tune the variables. This Elisp expression adds optional fields for keywords and files for each entry type. These fields are essential when using the bibliographic files to read electronic documents and take notes. This expression also instructs Emacs to align the equal signs for each entry when you press C-q.

  ;; Managing Bibliographies
  (use-package bibtex
    :custom
    (bibtex-dialect 'BibTeX)
    (bibtex-user-optional-fields
     '(("keywords" "Keywords to describe the entry" "")
       ("file" "Link to a document file." "" )))
    (bibtex-align-at-equal-sign t))

BibTeX is old but stable software that was last updated in 1988 and has, as such, some minor limitations. The BibLaTeX dialect is a newer version. To change bibtex-mode to BibLaTeX, change the bibtex-dialect variable in the configuration to biblatex.

  (bibtex-set-dialect 'biblatex)

Jonathan Le Roux publishes a comprehensive manual on his website that explains some more functionality, such as structuring the citation key, in more detail.

 

Using the Biblio package to add new entries

BibTeX mode requires you to type all entries manually, which is inefficient and could easily lead to errors. Clément Pit-Claudel’s biblio package lets you browse and import bibliographic references from a range bibliographic sources: CrossRef, DBLP, arXiv, Dissemin, and doi.org.

The configuration for this package is straightforward, as there are no variables to configure. To get better response times from CrossRef, you need to customise the biblio-crossref-user-email-address variable and include your email address in the string.

  ;; Biblio package for adding BibTeX records and download publications
  (use-package biblio)

To use the package, open the relevant BibTeX file, run M-x biblio-lookup, and select the database and enter a search query. Once the search has completed, a new buffer opens with the results. Select your target with the arrow keys or search in the buffer to select the preferred candidate.

Once you have your target, you can insert the BibTeX record into the buffer from where you called the function with i. Alternatively, you can copy the BibTeX record with c and paste it into place later. You quit the search results with q. To see all possible commands in this buffer, use h or ?.

If you have a DOI (Digital Object Identifier) for a publication, then use the biblio-doi-insert-bibtex function to insert a BibTeX record into the current buffer.

Unfortunately, large corporate publishers still hold the world’s academic knowledge behind lock and key. However, open access and pre-print publications are slowly becoming the norm. The Dissemin website searches for copies of papers in an extensive collection of open repositories and websites.

To use this service with Emacs, evaluate dissemin-lookup to show information about the open access status of a paper using a DOI number. You can also press x when in the search menu for the biblio-lookup function to check for open access version.

 

Using Citar to Access Bibliographies

Several Emacs packages are available that link bibliography files to notes. The Ebib, Helm-BibTeX and Ivy-BibTeX each use their own completion system. The Citar package uses the standard minibuffer completion mechanism to access your bibliographies. This package provides access to hyperlinks, notes, attachments and the source bibliography file. Citar also integrates with org-cite, Org mode’s citation module.

Within the Org mode system, you can have two types of bibliographies. The global bibliography is a set of BibTeX files available from anywhere within Emacs. You can also link one or more project-specific local BibTeX files to an Org mode file, discussed in the article about bibliographic notes.

The ews-bibtex-directory variable stores the location of all BibTeX files and their associated attachments. This variable is defined in the article on Emacs configuration. By default, this variable is set at ~/Documents/library/.

You store all your BibTeX files and the associated attachments in this location. BibTeX files have to reside in this directory, attachments can be in subdirectories. Any file paths for BibTeX entries start at this path. For example, when the BibTeX entry states: file = {computing/stallman-1981-emacs.pdf}, the attachment is stored at: ~/Documents/library/computing/stallman-1981-emacs.pdf

The EWS configuration creates a new variable named ews-bibtex-file which lists the .bib files in the ews-bibtex-directory that start with a letter or a number (to exclude backup and hidden files). This code is a regular expression, which is a powerful method to find text. If you add another bibliography file, you will have to rerun the configuration code, or restart Emacs to register it.

If you add or remove any bibliography files from the bibliography directory, then use the ews-bibtex-register function to register the new set of bibliographies. You don't need to do this when you add new entries to a file, only when adding, renaming or removing a file.

Citations are further discussed in another article.

  ;; Citar to access bibliographies
  (use-package citar
  :defer t
  :custom
  (citar-bibliography ews-bibtex-files)
  :bind
  (("C-c w b o" . citar-open)))

Open the Citar menu with citar-open (C-c w b o) and select the literature you seek.

Finding literature with Citar is easy. After every keystroke in the Citar menu, the list of options is narrowed to the relevant matches. Select your candidate with the arrow keys and hit Enter. Use the Tab key to select more than one entry.

The first three columns in the menu indicate which entries include a hyperlink (L), one or more attached files (F) and an associated note (N). The configuration for taking bibliographic notes is explained in another article. You can filter the Citar completion menu for entries with notes using :n, showing only entries with an attachment use ":f" and entries that have links with ":l".

After hitting Enter, Citar provides a popup menu in the minibuffer from where you can open any attachments, create a new note or open an existing note and follow any hyperlinks listed in the BibTeX entry.

Example of the Citar menu
Example of the Citar menu.

Integrating Citar and Biblio

The Biblio package is useful, but one minor inconvenience is that you must jump to the relevant bibliography file before inserting a new entry. It also provides two separate search functions.

The bespoke ews-bliblio-lookup (C-c w b b) function lets you select the BibTeX file where you would like to store the search results before choosing one of the available sources. This function also combines searching for DOIs with the other sources, removing some steps from your workflow.

Emacs Writing Studio

If you like to support my work, then please purchase the Emacs Writing Studio book.

Emacs Writing Studio

Emacs Writing Studio

A comprehensive guide for writers seeking to streamline their workflow using Emacs. The book covers everything from organising ideas and writing distraction-free to publishing in multiple formats. It’s perfect for both beginners and experienced Emacs users, offering practical tips and a tailored configuration to enhance your writing process.

You can find the source files for the book and the latest configuration files on GitHub:

Emacs is a malleable system, so everybody will have their personal preferences to undertake a task. Any article on how to use Emacs is thus opinionated. If you have a different way of doing things, please complete the contact form to send me an email or contact me on social media.

The next article explains how to create bibliographic notes with the Citar-Denote package.

Share this content

You might also enjoy reading these articles

Writing Prose with Emacs

Exploring Your Ideas With the Denote-Explore Package

Reading eBooks with Emacs