# Hyperref: \hypersetup ignored for \AtBeginDocument

I want to add an option to a class text.cls that automatically loads a default style for hyperref supplied via the hypersetup command. This works as expected.

However, if I want to account for the inofficial hyperref should be the last package you load rule, see the hyperref documentation, and move the loading of hyperref inside \AtBeginDocument the hypersetup seems to be ignored and I get several warnings like

Package hyperref Warning: Option colorlinks' has already been used,
(hyperref)                setting the option has no effect on input line 9.


Why is that and how can I fix it?

## Class

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{text}[2019/11/25 A documentation class]

% ---------------------------
% Package
% ---------------------------

\RequirePackage{kvoptions}
\RequirePackage{ifthen}

% ---------------------------
% Options
% ---------------------------

\SetupKeyvalOptions{%
family=text,%
prefix=text@,%
setkeys=\kvsetkeys,%
}

% Bool option
\DeclareBoolOption[false] {hyperref}

\DeclareDefaultOption{%
\PassOptionsToClass{\CurrentOption}{report}%
}

% Process options
\ProcessKeyvalOptions{text}

% ---------------------------
% Package
% ---------------------------

\iftext@hyperref
\AtBeginDocument{
\PassOptionsToPackage{%
bookmarks = true,
pdfusetitle,%
}{hyperref}
%
\RequirePackage{hyperref}
%
\hypersetup{%
citecolor     = black,% Color of citations
urlcolor      = blue,%  Color of external urls
pdfstartview  = Fit,%
pdftoolbar    = true,%
bookmarksopen = false,%
%bookmarksopenlevel = 1,%
}
}
\fi

% ---------------------------
% Fin
% ---------------------------

\endinput


## MWE

\documentclass[%
hyperref=true,
]{text}

\author{An author}
\title{text class description}
\date{\today}

\begin{document}

\chapter{Test}
\label{sec:test}

Hello World! Reference to \autoref{sec:test}. And a URL:

\href{https://tex.stackexchange.com}{https://tex.stackexchange.com}

\end{document}


## Results

• Without AtBeginDocument (wanted)

• With AtBeginDocument

It is better to load hyperref with \AtEndPreamble from the etoolbox package. But basically loading hyperref in a class is a bit of a pain, as there are also package who should be loaded later (cleveref), packages which force an early loading of hyperref (pdfx), packages which try to detect hyperref and so on.

\documentclass{book}
\usepackage{etoolbox}
%\AtBeginDocument{%
\AtEndPreamble{%
\RequirePackage{hyperref}
\hypersetup{%
citecolor     = black,% Color of citations
urlcolor      = blue,%  Color of external urls
pdfstartview  = Fit,%
pdftoolbar    = true,%
bookmarksopen = false,%
%bookmarksopenlevel = 1,%
}}

\begin{document}

\chapter{Test}
\label{sec:test}

Hello World! Reference to \autoref{sec:test}. And a URL:

\href{https://tex.stackexchange.com}{https://tex.stackexchange.com}

\end{document}

• Thanks, that solves the problem. However, I'd really like to understand why this behavior occurs for \AtBeginDocument. And for the problem with loading hyperref in a class: I know. But I am trying to write a simple wrapper for students of mine that should have an easy start with LaTeX. If they encounter these kinds of problems, they are more than welcome to build their documents based on another documentclass. Nov 27 '19 at 15:51
• Loading a package like hyperref which uses the \AtBeginDocument hook itself in \AtBeginDocument` is never a good idea. If you really want to know the details: Add \tracingmacros=1 before \begin{document} and look in the log and try to disentangle the processing. Nov 27 '19 at 16:04