24

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}
\unloadpackage{mypackage}
\usepackage{myotherpackage} %myotherpackage can only be loaded when mypackage is not present
24

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
19

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
  • \documentclass{article} \makeatletter \@namedef{ver@subfig.sty}{}% a fake for a "loaded" package \makeatother \usepackage{caption,subcaption} \begin{document} A \end{document} leads to : ! Package subcaption Error: This package can't be used in cooperation. Of course it works if I remove the "fake" loaded package, but in my preamble.tex something is loading subfig and I want to use subcaption. Any suggestions? – Huckleberry Febbo Nov 9 '18 at 4:48
  • turns out the svg package loads subfig – Huckleberry Febbo Nov 9 '18 at 5:16
  • \@namedef{ver@subfig.sty} fools the system that it is loaded, which leads package subcaption to the message. – user2478 Nov 9 '18 at 19:55

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.