Autocomplete Pandoc-style citations from a Bibtex

2020-05-26 13:09发布

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?

1条回答
再贱就再见
2楼-- · 2020-05-26 13:36

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.

查看更多
登录 后发表回答