How do (relative) paths work with the subfile package

importincludeinputpathssubfiles

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.

Best Answer

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

Related Question