Suppose I am writing a book with 45 chapters (in separate files 1.tex
2.tex
etc) and I want to input them into main.tex
. How can I write a for loop for this?
Use for loop to input files
inputloops
inputloops
Suppose I am writing a book with 45 chapters (in separate files 1.tex
2.tex
etc) and I want to input them into main.tex
. How can I write a for loop for this?
Best Answer
As it is about inputting files containing chapters of a LaTeX-document I suppose the
\include
-command is the appropriate command for inputting files. But you can easily replace instances of\include
by instances of\input
if you prefer.Edit: As a leftover from when I switched from
\input
to\include
, I erroneously left the filename-extension ".tex" as ⟨tokens after number-component of filename⟩. Unlike with\input
with\include
you don't provide the extension .tex—the extension .tex is added automatically by the\include
-command. This is now rectified.Probably a tail-recursive loop does the trick.
Here a loop where filenames are specified:
In case all filenames are of same pattern, you can have a loop based on counting:
Syntax:
yields a sequence of
The code
gets you a sequence:
If you don't want
\ifnum#1<\expandafter\exchange\expandafter{\number#2}{} %
you can introduce another macro which applies\number
for normalizimg things before actually starting the loop: