3

I'd like to be able to export BibTeX entries as (flexibly formatted) strings.

For example, a BibTeX entry like this one:

@Article{Chomsky:59,
  author    = {Noam Chomsky},
  title     = {On Certain Formal Properties of Grammars},
  year      = {1959},
  volume    = {2},
  pages     = {137--167},
  journal   = {Information and Control}
}

Should be converted into the following string:

Chomsky, Noam. 1959. On Certain Formal Properties of Grammars. Information and Control 2. 137--167. 

I used to manage my BibTeX database with JabRef which lets you customize exports. But I haven't seen something similar in Emacs/bibtex.el.

6

The org-ref package contains this functionality. Contrary to the name, org-ref has a lot of functionality outside of org mode. Take a look at the function org-ref-format-bibtex-entry. It also integrates this into helm-bibtex, so if you get everything set up correctly, you can call helm-bibtex, mark the citations you want, then hit f7 which inserts a formatted list.

EDIT:

If you didn't want to add a whole package with lots of dependencies, you can write your own function. Here's an example to illustrate:

(defun bibtex-format-entry-to-string ()
 "Format bibtex entry under point to a string."
 (interactive)
  (save-excursion
    (bibtex-beginning-of-entry)
    (let ((authors (bibtex-text-in-field "author"))
          (year (bibtex-text-in-field "year"))
          (title (bibtex-text-in-field "title")))
      (format "%s, %s, %s" authors year title))))
1

I've eventually found a very flexible way to achieve this using helm-bibtex and citeproc.el, adding just the following code (taken from here):

(defvar helm-bibtex-csl-style "~/.emacs.d/csl/unified-style-sheet-for-linguistics.csl")

(defvar helm-bibtex-csl-locale-dir "~/.emacs.d/csl")

(defun helm-bibtex-insert-citeproc-reference (_candidate)
  (let* ((locale-getter (citeproc-locale-getter-from-dir helm-bibtex-csl-locale-dir))
         (item-getter (citeproc-itemgetter-from-bibtex helm-bibtex-bibliography))
         (proc (citeproc-create helm-bibtex-csl-style item-getter locale-getter))
         (cites (mapcar (lambda (x) (citeproc-citation-create :cites `(((id . ,x)))))
                        (helm-marked-candidates))))
    (citeproc-append-citations cites proc)
    (insert (car (citeproc-render-bib proc 'plain)))))

(helm-add-action-to-source "Insert citeproc-formatted reference" 'helm-bibtex-insert-citeproc-reference helm-source-bibtex)

This method requires that you provide a CSL file and specify its path in helm-bibtex-csl-style and helm-bibtex-csl-locale-dir. There exist CSL files for virtually every formatting style. You can choose and download them conveniently here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.