How can I properly resolve relative paths in `\usepackage`?

I have a dedicated directory for my project; within that are nested directories source/, output/ and styles/ that contain the .tex, .pdf and .sty files.

I was surprised to find that while \usepackage{../styles/mystyle} from source/main.tex did work as expected, I have to use the same path in the incantation from within styles/mystyle.sty, not \usepackage{./substyle}.

In other words, relative paths in a file included with \usepackage would appear to be resolved with respect to the including file, not the included one.

This seems problematic since it couples sources and styles—and indeed, moving the source to, say, /tmp/main.tex breaks the document! this is, to say the least, surprising.

Am I doing something wrong? is there a package that allows me to do the equivalent of a \usepackage with a path that is resolved relative to the including file?

I know I could probably set up environment variables and/or edit some TeX configuration file, but I'd prefer not to in order to keep everything working with a standard TeXlive/MacTex installation.

• The usage of .. is prohibited for security reasons. – Martin Scharrer Jul 14 '13 at 19:47
• what would such a reason be—cding out of the current directory? it's not that my TeX install goes online or what. and i do currently use .. without (La)TeX complaining. – flow Jul 14 '13 at 20:39

The argument of \usepackage is a name not a file path. The fact that it sometimes works at all when passed a relative file path is just due to lack of error checking by the system. If the package does declare itself using \ProvidesPackage the use of such paths will generate a warning that the name is incorrect.

The fact that the primitve \input works relative to the initial file rather than maintain a notion of current file and input relative to the current file at that point in the document is just the way it is. It can't easily be changed from the macro layer of latex.

The best thing is to put the style files in the directory

~/texmf/tex/latex/mystyles/

where LaTeX will always look for packages, without the need to specify relative paths. Use

~/Library/texmf/tex/latex/mystyles/

if you're running MacTeX on Mac OS X. Create the directory structure if necessary. You can also decide to put symbolic links in that special place, so you don't need to maintain files in different places.

• symlinking might be acceptable... still means that my stuff won't run out of the box. i think sth along the lines of \usepackage{\thisdirectory/foo/bar} would be much preferable. – flow Jul 14 '13 at 20:42
• i selected the above answer, since (1) intense googling convinced me that (with the possible exception of LuaTeX) there is no way in TeX to get the current filename or path; (2) it would appear to be the most canonical way to do things; (3) it suffices to put symbolic links from within ~/(Library/)texmf/tex/latex to the directory containing the project-specific *.sty files; you do not have to link individual *.sty files. – flow Jul 14 '13 at 23:17
• (you may want to consider prefixing all your project stuff with a common string of letters—after all, all of TeX works within one huge namespace.) – flow Jul 14 '13 at 23:24

Do this

\usepackage{\string~"/path/to/file/<filename>"}

Which LaTeX understand as

\usepackage{"~/path/to/file/<filename>"}

Assuming you do not have conflicting package names/versions in your hierarchy, you could set the TEXINPUTS environment variable to be something like /path/to/main/directory// where the double slashes adds all the subdirectories. You could then just use \usepackage{mypackage} in your document.

Setting the environment variable does not need to be global; you can set it on a per run basis. It also doesn't require editing any configuration files. If you have a build file (e.g., arara or latexmk) you could probably automate it.