Generalsubfiles | Nested Files

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
srvs
Posts: 1
Joined: Tue May 24, 2011 10:10 am

subfiles | Nested Files

Post by srvs »

Dear all,

I have our project's folder set up as followed:

Code: Select all

-Chapters
--Subchapters
-Figures
-Includes
main.tex
I want to use the subfiles package so that everyone can compile their own chapter, however, the subdirectories cause numerous file not found errors. Compiling a file from e.g. chapters requires the figures to point to ../Figures instead of Figures. This is annoying because then all the paths would have to be changed when compiling the main document. The same goes for the preamble, which is also included from Includes. This isn't such a big problem as I can just type the preamble in the main file instead, but all the path errors are annoying as it kind of nullifies the advantages of the subfiles package.

main.tex

Code: Select all

\documentclass{}
%
\usepackage{subfiles}
%
\begin{document}
\subfile{Chapters/abstract}
\end{document}
abstract.tex

Code: Select all

\documentclass[../main.tex]{subfiles}
\begin{document}
\includegraphics{example.pdf} 
\end{document}
Compiling from abstract.tex causes a file not found error, obviously. Ideally I'd be pointing to all the files with an explicit path, however I can't just use C:\... as I'm not the only one compiling, and everyone has their path different. Is there some sort of way to use a variable that always points to the main directory depending on the user's directory setup, so that everyone can compile from wherever without changing paths of figures etc?

My question is similar to http://tex.stackexchange.com/questions/ ... -sty-files but I couldn't figure out how to use the mentioned texinputs or texmf solutions.

I'm using the latest version of Miktex.
Thanks in advance.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

drm0hr
Posts: 2
Joined: Mon Jan 30, 2012 9:08 pm

Re: subfiles | Nested Files

Post by drm0hr »

Did you ever figure out how to do this? I've run into the same problem and am curious if it is possible to include figures this way.
drm0hr
Posts: 2
Joined: Mon Jan 30, 2012 9:08 pm

subfiles | Nested Files

Post by drm0hr »

I have figured out a (messy) work around. I am using the subfiles package and a slightly different file structure than you:

Code: Select all

Chapters
--Chapter_1
---Figures
----image.jpg
---chapter_1.tex
main.tex
Again the issue was that the subfile "chapter_1.tex" needs to use the path "Figures/" to find the images, where as "main.tex" needs to use "Chapters/Chapter_1/Figures/". I solved this by adding the following to the subfile:

chapter_1.tex

Code: Select all

\documentclass[../../main.tex]{subfiles}

\let \originalcmd \graphicspath
\renewcommand{\graphicspath}[1]{\originalcmd{{Figures/}}}

\begin{document}
\graphicspath{{Chapters/Chapter_1/Figures/}}
...
\includegraphics{example.pdf}
...
\end{document}
and making no changes to the main file.

main.tex

Code: Select all

\documentclass{}
%
\usepackage{subfiles}
%
\begin{document}
\subfile{Chapters/Chapter_1/chapter_1.tex}
\end{document}
Essentially what I did was tell the \graphicspath command to ignore all inputs and instead use "Figures/" as an argument. Since this reassignment is in the preamble of the subfile, it only takes place when the subfile is being compiled. When "main.tex" is compiled, the subfile's preamble is ignored and the proper \graphicspath{Chapters/Chapter_1/Figures/} is used.
wirylattice
Posts: 1
Joined: Mon Jun 11, 2012 9:55 pm

subfiles | Nested Files

Post by wirylattice »

Instead of using renewcommand for graphicspath you can pass multiple directories. Slightly less complicated:

Code: Select all

\documentclass[../../main.tex]{subfiles}
\begin{document}
\graphicspath{{Figures/}{Chapters/Chapter_1/Figures/}}
In this way both Figures/ and Chapters/Chapter_1/Figures/ will be attempted to be scanned for the file needed, though only one of them will be present depending on which file is being compiled.
bvkatwijk
Posts: 1
Joined: Mon Jul 22, 2013 6:24 pm

subfiles | Nested Files

Post by bvkatwijk »

I've figured out a different solution which may come in handy. Basically its a way to make every subfile point to the main directory for a valid relative path if its compiled on its own. This solution works for files as well as pictures.
So suppose we have a directory structure like:

Code: Select all

main.tex
folderA
--fileA.tex
--folderB
----fileB.tex
And we wish for fileA to be able to find fileB, either when its compiled on its own or subfile'd by main.tex. This works for me:

main.tex:

Code: Select all

%documentclass, packages and preamble

\begin{document}
\newcommand{\main}{.}
%Command \main defined in document body instead of preamble
%(since subfiles use the main's preamble)
%so its only defined in main.tex, not in subfiles

\subfile{folderA/fileA}
\end{document}
A subfile fileA.tex:

Code: Select all

\documentclass[../main.tex]{subfiles}
%command /main will only be defined if it isn't already
\providecommand{\main}{..}
\begin{document}

%Insert fileB which is in folderB inside the current folderA
\subfile{\main/folderA/folderB/fileB}

\end{document}
Post Reply