Speed up pdf export from org-mode with latexmk

These days I write almost everything (e.g. research papers, lecture notes and slides) in org-mode, and then export to pdf (see my posts on org-mode here). This is a really pleasant and efficient way to create documents, but when working on a long document, it can take several seconds for emacs to compile the exported latex to pdf. During that time, emacs is hung up waiting for the compilation to finish, which is annoying.

I thought I’d share my workflow for streamlining this process. The key ingredient is not part of emacs. I use latexmk which is a perl script that watches a latex file and compiles it if it changes (or any file that it depends on changes). It will repeat the compilation (including calling bibtex) as many times as needed to resolve all references.

So, if I am working on a file, say document.org, I will run latexmk on the corresponding tex file using

latexmk -pvc -pdf -view=none document.tex

Here, the -pcv option means to keep watching the file for changes and recompile as needed; the -pdf option means build a pdf from the latex using pdflatex, and -view=none tells latexmk not to open a pdf viewer to show the resulting pdf.

I then open the resulting document.pdf in skim, an excellent pdf viewer for the Mac (unfortunately hosted on sourceforge). The killer feature of skim compared to the built-in OS X preview app, is that it will automatically redisplay a pdf if the file changes (I believe evince or okular on linux do the same).

Putting the pieces together, I have latexmk running in a terminal, and an emacs window and skim window side by side. I then export my org file to latex with e.g. C-c C-e l l which happens almost instantly and gives me control back of emacs. The latex is compiled in the background by latexmk and then a few seconds later the pdf updates in skim and I can see my changes.