Tag Archives: productivity

Streamlining your LaTex workflow with LaTexMK

It’s been a *long* time since I last wrote a post and that’s mainly because I’ve been frantically writing my thesis.

For scientific writing I’m a big fan of LaTex for a number of reasons: dealing with and formatting mathematical notation is a joy, the implicit handling of intra-document references and bibliography makes life a lot easier and the separation of content and formatting helps me better focus on my writing.

That said, I do like to preview changes that I make and I’ve always found the workflow a little clunky. Having to save the tex file, head to the terminal and enter:

pdflatex mytexfile
bibtex mytexfile
pdflatex mytexfile
pdflatex mytexfile

Before opening up a PDF viewer to take a look at the result. It’s tedious but fear not, I’ve since grown tired and (like a good little computer scientist) managed to automate the process using a tool called latexmk to dramatically improve my latex workflow.

Latexmk

Effectively what latexmk does is watch your tex source file for changes and then run whatever it needs to in order to update your PDF automatically. Now, I simply open up a tex file and start latexmk in a Terminal window. Each time I save the source file, latexmk automatically runs in the background and  opens (or simply updates)  the PDF. This way, I never have to leave my tex editor nor manually run latex at all. I just save the file. On Mac OS X, I can even scroll through the PDF without removing focus from the tex editor.

Getting set up

I’ve explained how I set things up below but beware that this is specific to Mac OS X.

  • Download and install latexmk (but be aware it may have been included with your Tex distribution – run ‘which latexmk’ in the terminal to check).
  • Next, create a latexmk config file in your home directory, ~/.latexmkrc, and add the following lines:
    $pdf_previewer = "open -a /Applications/Skim.app";
    $clean_ext = "paux lox pdfsync out";
    

    Obviously, you can use any PDF viewer but I strongly recommend skim.

  • If, like me,  you mostly use latexmk with pdf files, you can add the following to your ~/.bash_profile:
    alias latexmk='latexmk.pl -pdf -pvc'
    

    You can always run latexmk.pl with other options if you need to.

Once you’re done, just ‘cd’ to the directory containing your latex source file, and run “mklatex myfile.tex”. Now you just leave it running while you work and it’ll take care of things automatically! Job done.