# Generating a single TeX file by merging different TeX files

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

\documentclass{article}
\begin{document}
\input{input1.tex}
\input{input2.tex}
\input{input3.tex}
\end{document}


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.

-
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

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.

-

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; chomp($include);chop($include); print "%%%%%%%%% jump to ",$include, "\n";
p_inc($include); } 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;
chomp($include); chop($include);
print "%%%%%%%%%%% jump to ", $include, "\n"; p_inc($include);
} else { print \$zeile; }
}

-
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