# How does \include work in terms of preamble? And is \input better?

When you have different files; say different chapters of a book, that are written in separate Tex files, all having their own preamble packages and document class options, how does it work when you \include them in one file with its own preamble options. Do they use the root file preamble alone for things like \title or a4paper,openleft options? Do different options and packages add up from different files?

I also understand that \input uses the preamble of the root file alone, is this true? and if so, would that make it better than \include; as in easier to manage?

• \include and \input don't differ as regards the presence of a document preamble. In a file that's read in with either \input or \include, there mustn't be a \begin{document} or a \documentclass command (with one exception, but it's not important), unless you use the facility which Counter Terrorist pointed to. – egreg Jan 13 '12 at 16:43
• docmute package and combine documentclass might be useful as well. – kiss my armpit Jan 13 '12 at 16:57
• Possibly related: tex.stackexchange.com/questions/40144/… – cm2 Jan 13 '12 at 18:45
• possible duplicate: tex.stackexchange.com/questions/246/… – matth Jan 31 '12 at 17:42

Certain commands are only allowed in a document preamble. For example, if you use \usepackage{<package>} within the document environment, LaTeX conveniently "suggests" the obvious:

LaTeX Error: Can be used only in preamble.

And, since both \input{<file>} and \include{<file>} do very similar things - including the contents of <file> in your document (the latter is a little more elaborate) - you can't include a full-fledged or stand-alone document with its own document class and preamble via \input or \include without some help. Nor will preambles be interlaced sequentially. The difference is explained in When should I use \input vs. \include?

Your only solution is to use a package or document class that can handle (circumvent) this restriction. Options include the subfiles package (not distributed with TeX Live), combine or the standalone package/class.

A good place to start is Make a .tex file that combines complete .tex documents in subdirectories. The main idea is that you have a master (or main) file that covers all the sub-file preambles and requirements, since the sub-file preambles will be lost at the time of inclusion.

• So you could say that one could use \include to separate different included "chapters", add repeatedly used commands with \insert, use the standalone package to strip all preambles from sub-files OR use subpreambles to combine all the preambles from the sub-files into the root file. Right? – Ayman Elmasry Jan 13 '12 at 17:14
• @AymanElmasry: You should read the standalone documentation and/or the subfiles documentation and follow the instructions given in either, depending on your choice. What do you use \insert for? – Werner Jan 13 '12 at 17:53
• Oops, I meant \input. Sorry. – Ayman Elmasry Jan 13 '12 at 19:36