[Tex/LaTex] How to properly resolve relative paths in `\usepackage`

external filespaths

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.

Best Answer

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.

Related Question