Why am I able to \include in preamble?
There are lots of incorrect latex input constructs that don't actually give an error, they just do whatever they do by accident and luck. \include
only does anything useful if used after the preamble, if used before it probably works like an inefficient version of \input
and you manage to avoid an error just the way it works out, but no part of the behaviour in that case is designed, it's just accidental expansion of incorrect input.
See for example
\@input{bar.aux}
being written to the console, in the output you show, that is intended to be written to the main aux
file but it can not be as the aux file is not opened until \begin{document}
. As it happens, writing to an unopened stream is not an error in tex, it just writes to the console, but it is an error in all but name here.
The \include
command uses \clearpage
at the beginning, however there's
no page build up so far in the preamble, so it has no effect.
Here's some code from the latex.ltx
file (shortened!)
\def\include#1{\relax
\ifnum\@auxout=\@partaux
\@latex@error{\string\include\space cannot be nested}\@eha
\else \@include#1 \fi}
\def\@include#1 {%
\clearpage
%.... much more ...
}
Since \title{foo}
does only define (better set) another macro there is no typesetting content in the file used for \include
.
This little document is effectively doing the same:
\documentclass{article}
\title{foo}
\clearpage% Does nothing here
\begin{document}
\clearpage% Does nothing here, since there is no page yet!
\maketitle
Foo
\end{document}
But as soon as typesetting occurs (i.e. content that builds up a page) the \include
operation must fail in the preamble, as if you would have written the content in the preamble directly.
So, don't use \include
in preamble at all.
Of course, having typesetting code in another file and using \input
of that file in the preamble the error would be the same one!