# How do I specify a directory file path in the preamble such that all subfolders of the directory are searched?

I am creating a "main document" from a collection of sub .tex documents such that both the main and sub documents can be compiled separately.

i.e.

Main document looks something like this:

\documentclass{report}
\usepackage{subfiles}
\begin{document}
\tableofcontents
\subfile{chapter01}
\subfile{chapter02}
\subfile{chapter03}
\end{document}


Sub documents look like:

\documentclass[main.tex]{subfiles}
\begin{document}
\chapter{chapter title}
Some interesting text here.
\section{section 01 title}
A section of text
\section{section 02 title }
another section of text
\end{document}


I would like to put these files in separate directories with the structure:

(1) Parent Folder
(A) Main Document Folder
(B) Folder of Chapters
(i) Chapter 01 Folder
(ii) Chapter 02 Folder
(iii) Chapter 03 Folder


Is there a way to specify a file path in the main document preamble such that all folders in "Folder of Chapters" are searched for the appropriate file?

• The short answer is no. Now if you create a list of possible subfolder names one could search the list using \IfFileExists. – John Kormylo Nov 2 '18 at 16:28

I found an easy work around solution for my purposes. My goal in defining a file path was to make these documents easily portable from one computer to the next.

I found that I can simply use

\newcommand{\DirectoryPath}{DirectoryPath}


in the preamble, and

\subfile{\DirectoryPath filedirectory/filename}


in the body of the main file. Then if I ever move the files to a new computer it is easy to update the file directory path.

Depending on how you compile the document you might be able to set the TEXINPUTS environment variable to be something like /path/to/main/directory// where the double slashes adds all the subdirectories.

You can use texosquery to obtain file listings. It's a Java application designed to be used within TeX's shell escape using the interface provided by texosquery.tex. (texosquery.sty is just a small wrapper package that inputs the generic TeX code.)

If you want to use it with TeX's restricted mode, you need to set up the texosquery.cfg file to allow it. However, the restricted mode forbids certain characters to prevent malicious code injection, so it probably won't work in restricted mode with path names that contain spaces. If you have spaces, as your question seems to suggest, then you'll need the less secure unrestricted mode.

You can find all sub-directories using \TeXOSQuerySubDirList. For example:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{texosquery}

\TeXOSQuerySubDirList{\subdirlist}{,}{.}

\begin{document}
\ifdefempty\subdirlist
{Query Failed!}
{%
Subdirectories:
\newcommand{\handler}[1]{#1. }
\expandafter\forcsvlist\expandafter\handler\expandafter{\subdirlist}
}
\end{document}


This example just lists the sub-directory names in the current directory (.), which are fetched and stored in a comma-separated list (\subdirlist) with:

\TeXOSQuerySubDirList{\subdirlist}{,}{.}


This doesn't descend the subdirectories and just has the base name (not the full path) for each element of the list. If you want an alphabetically ordered list you can use \TeXOSQuerySubDirListNameAsc instead.

Another possibility is to hierarchically search for all files that match a particular regular expression. For example .+\.tex to match all files with the .tex extension:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{texosquery}

\TeXOSQueryWalk{\texfilelist}{,}{.+\string\.tex}{.}

\begin{document}
\ifdefempty\texfilelist
{Query Failed!}
{%
TeX files:
\newcommand{\handler}[1]{#1. }
\expandafter\forcsvlist\expandafter\handler\expandafter{\texfilelist}
}
\end{document}


In this case, each element of the list is a relative path. Since it's designed to work within TeX, / is used as the directory separator.