# Are there any situtations where it is better to load a package after other code?

I always place all my packages immediately following the document class, followed by setting information (\hypersetup for example) and customization to my documents (header and footer customization for example), and macros for use in the document.

Is there any case were you would want to load code before you load a package?

This answers points out that for clarity you might want to have setting information immediately following the package, to reduce the amount of documentation you have to do (For example, I should move \hypersetup to be right after \usepackage{hyperref}, but before any packages that follow it, achemso or cleverref or whatever)

I thought all packages had to be loaded first, but it seems that in some cases you need code before them, for example you need to set \author, \title, and \date before you load the titling package.

So, are there cases where it is technically better (or required) to write some code before loading a package? By technically better I mean, makes a difference to the compiler, not just someone reading the code later.

-

One situation in which you need some code before loading a package is to prevent name clashes. A little example: suppose you are using one of the AMS document classes and you want to use some command from the dingbat package, say \leftpointright; you then try

\documentclass{amsart}
\usepackage{dingbat}

\begin{document}

\leftpointright

\end{document}


! LaTeX Error: Command \checkmark already defined.
Or name \end... illegal, see p.192 of the manual.


The problem is that the package amsfonts internally loaded by the AMS classes, defines \checkmark and the dingbat package also defined this command, producing a name clash. One possible solution is to use the savesym package, rename the conflicting symbol, and then load the dingbat package: \savesymbol{XXX} renames a symbol from \XXX to \origXXX thus preventing the name clash:

\documentclass{amsart}
\usepackage{savesym}
\savesymbol{checkmark}
\usepackage{dingbat}

\begin{document}

\leftpointright

\end{document}


This techniques was used extensively by Scott Pakin to build the The Comprehensive LaTeX Symbol List.

As a curiosity, the file associated to the savesym package is savesym.sty but the package says

\ProvidesPackage{savesymbol}[2003/06/01v1.1 Saves and restores symbols]


so you will get a (harmless) warning when using the package:

LaTeX Warning: You have requested package savesym',
but the package provides savesymbol'.

-

The biblatex package uses \parindent to initialize the \bibhang length which controls the hanging indentation of bibliography entries. If you want \parindent and \bibhang to match, any changes to \parindent should be carried out before loading biblatex.

\documentclass{article}

\setlength{\parindent}{2.25em}

\usepackage[style=authoryear]{biblatex}

\newcommand*{\sometext}{Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu
libero, nonummy eget, consectetuer id, vulputate a, magna.}

\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@misc{A01,
author = {Author, A.},
year = {2001},
title = {Alpha},
}
\end{filecontents}

\nocite{*}

\begin{document}

\sometext

\printbibliography

\end{document}


-
Seems like a bug in the package: The code for initializing \bibhang should be executed with \AtBeginDocument to remove that dependency on code order. –  Martin Schröder Sep 23 '12 at 13:07