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?

-
–  Werner Oct 25 '12 at 20:42

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{ver@foo.sty}{...}


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

\ver@foo.sty


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

\opt@foo.sty


What's the purpose of these macros? When

\usepackage[<options>]{foo}


is found, LaTeX first checks whether \ver@foo.sty 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 \opt@ver.sty. 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.

-
What do you mean "it's not possible to access directly that macro" (\ver@foo.sty), when fontenc.sty does just that? Or by "directly" do you mean without using \csname? –  Ari Brodsky Dec 24 '12 at 8:04
@AriBrodsky \ver@foo.sty 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 Dec 24 '12 at 9:30