12

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?

2 Answers 2

7

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}
2
  • 1
    Why don't you simply put \PassOptionsToPackage{silent}{bar} before \documentclass{article}?
    – user11232
    Commented Jun 15, 2012 at 0:08
  • @HarishKumar: See my comments on the answer given by Daniel E. Shub.
    – mhp
    Commented Jun 15, 2012 at 6:24
2

What about declaring silent as a global option with \documentclass[silent]{article} and then just using \usepackage{bar} in your document. The silent option will still be loaded because of the global option.

3
  • This only works if the silent and noisy options complement each other, but doesn’t work if they are conflicting in some way.
    – mhp
    Commented Jun 14, 2012 at 14:48
  • @mhp In that case you really have a clash. Hopefully the bar package is well enough designed to handle requests for conflicting options. The foo package should not be loading the bar package with options that it doesn't require.
    – StrongBad
    Commented Jun 14, 2012 at 14:55
  • If you are lucky … ;-) In my experience, there are nowadays quite some opportunities where it is advantageous to outsmart the LaTeX2e option system.
    – mhp
    Commented Jun 14, 2012 at 17:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .