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.

Null
  • 1,525
Jashc
  • 41

3 Answers3

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.

Peter Grill
  • 223,288
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.

egreg
  • 1,121,712
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.

Display Name
  • 46,933