# Can i automatically load chapters and sections based on the filesystem?

I was interested in knowing if i can load chapters and sections of my book based on the filesystem. For example:

Chapter 1/
Section 1.tex
Section 2.tex
Section 3.tex
Chapter 2/
Section 1.tex
Section 2.tex
Chapter 3/
Section 1.tex
Section 2.tex
Section 3.tex


I'm using scrbook.

-
I have .tex files with simple contents: \section{My Section} contents. Those .tex files are inside CHAPTER folders. Take a look at the example in my question. – Jashc Jun 15 '11 at 17:31
This doesn't look like a job for TeX or friends. I expect a simple bash script or similar could input all .tex files in a directory recursively in the right way... But doing this inside TeX would be tricky, I expect... – Seamus Jun 15 '11 at 17:31
Related Question: How to iterate through the name of files in a folder. – Peter Grill Sep 20 '12 at 21:43

If you have a well defined naming structure, you don't need external scripts which would most likely be OS specific. Programming wise, there is some overhead in this solution, but should work fine for your needs. Might need some adaption as I am not sure where you want to start a new chapter. If the file Section1 in each directory starts with \chapter, then this is ready to go.

\documentclass{article}

\newcommand*{\MaxNumOfSections}{6}%

\usepackage{pgffor}%

\begin{document}
\foreach \c in {1,2,...,\MaxNumOfChapters}{%
\foreach \s in {1,2,...,\MaxNumOfSections}{%
\IfFileExists{Chapter\c/Section\s} {%
\input{Chapter\c/Section\s}%
}{%
% files does not exist, so nothing to do
}%
}%
}%
\end{document}


Also note that I assumed that you have no spaces in the directory and file names. You can probably adapt it if you want spaces but I find that is an extra work that I try to avoid.

There are probably a few extra % here, but I have had problems with extra spaces, and there should be no harm in adding them.

-
Are you sure you can omit the third argument of \IfFileExists ? – xport Jun 15 '11 at 19:27
Good point: It is probably better to include. I have updated the code to reflect that. I did not have any problems in my test case so I think it is ok. – Peter Grill Jun 15 '11 at 19:37
\documentclass[a4paper]{report}
\usepackage{etoolbox}
\makeatletter
\begingroup
\everyeof{\noexpand}
\edef\next{\@@input|"ls -dm Chapter*" }
\begingroup\edef\x{
\endgroup\xdef\noexpand\auto@chapters{\noexpand\zap@space\next\space\noexpand\@empty}}\x
\def\do#1{%
\csxdef{auto@#1}{\@@input|"ls -m #1/Section*.tex" }}
\expandafter\docsvlist\expandafter{\auto@chapters}
\endgroup

\def\makedocument{\renewcommand{\do}[1]{%
\begingroup\edef\x{%
\endgroup\noexpand\forcsvlist\noexpand\input{\csuse{auto@##1}}}\x}%
\expandafter\docsvlist\expandafter{\auto@chapters}}
\makeatother

\begin{document}
\makedocument
\end{document}


It's your responsibility to put in the files "section1.tex" the \chapter command. But it would be possible to modify the macros also to cope with this.

Just an exercise, of course: a shell script would be way better. I didn't try with spaces in the names, as this is a bad thing with TeX in general.

You need to launch (pdf)LaTeX with the --shell-escape command line option for this to work.

-

Package multido that is not specific to PSTricks can also do it similarly. In other words, you can use for pdflatex and latex-dvips-ps2pdf.

\IfFileExists{filepath}{true-jobs}{false-jobs} can be used to check whether or not the given filepath exists. If yes, the code in the second brace will be executed, otherwise the code in the third brace will be executed.

## Case 1: no white spaces in the path

\documentclass{article}

\usepackage{multido}

\begin{document}
\multido{\ic=1+1}{10}%
{%
\multido{\is=1+1}{6}%
{%
\IfFileExists{Chapter\ic/Section\is}%
{%
\input{Chapter\ic/Section\is}%
}%
{%
% do nothing :-)
}%
}%
}%
\end{document}


## Case 2: there are white spaces in the path

I tested it on Windows and it worked. Don't forget to enclose the path with a pair of " signs.

\documentclass{article}

\usepackage{multido}

\begin{document}
\multido{\ic=1+1}{10}%
{%
\multido{\is=1+1}{6}%
{%
\IfFileExists{"Chapter \ic/Section \is"}%
{%
\input{"Chapter \ic/Section \is"}%
}%
{%
% do nothing :-)
}%
}%
}%
\end{document}


For those who knows C# programming, this link may help you to get the rough idea how to iterate the file structures and populate the input file.

-