LaTeX forum ⇒ Generalsubfiles | Nested Files Topic is solved

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

subfiles | Nested Files

Postby srvs » Tue May 24, 2011 11:53 am

Dear all,

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

  1. -Chapters
  2. --Subchapters
  3. -Figures
  4. -Includes
  5. 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.

  1. \documentclass{}
  2. %
  3. \usepackage{subfiles}
  4. %
  5. \begin{document}
  6. \subfile{Chapters/abstract}
  7. \end{document}

  1. \documentclass[../main.tex]{subfiles}
  2. \begin{document}
  3. \includegraphics{example.pdf}
  4. \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 ... -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.


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

Postby drm0hr » Mon Jan 30, 2012 9:58 pm

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.

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

Postby drm0hr » Tue Jan 31, 2012 12:06 am

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

  1. Chapters
  2. --Chapter_1
  3. ---Figures
  4. ----image.jpg
  5. ---chapter_1.tex
  6. 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:

  1. \documentclass[../../main.tex]{subfiles}
  3. \let \originalcmd \graphicspath
  4. \renewcommand{\graphicspath}[1]{\originalcmd{{Figures/}}}
  6. \begin{document}
  7. \graphicspath{{Chapters/Chapter_1/Figures/}}
  8. ...
  9. \includegraphics{example.pdf}
  10. ...
  11. \end{document}

and making no changes to the main file.

  1. \documentclass{}
  2. %
  3. \usepackage{subfiles}
  4. %
  5. \begin{document}
  6. \subfile{Chapters/Chapter_1/chapter_1.tex}
  7. \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.

Posts: 1
Joined: Mon Jun 11, 2012 9:55 pm

Postby wirylattice » Mon Jun 11, 2012 10:00 pm

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

  1. \documentclass[../../main.tex]{subfiles}
  2. \begin{document}
  3. \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.

Posts: 1
Joined: Mon Jul 22, 2013 6:24 pm

Postby bvkatwijk » Mon Jul 22, 2013 6:45 pm

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:
  1. main.tex
  2. folderA
  3. --fileA.tex
  4. --folderB
  5. ----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:

  1. %documentclass, packages and preamble
  3. \begin{document}
  4. \newcommand{\main}{.}
  5. %Command \main defined in document body instead of preamble
  6. %(since subfiles use the main's preamble)
  7. %so its only defined in main.tex, not in subfiles
  9. \subfile{folderA/fileA}
  10. \end{document}

A subfile fileA.tex:
  1. \documentclass[../main.tex]{subfiles}
  2. %command /main will only be defined if it isn't already
  3. \providecommand{\main}{..}
  4. \begin{document}
  6. %Insert fileB which is in folderB inside the current folderA
  7. \subfile{\main/folderA/folderB/fileB}
  9. \end{document}

Return to “General”

Who is online

Users browsing this forum: No registered users and 7 guests