4

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.

3
  • 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, 2011 at 17:31
  • 1
    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, 2011 at 17:31
  • Related Question: How to iterate through the name of files in a folder. Sep 20, 2012 at 21:43

3 Answers 3

10

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*{\MaxNumOfChapters}{10}% Adjust these two settings for your needs.
\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.

2
  • Are you sure you can omit the third argument of \IfFileExists ? Jun 15, 2011 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. Jun 15, 2011 at 19:37
3
\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.

1

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.

You must log in to answer this question.

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