[Tex/LaTex] Using subfiles package with .sty files

folderssubfiles

Following the Project Structure Wiki and Subfiles Wiki, I've built the following project structure:

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

mydocument.tex looks like:

\documentclass{report}
\usepackage{mystyle}
\begin{document}
\subfile{./tex/mysubfile.tex} 
\end{document}

mystyle.sty looks like:

\ProvidesPackage{mystyle}
\usepackage{subfiles}
% other packages
% command declarations

mysubfile.tex looks like:

\documentclass[../mydocument.tex]{subfiles}
\begin{document}
% stuff goes here.
\end{document}

I'm using (1) the subfiles package because I'd like to compile mysubfile.tex by itself and (2) mystyle.sty because I'd like to modularize this project a bit and split package and command declarations into their own .sty file. The problem is that when I try and compile mysubfile.tex by itself it can't "see" mystyle.sty.

Q: Is there some way I can still satisfy (1), (2), and the project structure outlined above and get things working? Some sort of option or command in the subfiles package?

I've tried: (1) moving mystyle.sty to different directories, (2) moving the subfiles declaration \usepackage{subfiles} to different parts of the document, (3) checking the subfiles documentation. All no dice.

I suppose that the second best option would be to stick all project files in the same folder and get rid of my .\tex folder. At least then, I can get things to compile. But I'd rather not sacrifice the project structure.

Best Answer

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}
Related Question