Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Suppose I have a directory containing a bunch of LaTeX source files, and I want to input all of them into a single main file. I could do this by saying

\input{dir/file1.tex}
\input{dir/file2.tex}
\input{dir/file3.tex}
...

Is there some way to input all of files in the directory "dir" using a single command? This is useful, for example, if I want to keep adding files to the directory "dir" but don't want to have to remember to add further \input commands to the main file.

share|improve this question
    
This might be possible if TeX/LaTex had a system interface. Then one could query the directory for all files, and then programatically loop over all the files, calling \input on each one. But I don't know what the support for such things is. Wild suggestion - luatex? –  Faheem Mitha Mar 20 '11 at 9:28
    
This appears to be a near-duplicate of tex.stackexchange.com/questions/7653/… –  Faheem Mitha Mar 20 '11 at 9:39
    
And one of the answers uses Luatex, namely tex.stackexchange.com/questions/7653/… –  Faheem Mitha Mar 20 '11 at 9:41
add comment

2 Answers

up vote 14 down vote accepted

My suggestion is to create one file with all the \input lines automatically and use \input to include this file (and hence, all desired files) into your main document. The best way to keep everything up to date would be a Makefile. On a Unix system you can use this command line to create the file with the input lines:

ls dir/*.tex | awk '{printf "\\input{%s}\n", $1}' > inputs.tex

You would only update inputs.tex once in a while (or automatically) but always use \input{inputs.tex} in your main document. The beauty of using ls (or find) is that you can combine naming some files explicitly and others using shell (glob) patterns to control what files are included in what order.

Makefile

The GNU Make Makefile rule to do this automatically would look like this:

.PHONY: all
all:   inputs.tex

inputs.tex: $(wildcard dir/*.tex)
  ls dir/*.tex | awk '{printf "\\input{%s}\n", $$1}' > inputs.tex

A call to make would trigger updating the all target, which in turn would update inputs.tex if any of the included TeX files has changed or a new one was added.

Shell Escapes

Reading about shell escapes in PDFLaTeX in the solution mentioned by Faheem Mitha, here is another idea: one could also execute the command line mentioned above from within the LaTeX source file and read \input{inputs.tex} after that. However, I haven't tried this.

share|improve this answer
    
Parsing the output of ls like this is very bad. It will break if a filename contains e.g., a space! In bash it can be handled very neatly with a loop: { shopt -s nullglob; for file in dir/*.tex; do echo "\\input{$file}"; done; } > inputs.tex. Otherwise it can be handled with make: Replace the outrageous line with, e.g.: @{ $(foreach file,$(wildcard dir/*.tex),echo "\input{$(file)}";) } > inputs.tex –  gniourf_gniourf Mar 20 '11 at 16:47
1  
@gniourf_gniourf: \input has trouble with spaces in file names. So you would have to do more -- see this question. I intended my answer more as a general idea rather than a perfect solution. –  Christian Lindig Mar 20 '11 at 18:06
1  
@ChristianLindig I had to change your $1 to $$1 in order for make to not think that the $1 is a variable that needs expansion. –  g33kz0r Apr 8 '12 at 20:57
1  
@g33kz0r You are right, it must be $$1 in the Makefile. Fixed it. –  Christian Lindig Apr 11 '12 at 7:53
add comment

You could use package bashful to do it from within LaTeX, e.g., using gniourf_gniourf suggestion, you would write

\documentclass{minimal}
\usepackage{bashful}
\begin{document}
\bash[stdoutFile=inputs.tex]
{ shopt -s nullglob; for file in dir/*.tex; do echo "\\input{$file}"; done; } 
\END
\input{inputs.tex}
\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.