Dynamic On-Demand LaTeX Compilation

Most of the existing approaches to integrating LaTeX compilation into a LaTeX writing workflow centered around a text editor (as opposed to a fancy-schmancy IDE) are horrendously bloated creatures, aggressively and voraciously hijacking so many key-mappings and normal functionality that it makes your Vim feel like it is diseased and is experiencing a pathological personality disorder of some kind. Yes, LaTeX-Suite, I am looking at mainly at you.

I did not want a platoon of obnoxiously cheery elves to insert two hundred environments into the document while a marching band parades around the room when I hit the `$` key. I just wanted a way to quickly and easily compile the current document, and optionally open it for viewing. Thus, I wrote a little plugin to do exactly that.

It has worked fine enough all this time. But today, thanks to a comment in a reddit discussion, I discovered something magical: latexmk

Not all that great if you use TexShop or another LaTeX IDE for latexing, but if you use a Plain Old Text Editor, then the following may change your life:

    $ latexmk -pdf -pvc nature-publication-2014.tex

Two neat things going on here.

First, "latexmk". This is the smart-single-click-do-all latex compiler (and is almost always available with all TeX distributions). Takes care of all those multiple compilation passes with BibTeX and such. So, no matter how complex your document, a single command Gets It Done, from compilation to viewing:

    $ latexmk -pdf nature-publication-2014.tex

Then there are the '-pvc' flags shown above. This is where things get sexy. This invokes "`latexmk`" on the document which then compiles it as per usual. But then, instead of exiting, it waits there monitoring the file for any changes. Upon detecting changes (like `make`), it recompiles and refreshes the document in the viewer!

So, if you have a shell with this command running in the background (either in a separate window or through, e.g., tmux or screen), you can merrily edit away in your POTE, and have constant refreshes as you save.

Leave a Reply

Your email address will not be published. Required fields are marked *