[Tex/LaTex] Subfiles in subdirectory, file not found

collaborationdirectoryfolderspathssubfiles

I am writing a long document with two other people using Dropbox as the file storage mechanism. I want to have each person be able to work on their piece individually. Therefore I want to be able to compile each document seperately, as well as compile the main document. The subfiles package seemed perfect for this.

I tried following the example for modular documents on Wikibooks, but it is not working for me. The top level document will build, but the lower level ones will have file not found errors.

Here is my setup:

  • Images folder
  • tex folder
  • main.tex
  • styles.sty

Inside main.tex:

\documentclass[11pt,letterpaper]{article}
\usepackage{styling} %includes \usepackage{subfiles}
\begin{document}
\maketitle
\tableofcontents
\subfile{./tex/subpiece1}
\end{document}

Inside subpiece1 in tex folder:

\documentclass[../main.tex]{subfiles}
\graphicspath{ {Images/subpiece1/} }

\begin{document}
\section{sectiontitle}
%Images and text
\end{document}

The error when building the subfile seperately:
../main.tex:4: LaTeX Error: File `styling.sty' not found. [^^M]

Best Answer

I came accross the same problem you did when i compiled my first multi-file document using the subfiles package.

Since I am not a long term latex user i do not fully understand the mechanics of the problem but I suspect the problem is that when you compile the "slave" file (in your case 'subpiece1.tex') your compiler searches for the custom package in the same directory as 'subpiece1.tex' and the other default tex directories.

I managed to solve the problem by changing the \usepackage{} command to also include a relative path that will be common for both the "master" and the "slave" .tex files.

What you need to do:

  1. Add a folder in your home directory for the master document. i.e. your home directory should have folders: master folder (containing main.tex), tex folder, images folder.
  2. Edit your main.tex folder so that the \usepackage{} command includes the relative path to 'styling.sty' (it should read \usepackage{../styling} with no file extension)
  3. If you did step one correctly 'styling.sty' will have the same relative path from 'main.tex' and 'subpiece1.tex' (the relative path for both is one folder up. This is achieved by '../' in the \usepackage{} command)
  4. Update all the other relative file paths so they read as required.

main.tex now reads

\documentclass[11pt,letterpaper]{article}
\usepackage{../styling} %includes \usepackage{subfiles}
\begin{document}
%\maketitle (I just removed these because for the demonstartion i didnt actually need them)
%\tableofcontents
\subfile{../tex/subpiece1}
\end{document}

subpiece1 now reads

\documentclass[../master/main.tex]{subfiles}
% again I just removed the graphics path because I have no need for it
\begin{document}
\section{sectiontitle}
%Images and text
\end{document}

I personally prefer to group my preamble.sty into the same folder as my main.tex but the basic idea is the same. I believe any path would be fine as long as the relative paths are the same for both the 'main.tex' and 'subpiece1.tex' files.

I also suspect there are better/more elegant ways of getting around this problem but this worked for me so far.

Related Question