September 9, 2013

LaTeX workflow

In the last several years, the common workflow for using LaTeX has changed since I first learned it. In particular, bibtex has been to a large degree superseded by biblatex-biber. Additionally, while I used to use pdftex to build pdfs, it doesn’t support unicode. After reading Joel Spolsky’s article about unicode, I decided this is a hard requirement, so pdftex was out. While both XeTeX and LuaTeX projects have addressed this issue, it seems like XeTeX is more mainstream. Finally, for the build process, I used things like Vim-LaTeX or AucTeX (or sometimes a manually written Makefile). The latexmk package (not to be confused with the LaTeX-Mk package, which hasn’t seen an update since 2010-12-28 and is not part of the MikTeX distribution) now does an excellent job of managing the building of your pdf. This post outlines my new work flow that takes advantage of the latest tools in the TeX/LaTeX world.

First, install TeX Live (use your package manager if you can so you automatically will get updates). The first thing to set up is latexmk. latexmk has a few options that are worth configuring (man latexmk will give you all the gory details). I put the following in my ~/.config/latexmk/latexmkrc:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Choose xelatex as the default builder of pdfs, don't stop for errors, use synctex
$pdflatex = 'xelatex -interaction=nonstopmode -synctex=1 --shell-escape %O %S';
# .bbl files assumed to be regeneratable, safe as long as the .bib file is available
$bibtex_use = 2;
# User biber instead of bibtex
$biber = 'biber --debug %O %S';
# Default pdf viewer
$pdf_previewer = 'evince %O %S';
# Extra file extensions to clean when latexmk -c or latexmk -C is used
$clean_ext = '%R.run.xml %R.synctex.gz';

Open up your favorite editor and create example.tex with the following contents

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
\documentclass{article}
\usepackage[backend=biber]{biblatex}
\usepackage[pdfencoding=auto]{hyperref}

\addbibresource{references.bib}

\begin{document}
Numbering should start at zero~\cite{EWD831}.
\printbibliography
\end{document}

and references.bib with

1
2
3
4
5
6
7
@unpublished{EWD831,
  author = {Dijkstra, Edsger W.},
  pages = {3},
  title = {{Why numbering should start at zero}},
  url = {http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF},
  year = {1982}
}

Now to have latexmk build example.tex into the pdf enter this into your terminal

latexmk -pvc -pdf ./example.tex

This should immediately build your pdf and open it up using evince. In another terminal, start editing the .tex file or the .bib file. As soon as you save example.tex, example.pdf should automatically be rebuilt and the updated result shown in evince. This can sometimes be problematic if there are build errors, but the -interaction=nonstopmode will prevent the build from hanging so once you fix the problem it should automatically get fixed. latexmk automatically determines dependencies, so if you have files included/input into your main .tex file, it will update the build appropriately as soon as it detects a change to the file (not just a time stamp change, but an actual content change).

I haven’t yet gotten synctex to work with vim, but that option is enabled above so it should just be a matter of figuring out how to configure vim correctly. I’ll leave that for another day. Also, there is another TeX build automation tool called arara that looks pretty promising. It looked like a bit more work and it didn’t come bundled with TeXLive, so I stuck with latexmk, but I’ll keep an eye on arara down the road.

Luke Peterson, some rights reserved.