3

I am trying to understand how (relative) paths need to be used with the subfiles package. In particular, it's not clear how content that I usually would include with \include, \input, \includegraphics, and \bibliography should be included within subfiles.

  1. Does it matter whether a subfile uses \include or \input or \import to add content with a path relative to the subfile directory or does it do the same thing because of internal subfile processing? The package documentation notes that \import is used to address issues with standalone compilation of subfiles not located in the directory of the "main file" containing \usepackage{subfiles}:

Sometimes it is desirable to put a subfile together with its images and supplementary files into its own directory. The difficulty now is that these additional files have to be addressed by different pathes depending on whether the main file or the subfile is typeset. As of version 1.3, the subfiles package handles this problem by using the import package.

  1. For each use of \subfile in the main file, do I need to first change the \graphicspath to ensure the graphics path is set to the directory containing that respective subfile? In the example found at the bottom of page 5 of the documentation, no graphics path change is required, although this post suggests that it IS required, causing confusion.

  2. While I have a project-wide .bib file that each subfile uses to generate citations and bibliography, do I need to somehow specify the path of the .bib file relative to each subfile if I want (a) a single overall bibliography when the main file is compiled and (b) only those entries pertaining to the specific subfile when a given subfile is compiled? This post seems close to answering this but it's unclear exactly how this works.

2 Answers 2

1

To alleviate some of the confusion: most posts before 2020 refer to old versions of the subfiles package, which required more work. Information in more recent posts is probably more reliable than information in older ones.

include vs. input vs. import with subfile package

``Does it matter whether a subfile uses \include or \input or \import to add content with a path relative to the subfile directory or does it do the same thing because of internal subfile processing?''

Yes, it matters, these commands do different things. \input tells LaTeX to replace the \input command by the contents of the file; if this file refers to other files, the path information is interpreted relative to the file containing the \input command, even if the file is located in another directory. \include is similar in this respect, but does some additional things. \import, however, takes care that the input and the graphics paths are modified such that the path given in any \input, \include, \includegraphics, \import and \subfile statement in the imported file is interpreted relative to the imported file. \subfile essentially is an \import command, but additionally skips the preamble of the imported file. So the idea of \import and \subfile is that any path information in the imported/subfiled file is relative to this file, no matter which other document uses the file.

graphics paths with subfile package

``Do I need to first change the \graphicspath to ensure the graphics path is set to the directory containing that respective subfile?''

No, this is something \import and \subfile take care of.

how to use a project-wide bibfile

The following example assumes that you use classic BibTeX, and the following project structure:

main.tex
allrefs.bib
part1/details.tex

main.tex:

\documentclass{article}
\bibliographystyle{plain}
\usepackage{subfiles}
\begin{document}
This is the main file, citing~\cite{a}, but also~\cite{b}.
\subfile{part1/details}
\bibliography{allrefs}
\end{document}

allrefs.bib:

@Article{a,
  author =  {Morris, B.},
  title =   {Whipping the whip},
  journal = {JWCS},
  year =    {2022}
}
@Article{b,
  author =  {Henk, V.},
  title =   {Hipping the hop},
  journal = {JWCS},
  year =    {2022}
}

part1/details.tex:

\documentclass[../main]{subfiles}
\begin{document}
\section{The details}
This is a subfile, but can also be processed as a main file.
It sites~\cite{b}.
\ifSubfilesClassLoaded{%
  \bibliography{../allrefs}% <<< path relative to this file
}{}
\end{document}

In the main directory, you can typeset main.tex with the commands

pdflatex main; bibtex main; pdflatex main; pdflatex main

while the subfile can be typeset with

cd part1; pdflatex details; bibtex details; pdflatex details; pdflatex details

This results in the documents main.pdf (left) and part1/details.pdf (right). enter image description here

2
  • Yes, I know what \include, \input, and \import all do. The confusion was the documentation seems to imply \import somehow behaves differently in subfiles, but it seems like what it means is that \subfile uses \import rather than the others to "add" the content of the subfiles to the main file. Mar 2, 2023 at 22:43
  • 1
    @socialscientist Thanks for the edits.
    – gernot
    Mar 6, 2023 at 9:00
1

