18

I want to group packages together (in the real document, I load many more packages) and want to solve the sorting of the packages with commands like \AfterPackage from KOMA-Script.

I am using this solution to load the package cleveref after the package hyperref:

\documentclass{scrbook}
\usepackage{varioref} % before cleveref
\AfterPackage{hyperref}{\usepackage{cleveref}}
\usepackage{hyperref}

\begin{document}
\tableofcontents\chapter{test}
\subsection{variable references using vref}
\label{sec:references:vref}
\newpage
Some text which contains a vref reference
to section \vref{sec:references:vref}.
\end{document}

However the reference is never found. If I load cleveref instead hard coded after hyperref

\usepackage{varioref} % before cleveref
%\AfterPackage{hyperref}{\usepackage{cleveref}}
\usepackage{hyperref}
\usepackage{cleveref}

everything works as expected.

Why is \AfterPackage not doing what I expect it to do?

14

hyperref does seem to use some \AtEndOfPackage trickery. The normal version of \AfterPackage loads cleveref after hyperref, but before the trickery mentioned above. To ensure that cleveref is only loaded after hyperref has finished its business, use the "exclamation version" \AfterPackage!{hyperref}{\usepackage{cleveref}}.

\documentclass{scrbook}
\usepackage{varioref} % before cleveref
\AfterPackage!{hyperref}{\usepackage{cleveref}}
\usepackage{hyperref}

\begin{document}
\tableofcontents\chapter{test}
\subsection{variable references using vref}
\label{sec:references:vref}
\newpage
Some text which contains a vref reference
to \vref{sec:references:vref}.
\end{document}

EDIT: Both \AfterPackage!{hyperref}{\usepackage{cleveref}} and \AfterAtEndOfPackage{hyperref}{\usepackage{cleveref}} actually don't work if you issue them after \usepackage{hyperref}. My personal cleveref solution which is independend of its positioning in the preamble is

\usepackage{etoolbox}
\AtEndPreamble{\usepackage{cleveref}}
  • Indeed, the \AfterPackage! also solves the problem. However I had the impression that \AfterAtEndOfPackage is even more secure. – Matthias Pospiech Dec 8 '11 at 15:58
  • Actually I load the package using \newcommand{\ExecuteAfterPackage}[2]{ \AfterAtEndOfPackage{#1}{#2} \AtEndPreamble{ \IfPackageNotLoaded{#1}{#2} } } and load the package then with \ExecuteAfterPackage{hyperref}{\usepackage{cleveref}} – Matthias Pospiech Dec 8 '11 at 16:30
  • 2
    @lockstep: \AtEndPreamble{\usepackage{cleveref}} isn't a general solution, since Ben User might do \AtEndPreamble{\usepackage{cleveref}} <something> \AtEndPreamble{\usepackage{hyperref}}. – Ahmed Musa Feb 3 '12 at 19:23
7

It can be solved using a different command: \AfterAtEndOfPackage. Somehow the \AfterPackage variant executes the code in the context of the package which is not what I wanted. This is described in the release notes of koma script: http://www.komascript.de/release3.09 (however in german).

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.