# Managing BibTeX Files with Emacs

Here are a couple of tips on exploiting Emacs when working with BibTeX databases. I do not use a BibTeX database manager (though I have tried Jabref). I prefer to edit my bib files entirely within Emacs, and have had no trouble doing so even with my largest database of around 6800 entries.

Back in the 1980s and 1990s BibTeX users had little choice but to create bib entries from scratch. Nowadays most journal websites have an “export citation” option that allows you to download a bib entry for a paper you are looking at. Unfortunately, these bib entries are of widely variable quality and are almost never entirely correct. What’s more, they will not match your own house style as regards ordering of fields and use of abbreviations. A great help in reformatting bib entries is the `bibtex-clean-entry` command in the BibTeX-mode of Emacs. I use it to

• remove unwanted blank lines and spaces,
• put the fields in the order I prefer,
• change field delimiters from curly brackets to quotes,
• delete delimiters around numerical fields,
• change double dashes to single dashes in page ranges (BibTeX will convert single dashes to double as necessary),
• change the entry type and field names to lower case,
• set the indentation of field to two spaces.

These and other aspects of the formatting are customizable. Here is an example of a bib entry downloaded from JSTOR.

```@article{10.2307/2323313,
ISSN = {00029890, 19300972},
URL = {http://www.jstor.org/stable/2323313},
author = {Dan Scott, Donald R. Peeples},
journal = {The American Mathematical Monthly},
number = {7},
pages = {651-653},
publisher = {Mathematical Association of America},
title = {A Constructive Proof of the Partial Fraction Decomposition},
volume = {95},
year = {1988}
}
```

After I hit C-c C-c in Emacs the entry is transformed to

```@article{10.2307/2323313,
author = "Dan Scott, Donald R. Peeples",
title = "A Constructive Proof of the Partial Fraction Decomposition",
journal = "The American Mathematical Monthly",
volume = 95,
number = 7,
pages = "651-653",
year = 1988,
issn = "00029890, 19300972",
url = "http://www.jstor.org/stable/2323313",
publisher = "Mathematical Association of America"
}
```

Further work is needed (such as replacing the comma by “and” in the author field), but the entry is now much closer to the format I require.

BibTeX-mode is not perfect. While `bibtex-clean-entry` can even generate a new key for the entry, I have not been able to get it to match my key-generating algorithm. And I have not found a way to make `bibtex-clean-entry` delete fields (such as `issn`, or `publisher` for the `article` type) that I do not use.

If you would like to see how I customized BibTeX-mode, take a look at the `.emacs` and `.emacs-custom_windows` files in my dot-emacs setup.

## 2. Bib Search and PDF Display with Helm-BibTeX

The Emacs RefTeX package provides some useful features for handling citations within a $\LaTeX$ document, providing an intelligent link between the $\LaTeX$ document and the relevant BibTeX databases. One thing it does not do is enable easy access to PDF files corresponding to bib entries. The Helm-BibTeX package can do this and much more.

To explain how I use Helm-BibTeX, suppose I have the following lines in my dot emacs.

```(setq helm-bibtex-bibliography '("~/texmf/bibtex/bib/la.bib"
"~/texmf/bibtex/bib/njhigham.bib"
))

(setq helm-bibtex-library-path '("~/pdf_papers" "~/pdf_books"))
```

These tell Helm-BibTeX to look in the first path for bib files and in the second path for PDF files whose names match a specified bib key.

If I type `M-x helm-bibtex` and type `dongarra` in the minibuffer then all bib entries in `la.bib` and `njhigham.bib` containing `dongarra` in the author or title fields are displayed, one per line. I can narrow the list down by typing a space and then another search term. Hitting `tab` displays several options, which include options to insert a `\cite` command for the item under the cursor, to insert a formatted reference for the item under the cursor, and to go to that entry in the bib file. The fourth column shows a looped square symbol (⌘) that indicates the presence of a matching PDF file. Hitting enter (or f1, or Ctrl-j) opens the PDF file in the default PDF application.

Here is Helm-BibTeX in action.

Helm-BibTeX relies on the Helm package, which provides a powerful framework for incremental completion and selection narrowing.