[Tex/LaTex] Package options and \RequirePackage: order of commands and option conflicts

incompatibilitypackage-optionspackages

How do the options of a \RequirePackage command interact with options specified for that same package elsewhere?

Here is an example: The pdfx package, if loaded with the a-1b-option, executes the command \RequirePackage[pdftex,pdfa]{hyperref}. However, later in my LaTeX source file, I might also execute the command \usepackage[pdfa=true,linkbordercolor={0 0 1}]{hyperref}. Where in my document should I place the \usepackage[a-1b]{pdfx}-command so that it doesn't cause any conflicts with my hyperref-command? Does the fact that pdfx's \RequirePackage-command loads different options from the ones I am specifying in my \usepackage[...]{hyperref}-command cause problems?

Relevant:

Best Answer

Basically the package is only loaded once, with the options specified at that point. Any later uses of \RequirePackage or \usepackage are silently ignored if the options specified were specified earlier, or generate an error otherwise. Normally it works to put options on \documentclass, so they are global options seen by all packages.


The help text for the option clash error suggests placing the clashing package in the global options on \documentclass but that in fact isn't very good advice. (One might consider that a latex bug). What does work is to use something like

\documentclass{article}


\PassOptionsToPackage{pdfa=true,linkbordercolor={0 0 1}}{hyperref}
\usepackage[a-1b]{pdfx}
\usepackage[pdfa=true]{hyperref}

\begin{document}
a
\end{document}

\PassOptionsToPackage doesn't actually load the package but does arrange that if it is loaded it will get those options plus any others explicitly listed when it is loaded. So if you suspect that one or more of your packages is internally loading a package and you want to make sure that it has certain options you can use this method. In the particular case of hyperref the alternative suggested in mhp's anwer is also good, avoid the package option and use the hypersetup mechanism instead. However not all packages provide a means to set options after the package is loaded in that way.