[Tex/LaTex] How to “un-use” a package

packages

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?

Best 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.