[Tex/LaTex] Best practice on organising your preamble

best practicespreamble

I am putting together a large document and my preamble is unwieldly and disorganised. What order does it make sense to load packages. Obviously, hyperref goes last, apart from things that have to go after hyperref. I tend to load font packages first, as they seem like a big global change. What other categories should I arrange my preamble around? Are there other cases where load order is important? Should I keep other commands close to the packages they relate to? e.g. should I keep my bibliography formatting commands near my \usepackage{biblatex} call?

Best Answer

If you want to see how things can get tangled in a preamble have a look at the preamble of the Comprehensive LaTeX Symbol List. It might not be exactly spaghetti code but it can certainly be classified as code soup!

So you are right, you need to have a strategy and start working on it early in the development of your document. The tips below are from my own workflow and observations.

Early on, when I started with LaTeX, I realized that having used numerous macros and packages to change the looks of almost every single parameter originally set by the LaTeX book.cls, I would have been better off developping my own class and this is my first tip.

1. Consider developing your own class or package to hold your changes.

It is as simple as hitting a save as button to save the base class .dtx file and its .ins file. It will get you going with literate programming and honestly it should not take longer than an hour or two to find out how it all works. When you use doc for the first time you might get disoriented, but eventually you get used to the conventions. Another advantage of this approach is that at the beginning of developing a new document, you will find out that on Mondays, Wednesdays and Thursdays, you will want your document to look one way and on the rest of the days of the week you will want it in another way. By writing your rationale down using literate programming, it helps you settle your ideas.

2. Have the packages and own related commands, near each other

For smaller changes, i.e., write short packages, either using the doc/docstrip system or if you are in a hurry just use the package filecontents and write them on the fly while developing the document. This tends to remove a lot of code and comment lines in the preamble. I have all maths related macros normally in a package called moremaths.

% MATHEMATICS
\usepackage{amsfonts}
\usepackage{amsmath}[2000/07/18] %% Displayed equations
\let\equation\gather                          %% See tabu and hyperref docs
\let\endequation\endgather
\usepackage{amssymb}[2002/01/22] %% and additional symbols
\usepackage{amsfonts}
\usepackage{xfrac}
\usepackage{stmaryrd}
\usepackage{mathtools}
\usepackage{eucal} 

Similarly, for tables

%% Tables
%%  TABLES
\RequirePackage{array}
\RequirePackage{booktabs}
\RequirePackage{longtable}
\RequirePackage{tabularx}
\RequirePackage{dcolumn}
\RequirePackage{multirow}
%% Set some local commands and colors
\RequirePackage{colortbl}              % for colored table cells
\definecolor{green}{rgb}{0.1,0.1,0.1}
\newcommand{\done}{\cellcolor[gray]{0.9}done}  %{0.9}for done tables
%
%

3. Divide the preamble into headings, such as typography, graphics, maths, sectioning etc.

If you do not develop your own class and use any of the major classes such as Koma or memoir, you will discover that in general these classes have their own configuration commands for every possible change; it helps if you divide all the relevant commands in sections. If the sections grow, i.e, if you have too many typography save the code in a package and name it moretypography or moremaths etc. Again here, if the preamble grows question the need for your own class.

4. Have the problematic package settings in their own packages e.g., sethyperef or setlistings package etc.

Some packages are difficult to set and can give you problems if they are loaded before or after some packages (See Which packages should be loaded after hyperref instead of before?). Others need some complicated and long settings. Again here if you work on a long documents it may be worth changing these settings to small packages. If you identify sources of errors better save and restore commands rather than move them around. For example the verse package gave me problems with the macro theHpoemline and I normally only load it together with the following macros.

\let\oldH\theHpoemline
\let\theHpoemline\undefined

There are a lot of similar techniques in the Comprehensive LaTeX Symbol List, preamble, worth a read.