Using subfiles package with .sty files

I have a clever workaround to do this with the subfiles package. Just slightly modify your project structure:

./main/mydocument.tex
./main/mystyle.sty
./tex/mysubfile.tex
./img/

Then, always call files starting with .., for example ../main/mystyle.sty. This same path is correct whether you're starting from ./main/mydocument.tex or ./tex/mysubfile.tex.


There is no need to put your other .tex-files into subdirectories. I don't know the subfiles package, but I can recommend you the standalone package for this (I should mention here that I wrote it, so I'm biased). Put all required packages into the subfiles and compile them as normal just use standalone as class. The real class can be given by the class option and defaults to article. In the main document load the standalone package, maybe with the subpreambles option to include the preambles from the subfiles to the main file.

% Main file
\documentclass{report}
\usepackage{standalone}
\usepackage{mystyle}
\begin{document}
\input{mysubfile.tex}
\end{document}


% mysubfile.tex
\documentclass{standalone}
\usepackage{mystyle}
\begin{document}
% stuff
\end{document}

If you use mystyle.sty in multiple documents, you might consider putting it in a place that is searchable by TeX. Usually, this is the output of the command kpsewhich -var-value TEXMFHOME (more precisely, the tex/latex/ subdirectory of that directory). Once you put the file there, you should run the command texhash. Then, kpsewhich myfile.sty should print the path to the file. After doing this \usepacakge{mystyle} will work in all your LaTeX documents, without needing a copy of mystyle.sty in the same directory as the .tex file.