0

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}

\LoadClass{report}

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

\iftext@hyperref
  \AtBeginDocument{
    \PassOptionsToPackage{%
      bookmarks = true,
      pdfusetitle,%
    }{hyperref}
    %
    \RequirePackage{hyperref}
    %
    \hypersetup{%
      colorlinks    = true,%  Color text instead of boxes
      linkcolor     = black,% Color of internal links
      citecolor     = black,% Color of citations
      urlcolor      = blue,%  Color of external urls
      pdfstartview  = Fit,%
      pdfmenubar    = true,%
      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)

enter image description here

  • With AtBeginDocument

enter image description here

2

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{%
      colorlinks    = true,%  Color text instead of boxes
      linkcolor     = black,% Color of internal links
      citecolor     = black,% Color of citations
      urlcolor      = blue,%  Color of external urls
      pdfstartview  = Fit,%
      pdfmenubar    = true,%
      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}
2
  • 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.
    – raedma
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.