22

What would happen when you load a package more than once, say via \usepackage{<package>} or \RequirePackage{<package>}.

For example:

\documentclass{book}

\usepackage{geometry}
\usepackage{geometry}

\begin{document}
test
\end{document}

Will LaTeX ignore the double loading of geometry, or are there some problems and repercussions?

1

1 Answer 1

26

No, LaTeX won't load geometry a second time.

The first lines in the package are

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{geometry}[2010/09/12 v5.6 Page Geometry]

and their purpose is not only to announce the package.

Every time LaTeX loads a package, say foo.sty, it does an important assignment:

\@namedef{[email protected]}{...}

where ... stands for the contents of the optional argument to \ProvidesPackage, if present; otherwise it's empty. What's important, though, is that

\[email protected]

is defined (it's not possible to access directly that macro). Moreover, it stores the options passed to the package in another macro

\[email protected]

What's the purpose of these macros? When

\usepackage[<options>]{foo}

is found, LaTeX first checks whether \[email protected] is defined. If it isn't, it loads foo with the given options. If it is, LaTeX doesn't load foo and simply checks whether the option list given now is a subset of the option list it finds stored in \[email protected]. If it is, LaTeX does nothing; otherwise it raises an error

! LaTeX Error: Option clash for package foo.

so the user is warned about the problem.

The only package that's allowed to be loaded multiple times is fontenc. Of course a package could use the same trick as fontenc, but this is strongly discouraged and no package I know of tries this devious behavior.

Some packages that are expected to be called by classes usually have a "setup" command, so that one can change the settings the class may have made without having to load the package with different options, which would be impossible. Examples are caption and geometry.

Some classes allow passing options to the package they load. For instance, one can say

\documentclass[xcolor={svgnames}]{beamer}

in order to pass the svgnames option to xcolor. When this isn't supported, there's always the possibility of saying, before \documentclass,

\PassOptionsToPackage{<options>}{<package>}

where <package> is the package we need to pass the <options> to and is loaded by the class.

2
  • What do you mean "it's not possible to access directly that macro" (\[email protected]), when fontenc.sty does just that? Or by "directly" do you mean without using \csname? Commented Dec 24, 2012 at 8:04
  • @AriBrodsky \[email protected] can be obtained either with assignments (catcode, lccode or uccode) or with \csname. The first method is not fully expandable, the second one is.
    – egreg
    Commented Dec 24, 2012 at 9:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .