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.

  • Daniel Doherty

    Thanks for this. I work on straight LaTeX files using AucTeX. This is a great way to have continuous compiling there as well. Didn’t know latexmk could do that!

  • Pingback: How to achieve auto-compilation for the org–>latex–>pdf publishing chain — configurations around latexmk | Da's recipes on Emacs, IT, and more()

  • Pingback: 2016-02-01 Emacs News - sacha chua :: living an awesome life()

  • David G.

    I’ve set org-export-in-background to true and so that I can continue work in emacs while LaTeX file gets compiled in the background. If I want to revert this behavior (for debbuging) I do C-c C-e C-a l l.

  • David Henley

    Thanks for the latexmk tip – that’s a very useful tool. May I suggest adding -interaction=batchmode or -interaction=nonstopmode? That way, if there’s an error in your LaTeX file, latexmk will abort and try again next time you change your file, rather than waiting for you to input something in the terminal running latexmk.

  • eduardo mercovich

    Hi Ben.

    Thanks for another great tip. This really helped my pdf export. 🙂

    There’s also a relevant question & answer in http://tex.stackexchange.com/questions/10562/emacs-org-mode-and-latexmk about latexmk even more integrated into or-mode.


  • C. Baxter

    There is an all-elisp solution, namely:

    (setq org-latex-pdf-process (quote (
    “pdflatex -interaction nonstopmode -shell-escape -output-directory %o %f”
    “bibtex $(basename %b)”
    “pdflatex -interaction nonstopmode -shell-escape -output-directory %o %f”
    “pdflatex -interaction nonstopmode -shell-escape -output-directory %o %f”
    ;;; We could end here, but repeat to ensure full completion.
    “bibtex $(basename %b)”
    “pdflatex -interaction nonstopmode -shell-escape -output-directory %o %f”
    “pdflatex -interaction nonstopmode -shell-escape -output-directory %o %f”)))