Emacs code for fetching a bibliography in BibTeX form from CiteULike

Posted in Emacs

I typically manage my references with CiteULike. My typical workflow is to add the articles that I’m interested in, and then while writing, keep a complete copy of the exported BibTeX next to the LaTeX files. This gets tedious to maintain by hand, so I wrote this little chunk of elisp to download the file and name it the way I do.

To use, simply place it in a file called citeulike.el on your load-path and add (require ‘citeulike) to your .emacs. Make sure to look at the customization options. Then, run M-x citeulike-download-bibtex in your LaTeX buffer.

;;;; citeulike.el --- Getting things from CiteULike while working in AUCTeX


;;; Copyright (C) 2011 David Christiansen


;;; Author: David Christiansen


;; This program is free software: you can redistribute it and/or

;; modify it under the terms of the GNU General Public License as

;; published by the Free Software Foundation; either version 3 of the

;; License, or (at your option) any later version.


;; This program is distributed in the hope that it will be useful,

;; but WITHOUT ANY WARRANTY; without even the implied warranty of


;; General Public License for more details.


;; You should have received a copy of the GNU General Public License

;; along with GNU Emacs; see the file COPYING.  If not, write to the

;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,

;; Boston, MA 02111-1307, USA.


;;; Commentary:

;; Purpose:


;; To make downloading an updated copy of references from CiteULike easier.


;;; Code:


(provide 'citeulike)


(defgroup citeulike nil

  "Options for CiteULike support"

  :prefix 'citeulike)


(defcustom citeulike-username ""

  "CiteULike username, used for downloading BibTeX exports."

  :type 'string

  :group 'citeulike)


(defcustom citeulike-export-username-prefix nil

  "Whether to add a username prefix to keys"

  :type '(choice (const :tag "Yes" t)

                 (const :tag "No" nil))

  :group 'citeulike)


(defcustom citeulike-export-key-type 0

  "What kind of keys to put in the exported BibTeX"

  :type '(choice (const :tag "Prefer personal key; otherwise use numeric key" 0)

                 (const :tag "Prefer personal key; otherwise use AuthorYearTitle key" 4)

                 (const :tag "Prefer numeric keys" 1)

                 (const :tag "Only export articles with a personal key" 2)

                 (const :tag "Export both keys" 3))

  :group 'citeulike)


(defcustom citeulike-export-include-amazon-link nil

  "Include Amazon links for books in exported BibTeX"

  :type '(choice (const :tag "Yes" t)

                 (const :tag "No" nil))

  :group 'citeulike)


(defcustom citeulike-export-clean-urls t

  "Escape URLs for BibTeX?"

  :type '(choice (const :tag "Escaped URLs" t)

                 (const :tag "Don't escape URLs" nil))

  :group 'citeulike)


(defcustom citeulike-export-smart-wrap 0

  "Work around BibTeX capitalization"

  :type '(choice (const :tag "Don't wrap" 0)

                 (const :tag "Smart wrap whole field" 1)

                 (const :tag "Smart wrap individual words" 2))

  :group 'citeulike)


(defun citeulike-export-url ()

  (concat "http://www.citeulike.org/bibtex/user/"

          citeulike-username "?"

          "do_username_prefix=" (if citeulike-export-username-prefix "1" "0") "&"

          "key_type=" (number-to-string citeulike-export-key-type) "&"

          "incl_amazon=" (if citeulike-export-include-amazon-link "1" "0") "&"

          "clean-urls=" (if citeulike-export-clean-urls "1" "0") "&"

          "smart_wrap=" (number-to-string citeulike-export-smart-wrap)))


(defun citeulike-download-bibtex ()


  (let* ((extension (file-name-extension buffer-file-name))

         (rest (file-name-sans-extension buffer-file-name))

         (target (if (equal extension "tex")

                     (concat rest ".bib")

                   (read-file-name "Filename to save: "))))

    (if (or (equal citeulike-username "") (not (stringp citeulike-username)))

        (error "No valid CiteULike username is configured")

      (let* ((command (concat "wget \"" (citeulike-export-url) "\""

                              " -O " target))

             (final-command (read-string "Command: " command)))

        (shell-command final-command)))))