15

Pandoc-style citations

Pandoc uses a Markdown format that supports automatic citations using keys from BibTeX files. Some examples for the format are:

Blah blah [@doe99]

Blah blah [@doe99, p.33]

Blah blah [see @doe99, pp. 33-35; also @smith04, ch. 1].

Emacs and Pandoc/Markdown

There is a Pandoc-Mode for interacting with Pandoc through Emacs which can be combined with Markdown-Mode. Pandoc-Mode and Markdown-Mode don't support autocompletion of citations from bibtex files. A mode that has this kind of support for TeX files is Reftex. I am looking for a way to get Reftex-style autocompletion when editing Markdown files.

Autocompletion in other editors

There is a solution for Textmate: Autocomplete pandoc-style citations from a bibtex file in textmate.

The feature is also in Vim-Pandoc:

Autocompletion was implemented by hacking away at LaTeX Box's implementation of bibtex citation completion, even if the results don't look much like the original.

How can I get autocompletion for pandoc-style citations from a bibtex file in Emacs?

lecodesportif
  • 10,737
  • 9
  • 38
  • 58

1 Answers1

13

To do this you can use reftex-citation.

Setup

Enable RefTeX

You have to enable RefTeX for the file you are editing which can be done via M-x reftex-mode or by setting a file variable such as adding -*- mode: reftex; -*- to the first line of the file.

Tell RefTeX where you bibliography is

You need to tell RefTeX where your bibliography file is. This can be done by adding the following to your .emacs (and editing the path according to your setup):

;; So that RefTeX finds my bibliography
(setq reftex-default-bibliography '("path/to/bibfile.bib"))

Note that reftex-default-bibliography is a list, so you can add several paths to it.

If you use different bibliographies for different files it might be preferable to tell RefTeX of the bibliography from each file. I am afraid I only know of an ugly way to do this. The idea is that since RefTeX can extract the correct bibliography from LaTeX macros you can embed a LaTeX macro in a comment. Thus, you can add such a comment along with the specification of bibliography to Pandoc:

bibliography::bibliography_name.bib
<!-- \bibliography{bibliography_name} So that RefTeX knows about the bibliography -->

Tell RefTeX how to format citations

To get reftex-citation to insert in the format used by Pandoc you have to customize reftex-cite-format such as inserting the following into your .emacs:

(eval-after-load 'reftex-vars
  '(progn 
     (setq reftex-cite-format '((?\C-m . "[@%l]")))))

You may wan to include other formats too. For instructions on how to do this see https://tex.stackexchange.com/a/31992/5701. Note that this setting is global so that if you also use RefTeX for LaTeX it will also be affected.

Use

To insert a citation either do M-x reftex-citation or C-c [ then press Enter and you are allowed to insert a search term for searching in your bibliography. RefTeX will then insert the key of the bibliography item you selected in Pandoc format.

Community
  • 1
  • 1
N.N.
  • 8,336
  • 12
  • 54
  • 94
  • This works, but I can't change `reftex-default-bibliography` for each file that has a different bibliography. Each file has a settings file `.filename.latex.pandoc` which includes a line `bibliography::/path/to/bibtexfile.bib`. Can Reftex automatically load the Bibtex file from the settings file? – lecodesportif Nov 28 '12 at 22:49
  • @lecodesportif It seems that customizing `reftex-bibliography-commands` as described in http://tex.stackexchange.com/a/54825/5701 won't work as `bibliography::/path/to/bibtexfile.bib` is not in LaTeX format. How are comments formatted in Pandoc? Maybe you could specify the bibliography via a commented LaTeX macro or . You could also add more paths to `reftex-default-bibliography` as it is a list. You might also be able to this file local via a file variable. – N.N. Nov 29 '12 at 06:31
  • 1
    Pandoc Markdown uses HTML comments ``. How would Reftex retrieve the bib file path from the comments? The cleanest way might be to have some Emacs Lisp code get the bib file name from the [Pandoc-Mode settings file](http://wwwuser.gwdg.de/~jkremer/pandoc-mode-manual.html#settings-files) and pass it to Reftex. – lecodesportif Nov 29 '12 at 08:25
  • 1
    @lecodesportif Embedding a LaTeX macro to tell RefTeX where the bibliography is seems to work (added to my answer). I am afraid that other than this adding a list of the bibliographies you want to use with RefTeX is the best I can come up with at the moment as I am not sure how to fetch the bibliography from a Pandoc file and I am not how to tell RefTeX of a file local bibliography. – N.N. Nov 29 '12 at 09:55
  • You can also directly bind `reftex-citation` to `C-c [` in the markdown mode hook, as suggested [in the manual](https://www.gnu.org/software/auctex/manual/reftex/Citations-Outside-LaTeX.html#SEC31). That shows how to use a local definition for `reftex-cite-format` (and by analogy `reftex-default-bibliography`), too. – Dave Kleinschmidt Jun 26 '16 at 22:36