General\input{} : all tex.files in a folder

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
cato
Posts: 2
Joined: Mon Jan 05, 2009 3:33 pm

\input{} : all tex.files in a folder

Post by cato »

Hi! 1'm new to the community. hope someone can help me:

I have a holder with some tex.files that I want to \input into a main.tex file.

One way would be to write out each file name using separate \input{} commands:

\input{file1}
\input{file2}
\input{file3}
...

Since I want all the separate files input one after the other, it would be great to place the files in a separate folder and then only have one \input{} command:

\input{folder}

Is there any way aroud the "brute force" option?? Thanks :)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Re: \input{} : all tex.files in a folder

Post by phi »

Hello,
LaTeX cannot list directories, so it's impossible unless you use an external program.
cato
Posts: 2
Joined: Mon Jan 05, 2009 3:33 pm

Re: \input{} : all tex.files in a folder

Post by cato »

ok, thanks. good to know for future reference :)

(someone told me that it should be easy to write something in python; for instance a script that generates the necessary code in a separate tex.file and then \input{} that file in the main.document.)
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

\input{} : all tex.files in a folder

Post by phi »

Yes, that's indeed easy.

Code: Select all

#!/usr/bin/python
import os
import fnmatch
stream = open("Chapters.tex", "w")
for chapter in sorted(fnmatch.filter(os.listdir("Chapters"), "*.tex")):
    stream.write("\\input{Chapters/%s}\n" % chapter)
stream.close()
Or using a shell script (if you are running a Unix-compatible system):

Code: Select all

#!/bin/bash
for CHAPTER in Chapters/*.tex
do
    echo "\\input{$CHAPTER}"
done >Chapters.tex
PGScooter
Posts: 31
Joined: Mon Jul 14, 2008 2:47 pm

Re: \input{} : all tex.files in a folder

Post by PGScooter »

with the bash and python scripts, you would have to take that output and copy it into your main tex file. This shouldn't be much of a problem in most cases, but if your files are constantly changing and you have to rerun and repaste a lot, then you might consider a program which takes your main .tex file as input and have a comment (for example %replacewithtexs) and then use a regex substitution of that with the output of one of the scripts posted above. That way, you could also have the script just run latex (and bibtex if you use it) as well so it would just be a one step compilation process once you have it set up. Does that make sense? sorry if I'm making things complicated for you-- if so, just go with the scripts that phi generously wipped up for you.

best of luck
Post Reply