When should I use \input vs. \include?
\input{filename}
imports the commands from filename.tex
into the target file; it's equivalent to typing all the commands from filename.tex
right into the current file where the \input
line is.
\include{filename}
essentially does a \clearpage
before and after \input{filename}
, together with some magic to switch to another .aux
file, and omits the inclusion at all if you have an \includeonly
without the filename in the argument. This is primarily useful when you have a big project on a slow computer; changing one of the include targets won't force you to regenerate the outputs of all the rest.
\include{filename}
gets you the speed bonus, but it also can't be nested, can't appear in the preamble, and forces page breaks around the included text.
Short answer:
\input
is a more lower level macro which simply inputs the content of the given file like it was copy&pasted there manually. \include
handles the file content as a logical unit of its own (like e.g. a chapter) and enables you to only include specific files using \includeonly{filename,filename2,...}
to save times.
Long answer:
The \input{<filename>}
macro makes LaTeX to process the content of the given file basically the same way as if it would be written at the same place as \input
. The LaTeX version of \input
only does some sanity checks and then uses the TeX \input
primitive which got renamed to \@@input
by LaTeX.
Mentionable properties of \input
are:
- You can use
\input
basically everywhere with any content.
It is usable in the preamble, inside packages and in the document. - You can nest
\input
macros.
You can use\input
inside a file which is read using\input
. - The only thing
\input
does is to input the file.
You don't have to worry about any side effects, but don't get any extra features.
The \include{<filename>}
macro is bigger and is supposed to be used with bigger amounts of content, like chapters, which people might like to compile on their own during the editing process.
\include
does basically the following thing:
- It uses
\clearpage
before and after the content of the file. This ensure that its content starts on a new page of its own and is not placed together with earlier or later text. - It opens a new
.aux
file for the given file.
There will be afilename.aux
file which contains all counter values, like page and chapter numbers etc., at the begin of the filename. This way the file can be compiled alone but still has the correct page and chapter etc. numbers. Such part aux files are read by the main aux file. - It then uses
\input
internally to read the file's content.
Mentionable properties of \include
are:
- It can't be used anywhere except in the document and only where a page break is allowed.
Because of the\clearpage
and the own.aux
file\include
doesn't work in the preamble, inside packages. Using it in restricted modes or math mode won't work properly, while\input
is fine there. - You can't nest
\include
files.
You can't use\include
inside a file which is read by\include
. This is by intention and is because to avoid issues with the.aux
files. Otherwise three.aux
files (main, parent\include
, child\include
) would be open at the same time which was deemed to complicated I guess.
You can use\input
inside an\include
file and also\input
an\include
file. - Biggest benefit: You can use
\includeonly{filename1,filename2,...}
in the preamble to only include specific\include
files.
Because the state of the document (i.e. above mentioned counter values) was stored in an own.aux
file all page and sectioning numbers will still be correct. This is very useful in the writing process of a large document because it allows you to only compile the chapter you currently write on while skipping the others. Also, if used persistently it can be used to create PDFs of sub-parts of your document, like only the front matter or everything but/only the appendix, etc.
There is also theexcludeonly
package which provides an\excludeonly
to exclude only certain files instead of including all other files.
\input
effectively replaces the command with the contents of the input file. \input
's
can be nested. So, you can write:
\documentclass{article}
\begin{document}
AAA
\input{b}
AAA
\end{document}
where b.tex is:
BBB
\input{c}
BBB
and c.tex is:
CCC
to get output like:
AAA
BBB
CCC
BBB
AAA
include
triggers a newpage both before and after the included material, rather as though you'd used an \input
flanked by \clearpage
commands. include
also supports the \includeonly
mechanism. So, the file:
\documentclass{article}
\includeonly{c}
\begin{document}
AAA
\include{b}
\include{c}
AAA
\end{document}
with b.tex
and c.tex
as before, will produce output with AAA
on page one, CCC
on page two, and AAA
on page 3.
The \include
and \includeonly
pair is very useful for working on long documents: you can \includeonly
the file which you are editing, and compilation is much faster. If you do two runs on the full file before using \includeonly
, page numbers and cross-references will remain valid for the quicker \includeonly
compilation.