[Tex/LaTex] able to \include in preamble

includepreamble

The answer at When should I use \input vs. \include? mentions:

\include 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.

But I have written an example that seems to contradict this.

foo.tex

\documentclass{article}
\include{bar}
\begin{document}
\maketitle
Foo
\end{document}

bar.tex

\title{Bar}

I could compile foo.tex successfully and I also see the title rendered in the generated PDF when I view it with a PDF reader.

$ pdflatex foo.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2015/dev/Debian) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./foo.tex
LaTeX2e <2014/05/01>
Babel <3.9l> and hyphenation patterns for 2 languages loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
\@input{bar.aux}
(./bar.tex)
No file foo.aux.

LaTeX Warning: No \author given.

[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./foo.aux) )</usr/share
/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb></usr/share/texliv
e/texmf-dist/fonts/type1/public/amsfonts/cm/cmr12.pfb></usr/share/texlive/texmf
-dist/fonts/type1/public/amsfonts/cm/cmr17.pfb>
Output written on foo.pdf (1 page, 24969 bytes).
Transcript written on foo.log.

Why was I able to \include in the preamble when the answer I have linked to says it should not be possible?

Best Answer

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!