# How to “un-use” a package?

When using packages in LaTeX (or XeLaTeX, in my case), I load packages with \usepackage[options]{package}, but how can I "un-use" the package later in the document?

Is that even possible?

-
What is the intention of you question? So far every global defined command is used in the whole document. So you have to set every command or the relevant command to undefined. –  Marco Daniel Nov 10 '11 at 10:02
No this is not possible. You can naturally overwrite definitions and settings made by a package. –  Ulrike Fischer Nov 10 '11 at 10:02

## 1 Answer

No, this is not really possible. (La)TeX doesn't now namespaces and this makes it impossible to know which macros got defined by which packages. Also some packages might redefine existing macros and the old definition can't be restored. Same is for changed length or count registers.

It might be possible to use \usepackage inside a group (\begingroup\usepackage[..]{..} \dosomething \endgroup) to keep the package changes local, but there is absolutely no guarantee that this works without problems. Anyway, it isn't good programming style.

However, you can redefine or even undefine macros defined by the package, but this must be done manually for every macro. You can mark the package as not loaded by using \expandafter\let\csname ver@<name>.sty\endcsname\relax, but doing so hardly makes sense.

-
Note that \usepackage can't be used after \begin{document}, but \begingroup\makeatletter\input{package.sty}\endgroup could be used as substitute; however no option can be passed. –  egreg Nov 10 '11 at 11:45
@egreg: So you'd have to trigger the options manually, e.g. set the switch or execute code like the option would? (It's probably more complicated for complex packages.) –  doncherry Nov 10 '11 at 14:23
@doncherry Actually no: standard packages can't be used that way, because \ProvidesPackage is not allowed after \begin{document}. It works only for a very limited number of them (nath.sty, for example). –  egreg Nov 10 '11 at 14:33