%---------------------------------------- % CHAPTERS %---------------------------------------- \newcommand{\includechapters}[2]{% \foreach \i in {0, ..., #2} {% \edef\FileName{content/chapters/#1/\i}% The % here are necessary to eliminate any \IfFileExists{\FileName}{% spurious spaces that may get inserted \input{\FileName}% at these points } } } \includechapters{part01}{2} \includechapters{part02}{2}