# Using \BeforePackage to load a package before hyperref does not work

I am using the lipics document class, which includes hyperref. I want to load the float package, which should be loaded before hyperref, without changing the document class. As suggested in this comment, I am trying to use the KOMA-Script called scrlfile (cf. page 199 of the documentation), in the following way

\documentclass{lipics}
\usepackage{scrlfile}
\BeforePackage{hyperref}{
\usepackage{float}
}
...


however it never loads the package.

What I am doing wrong?

-
Try \RequirePackage{scrfile}\BeforePackage{hyperref}{\usepackage{float}}\documentcl‌​ass{lipics}  –  Marco Daniel Apr 7 '12 at 16:49
There is something really odd. It rise an warning You have requested package hyperref', but the package provides float'. and the error Command \c@float@type already defined. However, if I do not put anything and try to use some commands from the package float (for example \floatstyle{boxed} \restylefloat{figure}) I get the undefined commands error. I am a bit lost. –  Alejandro Díaz-Caro Apr 7 '12 at 20:47

The problem here is that you need to call \BeforePackage{} and related commands before the package is actually loaded so that the hooks can work. This is what Marco's suggestion tries to do.

Unfortunately, you cannot load an additional package using \BeforePackage{} because, when lipics finally requests hyperref, loading float will call the \ProvidesPackage{float} declaration and LaTeX thinks that the wrong package is being provided.

I have found that it is best to use \AfterPackage or \AfterClass. Why not just load the float package right after the article documentclass that lipics uses?

\RequirePackage{scrlfile}
\AfterClass{article}{
\RequirePackage{float}
}

\documentclass[english]{lipics}
\begin{document}
Hi
\end{document}


If you really want to make \BeforePackage{} work, you can use the following hack

\BeforePackage{hyperref}{
\let\oldProvidesPackage\ProvidesPackage   % Save original definition
\def\ProvidesPackage#1[#2]{               % Redefine (simply)
\oldProvidesPackage#1[#2]               % Call original \ProvidesPackage
\let\ProvidesPackage\oldProvidesPackage % restore original definitions
\let\oldProvidesPackage\relax


This temporarily redefines \ProvidesPackage to first call the original version (which will tell LaTeX that hyperref is being provided as expected), then to load float` after restoring the old definitions.