[Tex/LaTex] How does \usepackage differ from a simple \include or \input

package-writing

What does \usepackage do that is different than \input or \include? Does it simply import the commands in the package file to the current document or is it more complicated?

Best Answer

More complicated. \usepackage:

  • can check that the package is newer than a specified version date
  • can have options specified for loading the package
  • can inherit options from the \documentclass line
  • only loads .sty files
  • checks that it is only called after \documentclass and before \begin{document}
  • ensures catcodes are correct for LaTeX package syntax (\makeatletter/\makeatother) even in the presence of expl3 catcode environments
  • performs assorted housekeeping, including adding the package details to the results given by \listfiles
  • doesn't load the definitions of a package more than once
  • checks that if a package is loaded more than once, the same options have been requested

Whereas \input simply starts reading the file requested as if it had literally been pasted at that point in the document. It can be used to input document text and/or preamble code, for example.

\include is only suitable for inserting document text after \begin{document}, and in fact is geared quite specifically around being used for chapters or parts of document. It performs addition actions over \input, such as starting a new page (with \clearpage) and changing the .aux file so that \includeonly produces correct results.

Related Question