8

I'm developing a small LaTeX library that requires hyperref to work, but I'm facing 2 problems:

  1. I think that hyperref should usually be loaded at the very end. However, if I put a \RequirePackage{hyperref} in my library, will it be loaded at the beginning? If the user also adds \usepackage{hyperref} at the end, will it solve the problem, or will it be ignored as hyperref was already loaded before?

  2. When the user adds options to the hyperref package, it breaks with an error:

    ERROR: LaTeX Error: Option clash for package hyperref.

So what is the proper way to say "my package requires hyperref", but still allow the user to overwrite the hyperref options/make sure it's loaded last?

Thanks!

MWE:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}[overwrite]{mypackage.sty}
  \NeedsTeXFormat{LaTeX2e}
  \ProvidesPackage{mypackage}[2020/07/13 A dummy package]
  \RequirePackage{hyperref}
\end{filecontents}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[francais]{babel}

\usepackage{mypackage}
\usepackage[colorlinks]{hyperref}

\begin{document}
Hello
\end{document}
3
  • This is ultimately a limitation of LaTeX. You may find this helpful: texfaq.org/FAQ-optionclash
    – Don Hosek
    Jul 13, 2020 at 15:22
  • You could leave it to the user to load hyperref and say in the documentation of your package please load hyperref but use \PassOptionsToPackage{<option list>}{hyperref} in your package file. See the clsguide for more help on package and class creation
    – BambOo
    Jul 13, 2020 at 15:23
  • 2
    There is no proper way, loading hyperref is currently a bit of a pain. (But we hope to improve this). You can try \AtEndPreamble{\RequirePackage{hyperref} (needs etoolbox), but it doesn't solve every problem. Jul 13, 2020 at 15:25

1 Answer 1

9

The Option Clash error only happens when the options used at the second \usepackage are not empty and differ from the first option list, so you can defer loading hyperref at the very end of the preamble (for example, by using etoolbox's \AtEndPreamble) and load it without options. In this case, if the user doesn't load hyperref, your package does at the end of the preamble. If the user does load hyperref with options, then your \RequirePackage{hyperref} line won't raise an error because no option was requested. You can pass options to hyperref using \PassOptionsToPackage{hidelinks}{hyperref}:

\documentclass{article}
\begin{filecontents}[overwrite]{mypackage.sty}
  \NeedsTeXFormat{LaTeX2e}
  \ProvidesPackage{mypackage}[2020/07/13 A dummy package]
  \RequirePackage{etoolbox}
  % Any option your package wants:
  % \PassOptionsToPackage{hidelinks}{hyperref}
  \AtEndPreamble{%
    \RequirePackage{hyperref}}%
\end{filecontents}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[francais]{babel}

\usepackage{mypackage}
\usepackage[colorlinks]{hyperref}

\begin{document}
Hello
\end{document}

But this will be problematic, for example, with cleveref, or other packages that must be loaded after hyperref: the user will have to load hyperref explicitly before loading cleveref, otherwise things won't work properly.

You must log in to answer this question.

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