Is it possible to unload a LaTeX package?

I have a preamble file which I use for most documents. Now suppose I want to use a preamble file `preamble.tex` and explicitly don't want to use one of the packages loaded in it. For example in this file there is a line:

``````\usepackage{mypackage}
``````

and then I want to have a file `test.tex` which contains something like this:

``````\input{preamble}
\usepackage{myotherpackage} %myotherpackage can only be loaded when mypackage is not present
``````
-
–  Torbjørn T. Dec 27 '11 at 10:25
no, it is a special problem ... –  Herbert Dec 27 '11 at 10:56
No, you can't unload any packages. TeX does not have namespaces so it isn't possible to know what macros got defined by which package. –  Martin Scharrer Dec 27 '11 at 11:27

You can fool LaTeX into believing that it has already loaded package `foo` with options `bar` and `baz` by defining a command with name "`ver@foo.sty`" and one with name "`opt@foo.sty`" in a suitable way; so

``````\makeatletter
\newcommand{\dontusepackage}[2][]{%
\@namedef{ver@#2.sty}{9999/12/31}%
\@namedef{opt@#2.sty}{#1}}
\makeatother
``````

will prevent both loading the package and also keep LaTeX happy with the options possibly passed to it.

However, `\dontusepackage[bar,baz]{foo}` must be issued before `\input{preamble}`.

I'm in general contrary to "do-it-once-and-for-always" preambles, which raise problems such as this one. There's no such thing as a universal preamble and later developments make some packages obsolete or at least superseded by new ones.

Just copy `preamble.tex` into the working directory and change it for what's needed for the current document you're writing.

-
@egreg I think it is useful to emphasize that, to avoid option clash later, `bar` and `baz` must be the only options for package `foo`. This will require knowing all the options of `foo`. This also assumes that `foo` wasn't loaded before `\dontusepackage`. –  Ahmed Musa Dec 27 '11 at 19:25
I've chosen to pass also the options used in `preamble.tex` in order to raise an error if some other package tries to load it with different options. However it's not that important. –  egreg Dec 27 '11 at 20:21

Tell your preamble that `mypackage` is already "loaded". Packages are not loaded twice:

``````\makeatletter
\@namedef{ver@mypackage.sty}{}% a fake for a "loaded" package
\makeatother
\input{preamble}
\usepackage{myotherpackage} %myotherpackage can only be loaded when mypackage is not present
``````
-
I know this old, but could one not achieve something with boolean variables that would affect the loading or not loading of packages? –  Louis Aug 3 '12 at 8:56