20

I'm trying to use org-mode to do almost everything. In my scientific writing activity, I would like to sketch a whole paper in an .org file and then export in LaTeX. I don't want to load the .bib bibtex file in the init.el as I could have multiple ones. For now, I've added this at the head of my .org file:

#+BEGIN_SRC elisp
(setq reftex-default-bibliography '("./references.bib"))
#+END_SRC

But I have to execute it manually every time. Is there any way to make it automatic? The optimal would be to have a directive at the beginning of each .org file saying emacs: "ok, now you should load this .bib file". Any hint? Thanks.

petrux
  • 665
  • 1
  • 5
  • 18
  • Wouldn't a file local variable do the trick? E.g. `M-x add-file-local-variable` – olaf b Nov 11 '14 at 12:06
  • Thanks for reply. Unfortunately, it's not what I'm looking for. In this way, every time I load the file I need to type the command. Something lile `#+STARTUP: do this`. – petrux Nov 11 '14 at 14:13
  • Which command? You only have to set the file local variable once. – olaf b Nov 11 '14 at 14:16
  • You were right! Using the `add-file-local-variable-prop-line` I could generate the `# -*- reftex-default-bibliography: ("./references.bib"); -*-` line at the beginning of the file. – petrux Nov 11 '14 at 14:37

3 Answers3

23

Adding a line like the following is enough to tell org-mode which .bib file to consult:

#+BIBLIOGRAPHY: refs plain

refs is the name of the BibTeX file (.bib extension omitted), and plain is the bibliographystyle.

You can then call org-reftex-citation (bound to C-c C-x [ by default) to insert references.

From the documentation:

(org-reftex-citation)

Use reftex-citation to insert a citation into the buffer. This looks for a line like

#+BIBLIOGRAPHY: foo plain option:-d

and derives from it that foo.bib is the bibliography file relevant for this document. It then installs the necessary environment for RefTeX to work in this buffer and calls reftex-citation to insert a citation into the buffer.

Export of such citations to both LaTeX and HTML is handled by the contributed package ox-bibtex by Taru Karttunen.

As the last sentence suggests, you'll need to add

(require 'org)
(require 'ox-bibtex)

to your init-file to make sure references get exported to LaTeX properly. When I set this up for myself, I found that I also needed to customize org-latex-pdf-process in order to make PDF exports work:

(setq org-latex-pdf-process '("texi2dvi -p -b -V %f"))

Addendum

If you don't want to use the default binding for org-reftex-citation, you can set up a custom key binding for it. For example, the following binds org-reftex-citation to the default binding for reftex-citation:

(define-key org-mode-map (kbd "C-c [") 'org-reftex-citation)
itsjeyd
  • 14,586
  • 3
  • 58
  • 87
  • Thanks for the answer. I'm not marking it as correct just because I see the usage of `# -*- reftex-default-bibliography: ("./references.bib"); -*-` equivalent, with the advantage that I can use the usual RefTeX key binding to add a new reference. Am I missing something? Shall we edit the answer in order to make it more complete for any future reader? Thanks. – petrux Nov 20 '14 at 14:05
  • @petrux You can just bind `org-reftex-citation` to the same key sequence `reftex-citation` normally uses... See the updates to my answer. – itsjeyd Nov 20 '14 at 16:13
  • I take it there is no way to use multiple bibliography files with `org-reftex-citation`? If I add a second `#+BIBLIOGRAPHY`, then the first one seems to be ignored. – Brian Z Mar 03 '15 at 12:48
  • @BrianZ I don't think the use case you describe is supported out of the box. But maybe someone on here has a hand rolled solution? If I were you I'd ask a new question (pointing to this thread for reference); it definitely sounds like something that others might be interested in as well. – itsjeyd Mar 03 '15 at 20:16
  • 1
    Evaluating `(setq reftex-default-bibliography '("./a.bib" "./b.bib"))` seems to do the trick. So does `# -*- reftex-default-bibliography: ("./refs1.bib" "./refs2.bib"); -*-`, although it throws up a warning that this variable should not be defined locally. – Brian Z Mar 06 '15 at 13:59
  • For it to work with `reftex-citation`, I needed to insert `# \bibliography{path/to/bibliography.bib}` at the top of the file and call `reftex-reset-mode` afterwards. – henning Mar 03 '22 at 11:00
4

It seems that ox-bibtex is not included in the latest org package (8.2.10-34-gc41bbc-elpa). So I searched it online and put into directory /org/lisp/ once downloaded. But I ran into the following error when trying to do an export involving some bibtex.

Symbol's function definition is void: org-element-insert-before

Then I check the file 'org-element.el', of course there is no such function predefined. But why isn't there? The online API document of org-element mentioned it in once.

So I guess currently it is impossible to use RefTeX as the way the document describes unless you are willing to switch to the beta version or some older one. See

Here I share my way to use the beta version

  1. git clone git://orgmode.org/org-mode.git

  2. Move the folder to ~/.emacs.d/ and name it as org-beta

  3. Modify load-path, let the beta version shadow other version, and include the user contribute directory

    (add-to-list 'load-path (expand-file-name "org-beta\\lisp" user-emacs-directory))

    (add-to-list 'load-path (expand-file-name "org-beta\\org-contrib\\lisp" user-emacs-directory))

(optional) you can use make command to compile the source file and build the doc

  1. Now you can (require 'ox-bibtex). I prefer to write like this: (eval-after-load 'org (require 'ox-bibtex))

  2. In order to generate reference list at the right place, put something like #+BIBLIOGRAPHY: your_bib_lib_name plain option:-d to the end of the org file. More options please refer to the help document in ox-bibtex.el

  3. To make the HTML export part work right, you may need the latest bibtex2html, i.e., 1.98. To build it from source, download it from github, and follow the instruction in the 'README' help document.

If you are using windows like me, use Cygwin64 with all the devel packages. Using Cygwin64, cd to the dir of bibtex2html, and type ./configure, wait finish, then type make, wait finish. Now in this dir, you will get bib2bib.exe, bibtex2html.exe and aux2bib. Copy them to some dir in your environment var PATH. Test it by typing bibtex2html in the cmd.

I have to do the above job because I use texlive2014. If you are a miktek user, bibtex2html 1.95 may just work well, which can be easily installed with a windows installer.

  1. To make the PDF export part work right, use the following code to initialize your org-mode:

    (setq org-latex-pdf-process
          '("xelatex -shell-escape -interaction nonstopmode -output-directory %o %f"
            "bibtex %b"
            "xelatex -shell-escape -interaction nonstopmode -output-directory %o %f"
            "xelatex -shell-escape -interaction nonstopmode -output-directory %o %f"))
    

It is the 'bibtex' part that does the job. I use the shell escape command for minted package of latex.

wilx
  • 173
  • 7
empenguin
  • 41
  • 2
  • It seems to me that your answer is only related to the question in the sense that it's also about org-mode and ox-bibtex. Since your issue is different it's best if you open a separate question where others can provide targeted answers. – paprika Mar 17 '15 at 10:46
  • 1
    I just want to make a complement to the answer by itsjeyd. itsjeyd suggest following the document of org-reftex-citation. I did, and got the above problem. In case other people run into the same situation, I shared my investigation here. – empenguin Mar 17 '15 at 12:27
  • IIRC the ELPA package of org-mode does not include the files under `contrib/lisp`, which includes ox-bibtex. So you'd have to install org-mode from source *including* `contrib/lisp` or just copy `ox-bibtex.el` to your load-path. – paprika Mar 18 '15 at 16:27
  • 1
    Yes. I clone the beta version of org. Now it works fine. – empenguin Mar 21 '15 at 05:02
  • 6
    @paprika There's no need to install `org-mode` from source to get the `contrib` directory. As explained [here](http://orgmode.org/elpa.html), Org has its own ELPA archive. All you need to do is add that archive to `package-archives`; you can then install (and update) `org-plus-contrib` using the package manager. Of course, this is not to say that the approach you suggest doesn't work; I just wanted to mention (for anyone else that comes across this discussion) that there is another approach that does not involve the overhead of having to update `org-mode` separately through `git`. – itsjeyd Mar 21 '15 at 09:36
0

For some reasons, using #+BIBLIOGRAPHY: refs plain did not work for me, but, after enabling org-ref:, I can just do:

I can also try to include citation cite:SM20.

bibliographystyle:alpha
bibliography:ref.bib
tobiasBora
  • 405
  • 2
  • 12