# Sorting preamble packages thematically while accounting for compulsory load orders

Within a document's preamble (or a custom "preamble" package), at times there's the need to take compulsory load orders for certain packages into account. Most packages must be loaded before the hyperref package, some packages (e.g. glossaries, ellipsis) must be loaded after hyperref, and then there's the cleveref package which "basically[...] must be loaded last" (manual, section 2). A (small) document accounting for all this may look like

\documentclass{report}

\usepackage{varioref}% Must be loaded before hyperref
\usepackage{hyperref}
\usepackage{ellipsis}% Must be loaded after hyperref
\usepackage{cleveref}% Must be loaded after any packages that modify
%     LaTeX's referencing system

\begin{document}

\chapter{foo}\label{chap:foo}

Some text~\dots\ and some more.

\chapter{bar}

See \vref{chap:foo}.

\end{document}


However, I'd prefer a preamble that is sorted thematically, e.g.

\documentclass{report}

% Special characters and symbols
\usepackage{ellipsis}

% Cross-referencing
\usepackage{varioref}
\usepackage{cleveref}

\usepackage{hyperref}

\begin{document}

[...]


(I didn't round out the above code snippet to a complete MWE because it would result in unresolved references.)

I understand that sorting by compulsory load order is no big deal for small-to-medium preambles, but at some point (say, 20+ packages) it becomes confusing. Is it possible to sort preamble packages thematically, but still to account for compulsory load orders?

-
Do you want this to happen automatically? Do you mind using Unix utilities or does it have to be completely cross-platform? –  Andrey Vihrov Sep 3 '11 at 14:44
I'd prefer a completely cross-platform, "(La)TeX internal" solution; I'm aware that this may involve loading additional macros/packages and so may not be regarded as completely "automatic". That said, a good, comprehensible answer involving Unix utilities will get my upvote (I use Windows, though). –  lockstep Sep 3 '11 at 14:50
If restrictions were more formal, such as of form "X must be loaded before Y", it would be possible to apply topological sorting to the list of packages. But it's difficult to teach "Most packages must be loaded before the hyperref package" to the computer. –  Andrey Vihrov Sep 3 '11 at 15:34

A simple solution is to make use of the hook \AfterPackage{<package>}{<code>} provided by the scrlfile package (support package in the KOMA-Script bundle):

\documentclass{article}

...

% programming tools
\usepackage{scrlfile}

...

% glossaries and indices
\AfterPackage*{hyperref}{%
\usepackage{glossaries}%
}
\usepackage{makeidx}

...

% hypertext, bookmarks and PDF info
\usepackage{hyperref}

...


Note that <code> is only executed if <package> is eventually loaded. The star variant of \AfterPackage differs from the non-star variant in that <code> is immediately executed if <package> has already been loaded.

-
\AfterPackage* is great and covers most of my needs. For packages that should be loaded at the very end, \AtEndPreamble from the etoolbox package is a good addition. –  lockstep Sep 4 '11 at 8:39