# How to combine several LaTeX files with equations and tables into one?

I am preparing a thesis including equations, figures, and tables. I have read up on combining LaTeX files, but my problem persists. I created the main file and imported preamble, chapterfiles, and bibliography into the main file. Chapter files having tables do not get processed without the required packages, but the declared packages are only in the preamble file and not in the chapter files. On the other hand, if I declare packages in the chapter files, the compiler gives error saying I cannot declare packages before \documentclass{}. But, I have to use only one \documentclass{} right? How to solve this problem? Please help me.

-
Welcome to TeX.sx! To help you it would be usefulif you could also post a Minimal Working Example MWE. This code should include all relevant parts of your problem but should still be Minimal! – Peter Jansson Jan 9 '13 at 12:00
What stops you from putting additional package declarations into the preamble? If you want to be able to process your chapter files separately, you can consider using the standalone documentclass for them. – marczellm Jan 9 '13 at 12:02
You probably need to show what your main file looks like. In particular by import do you mean \import? You can import chapter files but you can not import the preamble documents (you could \input them, but it is more normal to put them explicitly in the main file) – David Carlisle Jan 9 '13 at 12:13

In the main file you can insert subdocuments with \input{subdocument}.

You can also use \include{subdocument}, that roughly is equivalent to \clearpage \input{subdocument} \clearpage (see When should I use \input vs. \include?).

In any case, the subdocument.tex is any chunk of LaTeX code as:

Some \textcolor{red}{text}


It might be any part of the main document content, or the preamble, but not a standalone document as:

\documentclass{article}
\usepackage{xcolor}
\begin{document}
Some \textcolor{red}{text}
\end{document}


This is because \input is just like paste a code into another, and you cannot have a document with two \documentclass declarations, two preambles, etc.

However, you can skip this unnecessary code just loading the package standalone or docmute in the preamble of the main file:

This is a MWE of a main file:

\documentclass{article} % This in the main.tex file
\usepackage{standalone} % load only in the main file
% \usepackage{docmute}  % alternative to standalone

% Tip: put the rest of the preamble in 'preamble.tex'
% so you can reuse in standalone subdocuments

\input{preamble}

\begin{document}
\maketitle
\tableofcontents
\input{SectionOne}   %  Load standalone SectionOne.tex
\input{SectionTwo}   %  Load "\section{Two} Some text ..."
\section{Three}
Lore \input{ipsum}  %  Load simple text of ipsum.tex
\end{document}

-