[Tex/LaTex] Replace \input{fileX} by the content of fileX automatically

automationinput

Suppose I have a document with multiple include or input statements

\input{fileA}
\input{fileB}

etc.

Is there an easy way to generate a single .tex file where \input{fileA} is replaced by the actual content of fileA etc. without copying it manually?

Best Answer

You can use the following tools to do this. All of them are on CTAN but not all of them are part of either TeXLive or MikTeX, so you may need to manually install them. They need either Perl or a C compiler installed. Both should not be a problem with Linux but might be one under Windows or Mac. However IIRC TeXLive installs its own Perl interpreter.


latexpand Perl script:

Latexpand is a Perl script that simply replaces \input and \include commands with the content of the file input/included. The script does not deal with \includeonly commands.

Installation:

Simply download it from http://mirrors.ctan.org/support/latexpand/latexpand and run it. You need Perl installed however.

Usage:

    perl latexpand mainfile.tex > newfile.tex

flatex

A C program to flatten a LaTeX file into a single file, by explicitly including the files included by \include and \input commands. Also, if BibTeX is being used, then includes the bbl file into the resulting file. The result is therefore a stand-alone LaTeX file that can be emailed to a collaborator.

Installation:

Get the single C file and compile it, e.g. with cc flatex.c -o flatex.

Usage:

    flatex mainfile.tex > newfile.tex

flatten

A program to flatten a LaTeX root file by copying \input and \include files into the root file.

Installation:

Get the ZIP file from CTAN, unpack it and run make followed by make install. You need a C compiler and maybe flex for this. However it seems quite old and you might run in trouble because of it. I failed compiling it!

Usage:

    flatten mainfile.tex newfile.tex