Saturday 24 December 2016

Include LaTeX packages in custom locations without changing environment variables

I recently had the need to include some packages with my repository, but it's not straightforward how to do this.

One way is to just dump all the necessary files at the root level of your document. This works (at least it does with pdfLaTeX), but if you have a lot of packages to include or your packages have a lot of files then this quickly becomes really messy.

Another way is to organize it nicely in a folder and then just use a relative path in usepacakge{abc}. This was nice, except for two things. If, for whatever, reason the folder containing the packages is removed but you do have the packages installed, then you'll still get a whole lot of errors as it expects them to be at that location exactly. The second and more onerous is that you're not supposed to provide a relative path here, so a lot of packages won't work (for me it was hyperref and scrextend). If it does work, you'll still get a warning about it (see the comments here for more information).

Of course, there's the recommended way of altering the environment variable TEXINPUTS. For me this wasn't ideal as I didn't want to have to run a CMake or custom setup script before compiling for a bunch of reasons. I'm sure this method works though. You can find instructions for this method here

Turns out that altering input@path{{abd/}{xyz/}} affects where usepackage searches (as usepackage is really just a wrapper around input). All you need to do is add your custom package path to input and you're good to go! Instructions for that are here

Edit:
It gets even better. Adding to the input paths also affects where documentclass searches! And (this may be good or bad) it will default to using any installed packages on the local machine before using the ones included in your packages folder.