12

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.

6 Answers 6

5

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}
7
  • 2
    @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.
    – lowndrul
    Commented Feb 28, 2011 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.
    – lowndrul
    Commented Feb 28, 2011 at 1:40
  • 1
    @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.
    – lowndrul
    Commented Feb 28, 2011 at 1:47
  • 1
    @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. Commented Feb 28, 2011 at 9:11
  • 1
    @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. Commented Feb 28, 2011 at 9:18
21

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.

0
3

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.

1
  • 2
    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.
    – lowndrul
    Commented Feb 28, 2011 at 1:27
2

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

You just need to add:

\makeatletter
\def\input@path{{../}}
\makeatother

at the top of mysubfile.tex.

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

0
1

I had the same problem just last week using the subfile package . If you have a file structure that includes different directories you'll often find addressing errors such as "file mychapter.tex not found" or "mystyle.sty not found". In order to avoid this problems always use the full path to each file, specially to the main.tex, since the style files are often in the same directory that the main.tex.

Here's the example you gave previously

/home/username/Documents/report/mydocument.tex
/home/username/Documents/report/mystyle.sty
/home/username/Documents/report/tex/mysubfile.tex
/home/username/Documents/report/img/

mydocument.tex looks like:

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

mystyle.sty looks like:

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

mysubfile.tex looks like:

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

It all goes in the logic followed by LaTeX to look for files: When you compile "mysubfile.tex" the reference directory will be /home/username/Documents/report/tex. It will look for the preambule in the file mydocument.tex located in the directory /home/username/Documents/report/. mydocument.tex will look for mystyle.sty located in the reference directory: /home/username/Documents/report/tex

By the way, you can put the \usepackage{subfiles} into mydocument.tex. It might be easier to load this package in your main.tex

5
  • 2
    Welcome to TeX.SX! Some people may find this helpful, however such full paths are not a portable solution, which others may need. Commented Jul 15, 2014 at 9:30
  • Thank you. I understand the problem with full paths. Although it is the best way to avoid problems in bash, is not that nice in LaTeX. I don't understand what you mean by portable. Full paths solved my problem with nesting directories while making subfiles.
    – 3nrique0
    Commented Jul 15, 2014 at 9:34
  • 1
    By "portable" I mean if you move your code to another machine or account then you will have to rewrite the paths before it will work. Commented Jul 15, 2014 at 10:51
  • Well.. When you compile a main or a subfile, the directory you are in becomes the "reference directory". Meaning that if you have a file structure with nested directories the "reference directory" will not be the same. So, the relative address of each subfile.tex and style.sty (as well as that of the main.tex) will change. This is a very common problem in bash, chaning the path to your files doesn't make your code less portable. If you require to compile your document in multiple locations, use specialized services to keep the structure of your files.
    – 3nrique0
    Commented Jul 15, 2014 at 11:20
  • 1
    I was wondering Andrew Swann, Is there a way to "declare the path" in LaTeX ? In bash we usually create a variable with the path. This way the path only needs to be "updated" once everytime you change computer|session|folder.
    – 3nrique0
    Commented Jul 15, 2014 at 11:32
1

If you are using a suitable operating system you can create a symbolic link from ./tex/ to the style files in the parent directory.

For example, ln -s ~/path/to/document/mystyle.tex ~/path/to/document/tex/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .