0

I like to have custom-color hyperlinks with

\usepackage[dvipsnames]{xcolor}
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=NavyBlue,
    filecolor=magenta,
    urlcolor=cyan,
    citecolor=MidnightBlue
}

and insert a multi-page pdf document with \usepackage{pdfpages}. It works fine (there's no error) when only one of the packages is used.

However, when I included both packages, I got an error that begins with:

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.7 \usepackage {hyperref} The package xcolor has already been loaded with options: [] There has now been an attempt to load it with options [dvipsnames] Adding the global options: ,dvipsnames to your \documentclass declaration may fix this. Try typing <return> to proceed.

I also have multiple other packages, and simply adding \documentclass[xcolor={dvipsnames}]{article} doesn't fix it, but rather causes even more errors.

What causes this? Is there a different package or approach to insert a PDF document that is compatible with hyperref with xcolor=dvipsnames?

alpha
  • 1
  • 3
    Pdfpages loads xcolor too. At some time in the future xcolor will no longer error in such cases but for now use \PassOptionsToPackage. – Ulrike Fischer Nov 24 '22 at 08:23
  • 1
    Thank you! The order of the packages seems to matter but it worked (\PassOptionsToPackage{dvipsnames}{color} has to come after \usepackage[dvipsnames]{xcolor} but before \usepackage{pdfpages}). – alpha Nov 28 '22 at 23:34

2 Answers2

1

The error your report is unrelated to hyperref. Surely you have a situtation like this

\usepackage{pdfpages}
\usepackage[dvipsnames]{xcolor}

To fix this do

\usepackage[dvipsnames]{xcolor}
\usepackage{pdfpages}

As other packages may load xcolor move the \usepackage[dvipsnames]{xcolor} earliest possible in preamble.

user691586
  • 1,988
0

It works by adding \PassOptionsToPackage{dvipsnames}{xcolor} before \usepackage{pdfpages} and after \usepackage[dvipsnames]{xcolor} and \usepackage{hyperref}; this solution is based on Ulrike Fischer's comment.

Like this:

\usepackage[dvipsnames]{xcolor}
\usepackage{hyperref}
\hypersetup{...}

\PassOptionsToPackage{dvipsnames}{xcolor} \usepackage{pdfpages}

alpha
  • 1
  • I'm somewhat surprised that worked. Usually, \PassOptionsToPackage should come before the package is loaded (people will often put it before \documentclass just to be sure). BTW, hyperref should be loaded last, with few exceptions: https://tex.stackexchange.com/q/1863/107497 – Teepeemm Nov 29 '22 at 01:29