Packaging Latex dependencies with your .tex files.

A problem I’ve had for a while now is that when I move my Latex code around to different computers or send it to colleagues is the fact that different Latex installations often have different sets of packages installed.  This usually results in lots of missing file errors and the slow process of discovering all the missing dependencies manually and installing them. Looking for a way to do this automatically, I did a quick web search which didn’t seem to turn up any results. One mailing list post however did point me to a Latex switch called ‘-recorder’ which prints out a list of file operations performed by the Latex complier and dumps them into a ‘.fls’ file. Not wanting to wade through this file looking for dependencies manually I wrote a python script called latex-packager.py which you can find in my Github repository. If you run it with a ‘.tex’ file as a parameter it will run through the following steps:

  1. Check if the current ‘.fls’ is valid (i.e. it exists and is up to date).
  2. If the ‘.fls’ file is invalid it will (optionally) run the latex compiler for you with the recorder switch to generate a new ‘.fls’ file.
  3. Create a temporary folder and copy in all the files used to compile your ‘.tex’ file.
  4. Put all the files into an archive file with the format of your choosing.

I was worried that the Latex compiler wouldn’t look in the working directory for all the dependencies so I tested it by packaging one of my ‘.tex’ files and extracting the archive file into an empty directory. When I compiled the document with the recorder switch the ‘.fls’ file showed that the complier was sourcing all the files from the current directory which shows that the current directory is always the first place it looks for any files.

The next step will be to find some way to store all the dependencies into a subdirectory and reference it so that the directory is not littered with so many files although it is fine to quickly send a ‘.tex’ file to someone and not have to worry about if they have all the dependencies needed to compile it.

Basic Usage: ./latex-packager.py tex_file output_file

There are numerous switches that you can use to customise the behaviour of latex packager:

-v: Print more verbose information while executing.
-f: Force the script to ignore non-critical errors (not recommended).
-[z | j | s | p]: Use gzip, bzip2, 7zip and zip compression formats respectively (default is gzip).
–latex <spec>: Specify the latex compiler to use and any custom switches (default is pdflatex).