I was using subfiles to write a rather extensive portfolio consisting of eight chapters, an appendix with additional literature and a dossier containing lots of external PDF's. All of these were created as subfiles to the main file. I currently am reworking that portfolio into a book using the same setup. Both portfolio and book are based on The Orange Book by Legrand. a public template that can be found on Overleaf among others.

The Orange Book uses a structure.tex file that defines a lot of the layout of the book to be created. It essentially is the preamble of the main file. That file is loaded into the main file using the command \input{structure.tex}. Since the bibliographic environment is also included in the structure.tex file the first few lines of the main document look like this:

\documentclass[11pt,fleqn]{book} 

\input{structure.tex} % Insert the commands.tex file which contains the majority of the structure behind the template
\addbibresource{hb_specialist_new.bib} % BiblateX bibliography file
\input{appendix/woordenlijst}

\usepackage{subfiles}

As you can see I load three separate files into my main tex file. The last file woordenlijst contains the glossery entries used in all chapters in this book. The bibliography source file is loaded the usual way. Both structure.tex and hb_specialist_new.bib files are located in the same directory as the main.tex file. The woordenlijst.tex file is located in a subfolder called appendix.

The the chapters are in subfolders to the main folder and contain their figures in subfolders to their folder. So two folders deep relative to the main folder. The folder structure is in short like this:

Parent folder containing the `main.tex`, `structure.tex` and `hb_specialist_new.bib` files
|_ figures folder that contain images for chapter heading and main file
|_ appendox folder containing `woordenlijst.tex`
|_ domein1 folder containing `chapter_one.tex`
   |_ figures1 folder containing the figures used in chapter one
|_ domein2 folder containing `chapter_two.tex`
   |_ figures2 folder containing the figures used in chapter two
|_ etc.

The main.tex file loads the chapter files using the line (here given for the first two chapters):

%----------------------------------------------------------------------------------------
%   CHAPTER 1
%----------------------------------------------------------------------------------------

\chapterimage{lancaster1.jpg} % Chapter heading image

\subfile{domein1/zien_weten}

%----------------------------------------------------------------------------------------
%   CHAPTER 2
%----------------------------------------------------------------------------------------

\subfile{domein2/zien_begrijp}

Since the Orange Book template uses pictures in the chapter heading that file is included in this example to show how a relative path is used in the chapter file. That image file has to be included in the heading of the chapter files, when you use the package subfiles. When you use the package import you can remove that image loading line.

\documentclass[../hbspec_main_mhc]{subfiles}

\chapterimage{../figures/lancaster1.jpg} % Chapter heading image

\begin{document}

\chapter{Weten en begrijpen}\index{Weten en begrijpen}
\label{chp:weten}

As you can see, the path to the image heading is given by its relative path. Inside the chapter file graphics are loaded using the line:

{\includegraphics[width=0.95\linewidth]{figures1/Emotional-Intelligence-Competencies.png}}

Remember that the folder figures1 is a subfolder of the domein1 (chapter one) folder. On compilation these images are loaded from their respective paths.

To answer your questions in summarise:

  1. It doesn't matter how you include another file into a subfile as long as you maintain path integrity throughout your folders and subfolders.
  2. You don't need to change the \graphicspath when you use the relative path structure inside \includegraphics. But if you want to, the both answers in the question you referred to, are very useful.
  3. As long as you load the bibliography.bib file in the preamble of the main file, all subfiles have access to that file and the bibliography will be created for all subfiles in the main file.

Note I currently use the package import inside the main.tex file. Switching from subfiles to import is rather easy to achieve changing the lines loading the chapter files. The main file now contains these lines to load the first chapter:

%----------------------------------------------------------------------------------------
%   CHAPTER 1
%----------------------------------------------------------------------------------------

\chapterimage{lancaster1.jpg} % Chapter heading image

\subimport{domein1}{zien_weten}

In the heading of the first chapter the preamble lines aren't required anymore, so the first line in the chapter file reads:

\chapter{Weten en begrijpen}\index{Weten en begrijpen}
\label{chp:weten}
2
  • The issue with using import instead of subfile is that your chapter files are no longer standalone compilable files because they lack a premable. Mar 1, 2023 at 20:37
  • Both packages would be incompatible with my chapter files. I would need to declare the structure file, bibliography and other stuff that resides in the preamble of my main file. So what is the benefit of using a package like import or subfiles then?
    – alchemist
    Mar 1, 2023 at 23:22

You must log in to answer this question.

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