Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
share|improve this question
1  
1  
no, it is a special problem ... –  Herbert Dec 27 '11 at 10:56
2  
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
add comment

2 Answers

up vote 18 down vote accepted

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.

share|improve this answer
1  
You probably meant \@namedef{opt@#2.sty}{#1} and what about an optional argument to \dontusepackage?. –  Werner Dec 27 '11 at 14:56
    
@Werner Yes, fixed. Thanks. –  egreg Dec 27 '11 at 15:04
    
@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
add comment

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
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.