# how to use \input{file.tex} correctly?

Suppose I have two latex files file1 and file2 with the same preamble and so on. Then I want to create a file called bigfile which just puts the two files together. This is not working for some reason. Therefore I first tried to include just file1, this is what I have done:

\documentclass .... %% same as file1 ...
same preamble as file1
\begin{document}
\input{file1}
\end{document}


The result is that I not only get file1, but also the preamble and so on. This is my first experience with putting files together in 1 file. Kindly provide me a solution :)

-
Please use the code formatting, as I did in my edit ;-) –  Tobi Jan 5 '12 at 15:09

The best thing is probably to leave out the document preamble and the \begin{document} and \end{document} from the included files and have one master file that sets up the style.

Master file:

\documentclass{article}
% preamble.
\begin{document}
\input{file1}
\input{file2}
\end{document}


First included file:

\section{Blah}
% more stuff.


Second included file

\section{More stuff}
% blah blah

-
@Joseph Thanks for editing. For future reference, if the edit has got something to do with basic errors (not typos) could you please leave a comment with an indication of what I did wrong? That way I can learn and avoid similar errors. It will save you time in the long run:-) –  Marc van Dongen Jan 5 '12 at 15:18
This just looked like a typo, not an error, so did not seem to need it. (It's very easy to miss out or mis-match the odd .) –  Joseph Wright Jan 5 '12 at 15:20
@Joseph Ok. From now on I'll assume that if you don't leave a message, the edit was mainly to fix typos. –  Marc van Dongen Jan 5 '12 at 15:38
@MarcvanDongen: You can find out directly what's been edited by clicking on the "... ago" hyperlink next to "edited". :-) –  Mico Jan 5 '12 at 15:50
@Mico Thanks. I wasn't aware of this option. –  Marc van Dongen Jan 5 '12 at 16:15

As far as I know you can’t do this in a way like this.

Make three files:

% in1.tex
This is my first file only Text and other stuff from
between \verb+\begin{document}+ and \verb+\end{document}+.


% in2.tex
This is my second file it contains only the body stuff
like file 1 did.


% master.tex
\documentclass{minimal}
\usepackage{...}

\begin{document}
This is the master file including all other files.

\input{in1}
\input{in2}
\end{document}


You may add a file called header.tex including all the stuff before \begin{document}

% header.tex
\documentclass{minimal}
\usepackage{...}


And put this in master.tex

% master.tex

\begin{document}
This is the master file including all other files.

\input{in1}
\input{in2}
\end{document}


An alternative for your purpose is maybe the \include command which is made to separate a document in it’s chapters. Before it includes the content it send a \clearpage command. To process all floats from the preceding chapter. With \includeonly{chap1} you can includ only a single file by keeping the whole TOC and \ref information from the ther chapters

-

From your question it seems that file and file are complete standalone files that are intended to be able to be compiled individually. If that is the case then you should consider the standalone package. This needs to be loaded in the bigfile early in the premable, and bigfile needs to include all the packages required by both files.

Here is how I would recommend that you do this:

## mypackages.sty:

While this is not absolutely necessary, I would recommend that you create a mypackages.sty file which has all the packages required by your completed document:

\usepackage{standalone}% Need standalone package
\usepackage{amsmath}%  any others that you use


This file should be your master and include all your customization such as page geometry, margins, etc...

This will simplify maintaining the preambles, and ensure that the files compiled individually are formatted identically when included in the main document.

## file1.tex and file2.tex:

Each of the individual files should look like:

\documentclass{article}
% Preamble of file <n>
\usepackage{mypackages}
\begin{document}
... File n contents here ...
\end{document}


Note that these are complete files and can be compiled by themselves.

## bigfile.tex:

\documentclass{article}
\usepackage{mypackages}

\begin{document}
\input{file1}
\input{file2}
\end{document}


## Notes:

Here is the complete code in one file. Here I have used the filecontents package to be able to include the individual files to produce a compilable example. Below, I have not used a mypackages.sty to show that only the main file needs to include the standalone package.

\documentclass{article}

\usepackage{standalone}
% Complete premable required by all files...
\usepackage{amsmath}

%-----------------------
\usepackage{filecontents}
\begin{filecontents*}{file1.tex}
\documentclass{article}
% Preamble of file 1
\usepackage{amsmath}
\begin{document}
... File 1 contents here ...
\end{document}
\end{filecontents*}

\begin{filecontents*}{file2.tex}
\documentclass{article}
% Preamble of file 2
\usepackage{amsmath}
\begin{document}
... File 2 contents here ...
\end{document}
\end{filecontents*}
%----------------------

\begin{document}
\input{file1}
\input{file2}
\end{document}

-

As a followup to the other answers - I recently had to do something similar in a long scientific proposal. I basically had to assemble a list of biographical sketches of all the collaborators. Each person sent me their individual bio sketch, some as a full standalone tex document, some with just the basics for their individual section and even some as PDF files.

I wrote a simple python script to put all of these together in a single document, but your mileage may vary:

#!/usr/bin/env python
import sys
import re

template_file = "bs.template"  # contains full document preamble
output_file = "bs.tex"

# regular expression that finds content between begin and end document statements
documentRE = re.compile(r".*\\begin{document}(.*)\\end{document}.*",re.DOTALL)

def removeLaTeXPreamble(file):
try:
fh = open(file,'r')
except IOError:
print ":removeLatexPreamble: Couldn't open file %s" % file
sys.exit()

fh.close()

searchResults = documentRE.search(content)
if searchResults is None:
# this is not a complete TeX file, so just input it
return "\input{%s}\n\\newpage\n" % file
else:
# this is a complete TeX file, so only use the section between
# the \begin{document} and \end{document}
return searchResults.group(1).strip() + "\n\\newpage\n"

def make_bios(fileList):

fileList.sort()

try:
fh = open(template_file,'r')
except IOError:
print "Could not open template file %s" % template_file
sys.exit()

fh.close()

for file in fileList:
if file.endswith(".pdf"):
# this is a PDF file, so just includepdf it
output += "\includepdf[pages=-,pagecommand=\\thispagestyle{plain}]{\
%s}\n\\newpage\n" % file
else if file.endswith(".tex"):
# this should be a .tex file, check to see if it is a full document
output += removeLaTeXPreamble(file)
else:
print "I don't understand how to process %s" % file
sys.exit()

output += r"\end{document}"
try:
outFH = open(output_file,'w')
except IOError:
print "Couldn't open file %s for writing" % output_file
sys.exit()

outFH.write(output)
outFH.close()

if __name__ == "__main__":
make_bios(sys.argv[1:])


The bs.template file just contains the LaTeX preamble up to the \begin{document}. You could run this script like python <script_name.py> <your files>`, and a bs.tex file will be created. I know, probably not the best choice of name, bs :-p.

-