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 :)

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

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}
• I love this idea, but am finding it difficult to implement: could you point me in the direction of resources for implementing? e.g. what is the format of mypackages.sty? & where should it be stored? I think I've successfully installed the standalone package (though am using ST3 which doesn't seem to interface well with CTAN). – Rax Adaam Apr 19 '17 at 16:06
• @RaxAdaam: I suggest you install the latest TeXLive2016 and update all the packages. Then, put .sty files in local directory (where your document is) and make sure things work. Then, Documents in different directories calling the same .sty files and How do I add a .sty file to my MacTeX/TeXShop installation? may help you further. If you get stuck, and can't locate an answr here, it is best to post a question, perferrable with a a fully compilable MWE. – Peter Grill Apr 19 '17 at 21:32
• thanks for the links, hopefully I can piece it together from what you've provided here & what I can find there. I couldn't give up ST3 unless TeXLive boasted the same incredible features - just a matter of figuring out the set up. Again, thank you! – Rax Adaam Apr 20 '17 at 1:21
• It worked...linux-latex 2018 – santimirandarp Nov 5 '18 at 5:12

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}

\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

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.