Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
I put your penultimate paragraph at the beginning of your question to provide the reader with the reason for your use of \AfterPackage right away; hope you don't mind. –  doncherry Dec 8 '11 at 16:00
    
See also tex.stackexchange.com/questions/27347/… –  lockstep Dec 8 '11 at 16:22
add comment

2 Answers

up vote 13 down vote accepted

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}}
share|improve this answer
    
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
add comment

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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