I have a main TeX file which inputs many other TeX files i.e. for example


Let's call the above file say main.tex I want a single TeX file which imports contents from input1.tex,input2.tex and input3.tex, so that I have only one single TeX file instead of the above four TeX files.

  • 1
    Assuming this is not something you need to do numerous times, can't you just cut and past the contents of the three files into the main file? But, why do you want to have one file in the first place? – Peter Grill Jul 5 '12 at 3:56
  • 1
    @PeterGrill I need to submit an article a journal and I thought it would be good to have a single .tex file. The issue is the following. I have different files in sub-directories in a recursive manner and hence I cannot upload these individual files since the directory structure in the input command is on the local folder on my computer. I could of-course put these manually but I thought if there would be a way out similar to the .bbl file for the bibliography. – user1876 Jul 5 '12 at 5:09
  • Perhaps one of the ideas in tex.stackexchange.com/questions/25713/… will help? – Joseph Wright Jul 5 '12 at 7:43

I'm not sure about Herbert's script "working in one pass for recursive calls." In my interpretation of recursive, I assumed that we would want it to work in one call if one of the input*.tex files themselves contains an \input{} request.

I would have made a comment above to ask about this, but I don't have the reputation.

I created a script with gawk using the same execution syntax:

#! /bin/gawk -f

    file = a[3];
          system("./recursiveInput.awk "file".tex");
          system("./recursiveInput.awk "file);

    print $0

This has the advantages that:

  • it is truly recursive in one-pass (in my interpretation)
  • it avoids the use of the perl chomp() and chop() which produced the undesired effect (in my case) of not faithfully reproducing blank lines (used sometimes in TeX for paragraph delineation)
  • It ignores lines with a % before the \input{} statement, these are lines for which the \input{} command is commented out.

This has the disadvantages that:

  • I only implemented it for the \input{} and not the \include{} statements
  • What is the syntax to use your script? – gbernardi Aug 31 '16 at 12:29
  • be sure to have the executable (with the above code) named recursiveInput.awk and double check that it is executable and you actually have awk! Then execute like $./recursiveInput.awk < main.tex > newmain.tex. Here main.tex is your original tex file with all the \input{} function and newmain.tex is the new file with everything combined. – villaa Sep 14 '16 at 16:22

Here is a Perl script that does that. It replaces all files included via \input and \include statements with their contents. To use it, run:

./buildFile.pl < main.tex > main_all.tex

And here is the script (buildFile.pl):

 #!/usr/bin/env perl

 sub p_inc {
   $DateiName = shift;
     if ( open (my $datei, "$DateiName.tex") ) {
       print "%%%---------- open: ", $DateiName, "\n";
       while (<$datei>) {
         if ((/^\s*\\include\{\s*(\S+)/i) or (/^\s*\\input\{\s*(\S+)/i)) {
           my $include = $1;
           print "%%%%%%%%% jump to ", $include, "\n";
         } else { print; }
       print "%%%---------- close: ", $DateiName, "\n";
       close $datei;
     } else { print "%%%<===== file doesn't exist\n"; }

 @zeilen = (<>);
 for $zeile (@zeilen) {
   next if $zeile =~ /^\s*(%)/;
   if (($zeile =~ /^\s*\\include\{\s*(\S+)/i) or ($zeile =~ /^\s*\\input\{\s*(\S+)/i)) {
     my $include = $1;
     chomp($include); # remove trailing whitespace
     chop($include);  # remove '}' from the matched string
     print "%%%%%%%%%%% jump to ", $include, "\n";
   } else { print $zeile; }
  • 1
    Does this work in one pass for recursive \input/\include calls, or does one need to call it as many times as there are recursive calls? – Werner Jul 5 '12 at 6:20
  • only one call ... – user2478 Jul 5 '12 at 6:32

Although I've never used it, the bundledoc package seems to provide this functionality. From the package description:

The bundledoc package is a post-processor for the snapshot package that bundles together all the classes, packages and files needed to build a given LaTeX document. It reads the .dep file that snapshot produces, finds each of the files mentioned therein, and archives them into a single .tar.gz (or .zip, or whatever) file, suitable for moving across systems, transmitting to a colleague, etc.

A script, arlatex, provides an alternative “archiving” mechanism, creating a single LaTeX file that contains all of the ancillary files of a LaTeX document, together with the document itself, using the filecontents* environment.