# Is there a way to limit the scope of \usepackage?

When I tried to use the breqn package suggested by an answer to the question

It managed to break a whole bunch of unrelated equations (equations that did not use its functionality), including a definition for the control sequence \mf which I had previously defined myself.

Is there a way to limit the scope of a \usepackage declaration so that its (positive and negative) effects are limited to a particular segment of a document?

I have tried putting it inside an environment, but get the following error:

! LaTeX Error: Can be used only in preamble.

-
You can only load a package in the preamble. The scope starts where you import the package and lasts until the end of the document environment. – Marc van Dongen Feb 28 '13 at 4:58
It is probably easier to adjust your definition of \mf instead of trying to load breqn locally. – clemens Feb 28 '13 at 11:16
If you can provide the parts of the code that are failing (a full working example), we might be able to help in fixing the code so everything compiles correctly. – Mythio Feb 28 '13 at 11:31
although the question as phrased has global interest, this case is likely to be specific to breqn, so i added that as a tag. – barbara beeton Feb 28 '13 at 13:24

Perhaps for some small package this could be possible, but the general answer is no. In particular, breqn and its companion package flexisym do very extensive changes to the normal setup of math formulas which are, in some cases, global; therefore, even if it were possible to load them in a group, the changes would be performed also at the outer level. The same is true for many other packages.

It's not to be neglected that many commands that are used in packages are disabled after \begin{document} and this makes quite difficult to cope with the task of loading a package after the document has begun.

-

If instead of using \usepackage you use \input, you can limited the scope of the package. It will require redefining some commands and patching others, but it should be technically possible. For example

\documentclass{article}
\begin{document}
\begingroup
\makeatletter
\renewcommand{\usepackage}[1]{\input{#1}}
\renewcommand{\NeedsTeXFormat}[1]{}
\renewcommand{\ProvidesPackage}[1]{}
\usepackage{fixltx2e.sty}
\makeatother
\endgroup
\end{document}

-
Perhaps this can work for some packages, but there are many commands that are disabled after \begin{document} and that are used in packages, for instance \RequirePackage. Global definitions performed by a package may disrupt the working of the whole system. – egreg Feb 28 '13 at 14:35
@egreg You're very polite. I would have said that this solution looks a bit like calibrating your dosimeter with a hydrogen bomb... – Brent.Longborough Feb 28 '13 at 18:43