# Using subfiles package with .sty files

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.

-

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}

-
@Martin: It looks like the subfiles package does something very similar. One thing I like about the subfiles package is that it allows me to put sub-documents in folders different from the main document. I can just pass the location of the main document as an option to subfiles package declaration. –  brianjd Feb 28 '11 at 1:35
@Martin: I supposed I don't need to put other .tex files in sub-folders, but it is recommended in a number locations like, here, here, and here. Plus, I just like the structure. Ahh well, I guess something will have to give. –  brianjd Feb 28 '11 at 1:40
@Martin: The other thing I like about the subfiles package is that you don't need to include extra package declarations in your sub-files. –  brianjd Feb 28 '11 at 1:47
@brianjd: Yes, its seems that subfiles and standalone are doing something very similar just look at the preamble-sharing from two different sites. What I like on standalone is that you don't have to update your main preamble every time you include one of your subfiles! IMHO the subfiles are independent from the main file because they are used in many different main files (digest, paper, presentation of that paper, thesis). I can freely mix my subfiles (holding mostly diagrams) and standalone makes sure all required packages are loaded. –  Martin Scharrer Feb 28 '11 at 9:11
@brianjd: Also you can load files from other directories with standalone and I agree that different types of files might be placed in different directories. I normally put my figures into figures/. What I don't get is why put other .tex files in different directory by default as the main .tex file? I also put .tex holding diagrams into a central directory and use the TEXINPUTS environment variable to a point latex there. No package option required. Putting all of these files into a texmf tree might be even better. –  Martin Scharrer Feb 28 '11 at 9:18

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.

-
An absolutely genius hack. Congratulations. –  Bill Cheatham Jul 5 '12 at 9:55

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.

-
Thanks for the comment. It's a good idea. It'll definitely work. But I (1) don't want the mystyle package to be visible to other projects, (2) don't want the tex/latex folder becoming clogged with a bunch of my project-specific classes, and (3) want all my project files in one folder so that they can easily be tar'd and emailed. –  brianjd Feb 28 '11 at 1:27

You can use the internal macro \input@path as explained in http://tex.stackexchange.com/a/24827/34384.

\makeatletter

at the top of mysubfile.tex.
However, a problem may occur if you have two files with the same name in . and ./tex.