TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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
@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
share|improve this answer

here is a perl script which can build one TeX file. All \input and \include files are read and inserted into the main file. Use it

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

the perl program buildFile.pl:

#!/usr/bin/env perl    
### change the
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 unless /^\s*(#|$)/; }
      print "%%%---------- close: ", $DateiName, "\n";
      close $datei;
    } else { print "%%%<===== file soesn'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;
    print "%%%%%%%%%%% jump to ", $include, "\n";
  } else { print $zeile; }
share|improve this answer
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 ... – Herbert 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.

share|improve this answer

Your Answer


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