Customize BibTex citation keys with Emacs

A BibTeX citation key is the short identifier that connects a citation in a document to an entry in a bibliography. The same paper can arrive with quite different keys depending on where its BibTeX entry was downloaded. A consistent naming convention makes those keys easier to recognize when reading or editing the manuscript.

Tools such as Zotero and JabRef can organize references and generate citation keys. Zotero organizes a reference library, while its Better BibTeX extension provides customizable citation-key generation and automatic bibliography exports. JabRef works directly with BibTeX and BibLaTeX files and includes a configurable key generator. As an emacser, I prefer to manage my references inside Emacs, where its packages offer several options. Ebib provides an interface for browsing and editing bibliography databases. Citar lets you search references, insert citations, and open associated files and notes from the minibuffer. Org-ref combines citation and cross-reference support in Org with BibTeX utilities.

For my workflow, Emacs's built-in tools are enough. I mostly use LaTeX commands for cross-references (\Cref is is a favorite). RefTeX and Org’s citation support cover my everyday needs. I do not need a separte interface for browsing the bibliography. I keep my references in one plain-text refs.bib file and use the built-in bibtex-mode to browse, search and edit it.

An essential part left to fix is, back to what we start with in this blog, a consistent automatic key naming convention. The default settings of bibtex-mode produce longer keys than I want, making citations harder to scan in my Org manuscripts. The function responsible is bibtex-generate-autokey, which Ebib also uses. My method customizes this existing mechanism: for one author, I use the surname and year, such as Smith2026; for multiple authors, I use their surname initials and year, such as SJ2026 for Smith and Jones.

The code below implements that convention. After pasting a BibTex entry into refs.bib, place the cursor inside the entry and press C-u C-c C-c to clean it and regenerate its key. This command executes the Elisp function (bibtex-clean-entry t). Once this configuration is loaded, the generated key follows my naming convention.

;; reftex configs for using \ref \Cref in the text.
(setq reftex-extra-bindings t)
(setq reftex-ref-style-default-list '("Cleveref"))
(setq reftex-use-multiple-selection-buffers t)

;; bibtex-mode configs for refs.bib.
(setq bibtex-align-at-equal-sign t)
(setq bibtex-maintain-sorted-entries 'crossref)
(setq bibtex-autokey-year-length 4)
(setq bibtex-autokey-edit-before-use nil)
(setq bibtex-entry-format t) ;; required by clean entry

;; automatic keys: first capital letters of authors' names + year
(defun my/bibtex-generate-autokey ()
  "Generate a citation key from author surnames and the year."
  (let* ((names (mapcar #'bibtex-autokey-demangle-name
                        (split-string (bibtex-autokey-get-field "author") "[ \t\n]+and[ \t\n]+")))
         (name-key (mapconcat 'upcase (mapcar (lambda (name) (substring name 0 1)) names)))
         (year (bibtex-autokey-get-year)))
    (if (> (length names) 1)
        (concat name-key year)
      (concat (capitalize (bibtex-autokey-get-names)) year))))

(with-eval-after-load 'bibtex
  (advice-add 'bibtex-generate-autokey :override #'my/bibtex-generate-autokey))

Comment by email Read the discussion

Copyright © 2024-2026 Zaichuan Du
Generated using Emacs and Org-mode