org-mode

org-mode citation and export to LaTex(PDF) and HTML

This article is composed in org-mode and posted by org2blog.

Workflow

The workflow is learned from the following links:

Journal articles and books are managed by Mendeley and Calibre respectively. Both Mendeley and Calibre allow exporting catalog into a .bib bibtex file. I usually directly export to a folder named ‘bib’ in my org directory where all my .org files stay. Taking notes while reading those articles and reviewing them afterwards help to memorize and organize the knowledge. There is a bibnotes.org where I put all my notes on papers and books. Of course I keep notes for other things as well. I decided to keep notes on papers and books separately because they are linked to the bibtex database and require special setup in the org-mode header.

Setup

My OS is ubuntu 12.04 32bit.

Requirement

reftex and org-exp-bibtex are included in emacs24. auctex can be installed by el-get. During my first installation of auctex, error message says autoconf is not found. If it happens, do “sudo apt-get install autoconf”.

reftex

Included in emacs24. It handles bib database search.

org-exp-bibtex

It depends on bibtex2html from http://www.lri.fr/~filliatr/bibtex2html/ which is packaged in e.g. Debian.

auctex (optional)

org-mode export to pdf function cannot handle bibtex export. Instead first export to .tex and C-c C-c in auctex mode multiple times, just as what we do with other LaTex IDE when export bibliography. Location of reference in the exported pdf file depends on where \bibliography{} and \bibliographystyle{} is. org-mode will put them before the first chapter no matter where # \bibliography{} is in the org file. Maybe latex source block works but I haven’t try that one since I prefer all the options in the header block and I am going to open the .tex in auctex mode anyway.

Org header

Put the following block in your org file header.


  #+LINK: bib file:bib/bibref.bib::%s
  #+LINK: paper file:~/Documents/org-mode/paper/%s.pdf
  #+LINK: chm file:~/Documents/org-mode/paper/%s.chm
  #+BIBLIOGRAPHY: bib/bibref plain limit:t
  #\bibliographystyle{plain}
  #\bibliography{bib/bibref}
  #+STYLE: <link rel="stylesheet" type="text/css" href="css/org.css">

Line 1-3 set up short form links. For example, a org-link like bib::abc link to the entry with id ‘abc’ in the file bib/bibref.bib. And paper::abc or chm:abch link to abc.pdf or abc.chm in the paper directory.

Line 4-6 are required by reftex which handles the bib database search as well as org-exp-bibtex for exporting the org file into LaTex/pdf or html.

Line 5 limit:t tells bibtex2html to only includes cited entries in the export, rather than the entire .bib.

Line 7 adds style sheet in the html export by org-export-as-html.

Emacs configuration

I use orgmode to manage my init.el, too. For example, configuration for different major mode can be organized into different src block and the web link where I got the code from as well as other notes are also added. Depending on my work, useful blocks are tangled into a single init.el file in my org directory which is symbolically linked to “.emacs.d/init.el”.

org-exp-bibtex reftex

I defined four templates for reftex. To trigger reftex for database search, use C-c C-g.

cite
used in writing paper for reference citation
note
used in writing paper to link to notes
chm
used in writing notes with chm file
pdf
used in writing notes with pdf file

  (defun my-org-mode-setup ()
    (when (and (buffer-file-name)
               (file-exists-p (buffer-file-name)))
      (load-library "reftex")
      (and (buffer-file-name)
           (file-exists-p (buffer-file-name))
           (reftex-parse-all))
      (reftex-set-cite-format
       '((?c . "\[_{}[[#%l][%l]]]")
         (?n . "[[note:%l]]")
         (?h . "%t\n:PROPERTIES:\n:Custom_ID: %l\n:BIB: [[bib::%l]]\n:PAPER: [[chm:%l][%l]]\n:END:\n")
         (?p . "%t\n:PROPERTIES:\n:Custom_ID: %l\n:BIB: [[bib::%l]]\n:PAPER: [[paper:%l][%l]]\n:END:\n")
         ))
      (setq org-link-abbrev-alist
            '(("bib" . "~/Documents/org-mode/bib/library.bib::%s")
              ("note" . "~/Documents/org-mode/bibnotes.org::#%s")
              ("chm" . "~/Documents/org-mode/paper/%s.chm")
              ("paper" . "~/Documents/org-mode/paper/%s.pdf")))
      (define-key org-mode-map "\C-c\C-g" 'reftex-citation)))
  (add-hook 'org-mode-hook 'my-org-mode-setup)))

Auctex

The following configuration block is from http://emacswiki.org/emacs/AUCTeX and is optional.

;;----------------------------------------
;; AUCTex
;;----------------------------------------
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)
(add-hook 'LaTeX-mode-hook 'visual-line-mode)
(add-hook 'LaTeX-mode-hook 'flyspell-mode)
(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)
(setq reftex-plug-into-AUCTeX t)

org-file-apps

Org-mode by default may not know how to open pdf or chm, customize the variable org-file-apps or insert the following block in the emacs config file (custom-set-variable may already exist, insert the org-file-apps line then).


 (custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
  '(org-file-apps (quote ((auto-mode . emacs) ("\\.mm\\'" . default) ("\\.x?html?\\'" . default) ("\\.pdf\\'" . default) ("\\.chm\\'" . "kchmviewer %s")))))

Link to papers

Using the Bibtex parses ubuntu package pybtex is of version 0.15 which raise error on duplicate entries. Download version 0.16.

The following script parse bibtex using pybtex library and create a link named by ID, the same one which is used in reftex. It works for me and only is meant to be an example, so feel free to modify it for your needs.

from pybtex.database.input import bibtex
from subprocess import call
import os

parser = bibtex.Parser()
bib_data = parser.parse_file('~/Documents/org-mode/bib/library.bib')

os.chdir('~/Documents/org-mode/paper')
for key in bib_data.entries.keys():
    try:
        file = '/' + bib_data.entries[key].fields['file'].replace('\\','').split(':')[1]
        ext = bib_data.entries[key].fields['file'].split(':')[2]

        if ext != '' and os.path.exists(file): # avoid link to folder
            if ext.lower() == 'chm':
                call(['ln', '--symbolic', file, key + '.chm']) # chm cannot open without ext
            else:
                call(['ln', '--symbolic', file, key])
        else:
            print file
    except KeyError as e:
        pass

Leave a comment