11

I would like the have a simple keyboard shortcut/command in emacs that opens the .bib file of the current (possible multifile) document. This should be possible with RefTeX, as it can figure out the name of the .bib file (uses it for the C-[ command), but I could not find a command that just opens the file in the editor without any fancy selection feature.

This command would be useful, because usually what happens is that you find a bibtex entry online and you just need to paste it into your .bib file.

user41347
  • 213
  • 1
  • 5
  • 1
    In general there can be more than one bibfile. The simplest way to open one of the relevant buffers is to find a \cite command in the document and type C-c &. This will pop open another buffer containing the relevant bibfile centered on the bibtex entry referred to. The list of files ought to be accessible via reftex-get-bibfile-list, but it doesn't seem to work on my set-up. – Andrew Swann Nov 20 '13 at 11:48

3 Answers3

5

This requires AUCTeX:

(defun mg-LaTeX-find-bibliography ()
  "Visit bibliography file of the current document."
  (interactive)
  (let ((length (length (LaTeX-bibliography-list)))
    bib)
    (if (= length 1)
    (progn
      (setq bib (car (car (LaTeX-bibliography-list))))
      (unless (file-name-extension bib)
        (setq bib (concat bib ".bib")))
      (mg-TeX-kpsewhich-find-file bib))
      (if (< length 1)
      ;; If there is no element in `LaTeX-bibliography-list' use a `.bib'
      ;; file with the same base name of the TeX master as an educated
      ;; guess.
      (mg-TeX-kpsewhich-find-file (TeX-master-file "bib"))
    (setq bib (completing-read "Bibliography database: "
                   (LaTeX-bibliography-list)))
    (unless (file-name-extension bib)
      (setq bib (concat bib ".bib")))
    (mg-TeX-kpsewhich-find-file bib)))))

mg-TeX-kpsewhich-find-file is defined here.

giordano
  • 8,486
  • Thanks, it works, but there are two issues. First, it seems that we need to call TeX-normal-mode to reparse the file. Second, this is weird, but if the main .tex and .bib files have the same name (e.g., foo.tex and foo.bib), then the database is not found (as LaTeX-bibliography is empty in this case). – user41347 Nov 20 '13 at 13:37
  • If you insert the \bibliography macro with C-c RET bibliography RET you don't need to reparse the document. 2) Unfortunately that's a known bug with AUCTeX :-( I suggest you to always use different base names for the document and the bibliography file.
  • – giordano Nov 20 '13 at 13:51
  • Can't we use then the name of the tex master file?
  • – user41347 Nov 20 '13 at 14:04
  • Sorry, I'm not sure I understand your question. If you are asking whether is it possible to use the same base name for the TeX master file and the bibliography file then the answer is "no" (I'm talking about AUCTeX ability to parse files with the same base name, not about TeX). – giordano Nov 20 '13 at 14:07
  • I'm suggesting the workaround that if the function detects that LaTeX-bibliography-list is empty, then we try to open (basename of tex master)+".bib" as an educated guess. – user41347 Nov 20 '13 at 14:15
  • Ah, ok. I'm going to update the answer! – giordano Nov 20 '13 at 14:16