13

Have a look at the following example, please.

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents}{my.sty}
   \RequirePackage{xcolor}
\end{filecontents}

\RequirePackage{my}
\usepackage[cmyk]{xcolor}

\begin{document}
   Content
\end{document}

I expect this to compile, but it reports an option clash. I thought there could be only an option clash if I try to load a package with different options and that there's no clash if the package is loaded multiple times and one time with options. Is that wrong?

I know that I could fix this by changing the order of my and xcolor but I can think about cases where I can’t influence the order (e.g. my is loaded internally by the document class).

I found \RequirePackage without option clashes because of the package loading order? where Herbert gave the solution to split the package loading, which won’t work in my case. And I asked catoptions causes option clash with xcolor, which is possibly a duplicate, a few minutes ago …

2 Answers 2

13

The LaTeX2e kernel defines usepackage to be exactly the same as RequirePackage in the ltclass.dtx.

    \if@compatibility\else\let\usepackage\RequirePackage\fi

So the fact that you use RequirePackage makes no difference and you still get an error. If the same package is requested more than once, nothing happens, unless the package has been requested with options that were not given the first time it was loaded, in which case an error is produced.

3
  • Thanks. I kown that \usepackage uses \RequirePackage but I thought that no options won’t cause problems. So did I get you right that there’s no better work around than changing the order of loading packages?
    – Tobi
    Commented Feb 26, 2012 at 10:38
  • @Tobi Unfortunately yes, unless you write your own routine to check if the package is present and then ignore errors. In general though is best to see the clashes.
    – yannisl
    Commented Feb 26, 2012 at 10:44
  • Thanks again. Here Ahmed quotes the error help for the clash which suggests to use global options instead.
    – Tobi
    Commented Feb 26, 2012 at 11:07
4

In the above linked question Ahmed Musa quotes the LaTeX error help on an option clash:

? h
The package 'xcolor' has already been loaded with options:
  [cmyk].
There has now been an attempt to load it with no options. 
Adding the global options:
  'cmyk'
to your \documentclass declaration may fix this.

It solves my problem if I set the options for xcolor via \documentclass

\documentclass[cmyk]{article}
\usepackage{my}
\begin{document}
  Hello World!
\end{document}
4
  • 3
    Another trick is to use \PassOptionsToPackage to pass the option to the package.
    – Seamus
    Commented Feb 26, 2012 at 11:31
  • @Seamus: Yes, I know, thanks. But I don’t like it because it seems inconvenient for me. Are the any pros or cons in preferring the global option way?
    – Tobi
    Commented Feb 26, 2012 at 12:17
  • 2
    Well, if you wanted to pass draft option to one package and final option to another, your way wouldn't work…
    – Seamus
    Commented Feb 26, 2012 at 18:31
  • @Seamus. Ahh … I didn’t think on that case :-)
    – Tobi
    Commented Feb 27, 2012 at 1:48

You must log in to answer this question.

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