15

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?

1
  • The \clearpage that is mandatory in \include has no effect in the preamble`
    – user31729
    Jul 7, 2016 at 13:39

2 Answers 2

16

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!

17

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.