Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers 3

up vote 37 down vote accepted

You can use the following tools to do this. All of them are on CTAN but are not part of either TeXLive nor MikTeX, so you 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
share|improve this answer
    
Great! Thanks!! –  student Jun 28 '11 at 19:34
3  
I wanted a Python version of this for my own paper building toolchain so I created one: github.com/johnjosephhorton/flatex –  John Horton May 12 '12 at 16:18
1  
@JohnHorton: Good work! You should consider uploading it to CTAN as well. Just get into contact with the author of the original flatten. I don't think he mind having the Python version in the same directory. –  Martin Scharrer May 12 '12 at 16:36
1  
Meanwhile, latexpand seems to be part of TexLive (confirmed with v2012) and also of MikTex –  Hotschke Apr 4 '13 at 8:48
    
@JohnHorton when I tried to use your script on windows I got some errors. I added couple of lines to your script and gave you a pull request on github. –  Atilla Ozgur May 23 '13 at 21:57
show 4 more comments

You need an installed Perl. Save it as buildFile.pl and make it executable or run it with perl buildFile.pl

eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' && eval 'exec perl -S $0 $argv:q'
  if 0;
#use strict;
# 
# Usage:
# buildFile.pl < main-file.tex > main-file.tot
#     
sub p_inc {
  $DateiName = shift;
    if ( open (my $datei, "$DateiName.tex") ) {
      print "%%%---------- open: ", $DateiName, "\n";
      while (<$datei>) {
        if (/^\s*\\include{\s+(\S+)/i) {
          my $include = $1;
          chomp($include);chop($include);
          print "%%%%%%%%% Springe nach ", $include, "\n";
          p_inc($include);
        } else { print unless /^\s*(#|$)/; }
      }
      print "%%%---------- close: ", $DateiName, "\n";
      close $datei;
    } else { print "%%%<===== Datei existiert nicht\n"; }
}
#
@zeilen = (<>);
for $zeile (@zeilen) {
  next if $zeile =~ /^\s*(%)/;
  if ($zeile =~ /^\s*\\include{\s*(\S+)/i) {
    my $include = $1;
    chomp($include);
    chop($include);
    print "%%%%%%%%%%% Springe nach ", $include, "\n";
    p_inc($include);
  } else { print $zeile; }
share|improve this answer
add comment

I tried all the options mentioned in this post, but the original flatex works best for me. There appears to be a newer version, but I was unable to compile it as I am not familiar with multi-file C sources. If anyone could try it and let me know about the outcome that would be great.

The original flatex has an issue as it only creates a .flt (the flattened document), even when you specify a different .tex as the output. But that's not really an issue. I have created a batch file that sorts that out and allows integration with Subversion as well (see this post on my website http://www.jwe.cc/2012/02/workflow-with-subversion-and-latex/).

share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.