Over the last weeks I had to work with unix command line tools a lot. It was fun and added many new tools to my kit.

Just yesterday I was confronted with the task of installing all packages used in a TeX file. While trying to work on my master thesis from both a Mac and a Windows PC, I discovered that MacTeX does not automatically install packages when processing a document. (MiKTeX does.)

I am lazy when it comes to repetitive work, so I decided it was time for some coding. My first intuition was to write a small Java / Clojure / Ruby / uNameIt Tool, that would do the job for me. Instead, I put my new found affection for bash scripting to good use.

To print a LF separated list of all packages referred to in file:

  sed -nE "s/.*\\usepackage(\[.*\])?\{(.*)\}.*/\\2/p" <file>

How does it work

Sed’s -n flag suppresses output of lines that do not contain a match. When a matching line is encountered, it is replaced by the value of the second (\2) matching group. The /p flag tells sed to print the modified line. (See sed - The ‘s’ Command)

An example:

$ sed -nE "s/.*\\usepackage(\[.*\])?\{(.*)\}.*/\\2/p" document.tex

The whole script for installing all found packages using tlmgr can be found here.