[Tex/LaTex] How does \include work in terms of preamble? And is \input better

chaptersincludepreamble

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?

Best Answer

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.

Related Question