Every now and then, I write an article/report/whatever in LaTeX. Last time I had to do it, I was in a rush. So, I recalled a really nice trick I found out a while ago. This trick allows me to have a .pdf file that is updated each time I save the corresponding .tex file(s).
The main idea is to combine two very interesting tools:
- latexmk: Usually, when you write a LaTeX document, you run the (pdf)latex command, then the bibtex command if you have a bibliography, the (pdf)latex command and maybe one last time to be sure the (pdf)latex command again. If you grew tired of this, you could have created a Makefile that just do all of that.
All of this was true until latexmk was born. Now, you change your shell to be in the directory and you run ‘latexmk’ or ‘latexmk -pdf’ and voilà!
- inotifywait: Inotify is a Linux subsystem that notify changes on the filesystem. The inotify-tools package provides several tools based on inotify. Among them, the inotifywait command will retain our interest. Just as the sleep command pauses the shell for N seconds, the inotifywait command pauses the shell until one of the files it “watches” has changed state.
So, now, you might be wondering: is there a way to combine this two tool so my LaTeX file get recompiled each time I save it ?
Simple answer: yes, it is. Look at that:
$ while true; do inotifywait -e modify *.tex *.bib; latexmk -pdf; done
For those that are new to shell programming, this is just an infinite loop, inotifywait waits for the various LaTeX related files in your directory to be modified. When it happens, the latexmk command is triggered. Once the build is finished, the inotifywait is relaunched.
Of course, the inotifywait trick is not limited to latexmk and LaTex documents !
Hope you enjoyed the tricks. Since I’m always eager to discover new productivity tips, feel free to drop a comment if you have anything to share.