[Tex/LaTex] How to circumvent ‘Option clash for package …’ error

package-options

I use a package foo that requires a package bar with some options. Now, assume that I need bar to be loaded with a different set of options. Unfortunately, when I try to load bar myself I usually end up with an Option clash for package bar LaTeX error. Minimal example:

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents}{foo.sty}
\ProvidesPackage{foo}
\RequirePackage[noisy]{bar}
\end{filecontents}

\begin{filecontents}{bar.sty}
\ProvidesPackage{bar}
\DeclareOption{noisy}{\PackageWarning{bar}{I'm noisy}}
\DeclareOption{silent}{\PackageWarning{bar}{I'm silent}}
\ProcessOptions*
\end{filecontents}

%% \usepackage[silent]{bar}

\usepackage{foo}

\usepackage[silent]{bar}

\begin{document}

\end{document}

What can I do?

Best Answer

In an ideal world, the LaTeX message Option clash for package … simply indicates an error and, as such, should not be ignored, but entail appropriate measures. Nonetheless, I’ve made the experience that, in reality, there are situations where it is either harmless or even advantageous to circumvent this error. Versatile tools for this purpose are \PreventPackageFromLoading and \ResetPreventPackageFromLoading from the scrlfile package. Minimal example:

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents}{foo.sty}
\ProvidesPackage{foo}
\RequirePackage[noisy]{bar}
\end{filecontents}

\begin{filecontents}{bar.sty}
\ProvidesPackage{bar}
\DeclareOption{noisy}{\PackageWarning{bar}{I'm noisy}}
\DeclareOption{silent}{\PackageWarning{bar}{I'm silent}}
\ProcessOptions*
\end{filecontents}

\usepackage{scrlfile}

\PreventPackageFromLoading{bar}

\usepackage{foo}

\ResetPreventPackageFromLoading

\usepackage[silent]{bar}

\begin{document}

\end{document}
Related Question