1

I have a folder containing 95 tex files. I need to create a tex file all.tex that contains the following code:

\documentclass[10pt]{article}
\begin{document}
\input{file1.tex}
\input{file2.tex}
...
\input{file95.tex}
\end{document}

The file names do not have any pattern and hence I cannot use a loop. The above is just a working example.

I tried the following on bash:

printf '%s\n' *.tex | paste -sd "\n" - > all.tex

That creates an all.tex file that looks like this:

file1.tex
file2.tex
...
file95.tex

How do I add \input{} around the filenames? And how do I add the preamble etc.?

I am using bash (on Ubuntu) on Windows 10.

1

4 Answers 4

4

In a simple shell script, using printf and simple wildcard expansion:

#!/bin/sh

printf '%s\n' '\documentclass[10pt]{article}' \
              '\begin{document}'

printf '\input{%s}\n' *.tex

printf '%s\n' '\end{document}'

You would call that script and redirect the output, such as:

/path/to/script > /path/of/output.tex

On a single command-line, you might group the prints together for a single redirect:

{ printf '%s\n' '\documentclass[10pt]{article}' \
              '\begin{document}'
printf '\input{%s}\n' *.tex
printf '%s\n' '\end{document}'
} > /path/of/output.tex
6
  • I'm curious, why didn't you apply the output redirection directly in the script ?
    – AdminBee
    Nov 11, 2019 at 15:24
  • You certainly could, if you always had the same filename; I left it open to be more in line with a typical UNIX filter.
    – Jeff Schaller
    Nov 11, 2019 at 15:25
  • Valid point, although you could still call the script with a parameter for the filename ;)
    – AdminBee
    Nov 11, 2019 at 15:27
  • TMTOWTDI!
    – Jeff Schaller
    Nov 11, 2019 at 15:29
  • Thanks Jeff. Here's a dumb question. I am failing to execute the shell script (I only ever run straight from the command line but this time I need to run it from a script). I copy-pasted your code into a combine.sh (both with and without #!/bin/sh) and then ran sh combine.sh > all.tex. I get : not found 1: combine.sh \begin{document}. Nov 11, 2019 at 15:38
0

Probably not the best, but here's a solution using sed to edit your all.tex file:

sed -e 's/^/\\input\{/g' -e 's/$/\}/g' all.tex
0

A regular Ubuntu by default has perl installed so you can do it like this:

ls *tex | perl -pne 's/(.*)/\\input{$1}/' > allfiles.tex

It could be that it's not installed yet on Ubuntu on Windows, if this is the case apt-get install perl-base will do the trick.

EDIT: If perl is not available and you don't want to install it, you could also use sed:

ls *tex | sed 's/\(.*tex\)/\\input{\1}/' > allfiles.tex

(But I always recommend the more powerful perl)

0

Using GNU awk:

awk '
    BEGIN     { print "\\documentclass[10pt]{article}"
                print "\\begin{document}" }
    BEGINFILE { printf("\\include{%s}\n", FILENAME); nextfile }
    END       { print "\\end{document}" }' *.tex

This would produce a list such as the one you show, with the files listed in the order that the shell expands the filename globbing pattern *.tex.

For files named file1.tex through to file95.tex, this will not be in numerical order. For that, you may want to switch over to the zsh shell and use the file<1-95>.tex globbing pattern instead, or the more relaxed pattern *.tex(n).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .