< A collection of things
Main page :: Things :: LaTeX packages

LaTeX packages

A description of packages that a LaTeX user should consider for inclusion in the preamble.
In order to help LaTeX users to find their way in the sea of LaTeX packages, I provide here a list of packages that make it into my documents, with some explanation about the reasons.

Introduction

LaTeX documentation is abundantly available on the Internet. The problem is that over the years, there have been some evolutions, and practices that were once common are not relevant or correct any more, or have been replaced with better alternatives.

In this article, I list a few things that I always do (or should not have forgotten to do) when writing down the preamble. I am not a LaTeX specialist, but a user with several years of experience, and the will to search and read documentation for this kind of problem. As such, this does not constitute a definitive guide, but a personal opinion, and hopefully a good and useful one. I also did my very best to make sure that it does not contain any errors.

Rationale

There is no one-size-fits-all preamble that will work for all your documents. Therefore, instead of providing a copy-and-paste preamble, I will list a number of packages that I often include in my documents. You have to realise that I mostly write scientific articles: documents of, say, 10 pages, with some figures, tables and equations, and references to a literature list. This influences of course my experience and choice of packages.

Also, I am not providing an introduction to LaTeX. There are enough of these around, and I have not much to add to them: install the MacTeX, TeX Live or MiKTeX that suits your platform, find a suitable editor and try some things out, and realise that it is a good thing to read some documentation in advance.

I target those people who have done that, and who are now using Google to find out how to get things done and are a bit lost in the choices. After all, TeX Live contains several GB of packages and documentation.

Before we start

While drafting this document, I came across this document, explaining things you should not do when writing “modern” LaTeX 2ε. It also lists a number of obsolete packages and habits, and is thus a good companion with my list.

There is a package nag that warns you about writing LaTeX code against the advices of the above document. You call it by putting \RequirePackage[l2tabu, orthodox]{nag} at the start of your document, apparently thus before the \documentclass. I have not tested this package, but it seems a good concept that you can check your code for obsolete or problematic code.

Paper size

A first thing to be aware of is that LaTeX is in origin American, and hence, its default setting is to use Letter paper, which is a bit broader and shorter than the European A4 paper. The difference is not all that large: with normal margins, text on a given paper format will be printed on the other one, but the position on the page and hence the visual appearance will be off.

The latest releases of TeX Live (I am not sure since when, but in MacTeX, the Mac OS X distribution of TeX Live, at least since 2012, with a graphical interface called Tex Live Utitility.app) include the utility tlmgr, which allows to set the paper size for the entire distribution. I vaguely recall encountering something analogous in MikTeX.

Apart from that, the default LaTeX documentclasses (article, book, report,…) have an optional parameter to set the paper size: \documentclass[a4paper]{article}.

In general, you should always fix somehow the paper size for which you intended the document. Suppose an A4-intended document is typeset by a Letter-assuming TeX engine or the other way around: you can have layout failures because a (hard-coded) length does not mean the same any more. You can often get around this by deriving your lengths from the page or text block dimensions, but that also requires care.

Packages

microtype

pdfTeX, currently in practice the default TeX/LaTeX engine, knows quite a number of micro-typographic enhancements. In fact, pdfTeX was originally written for a PhD thesis on typesetting. The micro-typography features are not enabled by default, and the microtype package provides an interface to these, and also enables a number of them when the package is loaded.

Micro-typography is about the very fine details of laying out text a on page: what should happen at the end of letters, words or lines to give the best possible visual appearance. Without going into the details (for that, you should read the package’s documentation), pdfTeX can do character protrusion and font expansion, among other things. As only the former two are enabled by default, I will sketch what they do.

Character protrusion, also known as margin kerning, is the adjustment of the position of the character at the edge of a line in a justified text block. Not every character at the end of a line appears equally broad: a point takes “less space” than a letter “n”. As such, to keep the optical appearance of a straight line for the margin, a line ending in a point should extend a bit in the margin. If performed correctly, the margins of the text block will appear less ragged.

Font expansion is about changing the font, using a slightly wider or narrower variant, in order to get a more even interword spacing. Justifying a text requires adjusting the interword spacing, but in some cases, a better effect is achieved by a small variation of the font.

Both character protrusion and font expansion, as well as other micro-typographic features, serve the purpose of improving the visual appeal of the text. But, these changes are often so subtle that for the (my) untrained eye, it is difficult to tell which version is with or without microtype enabled. On the other hand, if you use an editor/previewer combination like TeXShop, which updates immediately the PDF files after rerunning, you see that many lines undergo small changes, and that occasionally the hyphenation changes or words jumps to another line. The effect is subtle, but I often feel that a microtyped text is more evenly distributed over the page.

a4wide

a4wide is an old package that occasionally makes its way into tutorials and the like, but it should not be used. It creates a text block which fills a large amount of an A4 page. But, it actually fills, according to several knowledgeable sources, too much of the space. For a text to be comfortably readable, a line should neither be too short (with your eye continuously jumping back to the next line) nor too long (where your eyes have to jump back a long distance, and may miss the correct line to continue). Although you will find different opinions, values between 60 and 70 characters are generally considered optimal. The a4wide package usually gives a much broader textblock.

As an alternative, you should consider the geometry package (described below) or the documentclass option (described above).

geometry

The geometry package allows the tuning of the whole collection of distances that make up the layout of a page. It supports the very simple usage \usepackage[a4paper]{geometry}, which sets a good layout for an A4-sized paper. Of course, a wide collection of paper sizes is supported.

In addition, the package allows to tune every possible length (margin, header space, odd/even page specific margins,…) to your liking. The documentation of the package is good at explaining these lengths, and many options are available for leaving out headers, footers, margin notes, etc. If you ever need anything of a manully controlled, alternative page layout, geometry is your friend.

booktabs

Typesettings tables in default LaTeX is very tedious business, and the end result is bad. The main problem is that the default \hline and \cline for the horizontal lines in tables do not appropriately add additional horizontal space between the lines they separate. This results in a table where the text in the cells nearly runs into the horizontal separation lines.

booktabs resolves this problem by defining four new commands for the horizontal lines (or rules): \toprule, \midrule and \bottomrule for rules over the full table, and \cmidrule for partial rules. These commands provide a sensible additional horizontal space.

It should be noted that this package does not support vertical lines in the tables, as it is the author’s (and many other typographers) opinion that vertical lines should never be used in tables. In nearly all situations you need a table, I think you should adhere to this advice.

The package is no replacement for the default tabular enviroment, it just results in correct horizontal rules. There are a whole lot of packages that provide such replacements or additions. In my opinion, apart from the sometimes unreadable LaTeX code that you get for a more complex table, tabular and booktabs are good enough for most of the tables you will ever want to write.

graphicx

The principal function of this package is to allow the inclusion of external graphics files. It provides the \includegraphics command that takes an external figure file as input and puts in your document, subject to a bunch of settings concerning size, rotation and the like. In addition, it also defines a few other commands like \rotatebox and \scalebox .

This package actually exists in two versions, the “old” graphics and the “new” graphicx, where the final ‘x’ stands for extended. graphicx is built on top of graphics. A rationale for this from the author of both packages (David Carlisle) can be read on TeX.SE. The answer essentially boils down to this: graphicx provides the modern way of accepting parameters (the “key=value” system), so you should use that one.

other graphics packages

I have seen several tutorials or example preambles that use the epsfig and epstopdf packages. Both of these are not always necessary any more.

epsfig provided an interface for the inclusion of .eps files in the days of LaTeX 2.09, when LaTeX was young and pdfTeX did not exist. This is: before 1994. A compatibility packaged is still included in current LaTeX distributions.

epstopdf is still relevant, as it handles the conversion of .eps graphics files (suitable for “classical” LaTeX use, where a .ps file is generated in the end) to .pdf that can be included by pdfTeX. The recent LaTeX distributions call this package themselves when it is needed, so it is not necessary any more to write it explicitly (but it does not do any harm either).

siunitx

The Bureau International des Poids et Mesures, in non-Fenchspeaking circles better known as the BIPM, is responsible for the development of the Système International d’Unités, the SI. Apart from defining a consistent system for units of measurement, it also provides a number of rules on the typography of the units and quantities. So it was only a matter a time before someone would convert these rules into a LaTeX package.

The siunitx package fulfils this expectation. Note the x, there was/is also SIunits, but that package is superseeded by the x-version; the old version is still distributed, but for new texts, use the new one. siunitx provides a wealth of possibilities, not only for the core SI, but also for the typesetting of numbers in itself, and the typesetting of uncertainties. I invite you to skim through the documentation get an impression of these.

The essential commands are \si{}, which typesets a unit, and \SI{}{}, which typeset a number with the corresponding unit. The usage is simple in itself: you type the unit as you would read it: \si{\joule\per\gram\per\kelvin}, and the package handles the format according to its settings, for example translating the \per to a division or an exponent: J/(gK) or J g–1 K–1.

PGF and TikZ

There is nothing that makes an explanation more clear than a good illustration. Where LaTeX is the tool to provide you with a good-looking text, PGF is its companion for the graphical part. In the title I wrote PGF and TikZ, and just a comment on that: PGF is actually an abstraction layer which provides very basic drawing commands, TikZ utilises these to construct the higher-level commands that you use in practice. Roughly speaking, TikZ is a drawing “program” built on top of the PGF engine.

You have to like drawing with commands instead of the mouse (but if you are reading this, you are also not writing text in Word, WYSIWYG is not always the best way to do things), but have a look at the examples in the manual to see what is possible (virtually everything). But be warned: the documentation of the PGF/TikZ package is huge at currently 1165 pages. And yet: due to the way it is designed, once you catch its logic, it becomes manageable. I do nearly all drawing work within my artistic capabilities in TikZ these days.

I will leave the discovery of this beautiful piece of LaTeX programming as an exercise for the reader, but let me point you to one example on this website: the graphical sitemap. Largely based on an example in the manual, its source code consists of a simple tree with the names of the different bulbs, and everything else is handled by a list of options for defining how to place and colour these bulbs. This website also maintains an extensive collection of examples.

Note that my endorsement of TikZ is not a statement against other drawing packages, such as PSTricks or asymptote. They probably have their values, and advantages and disadvantages compared to TikZ. PGF/TikZ was the first one I tried, and I never felt the need to try anything else again. The reason I tried this one first was that I disliked the premisse of PSTricks that it only generates PostScript natively (PGF works on most common LaTeX engines in contrast) and that asymptote is an external program. Although I never made a comparative study of the three mentioned packages, the syntax of PSTricks looks to me not a clean as that of either PGF or asymptote, and asymptote seems to lack the very convenient key=value system from PGF and PSTricks for options to graphical elements.

PGFPlots

For your drawings there is TikZ, and for data plots there is PGFPlots. As the name suggests, it is built on top of PGF. It loads a data file and creates a plots using LaTeX fonts. The plots are highly configureable, but the defaults give already an excellent basis. Let me put it like this: it takes more time to make a good figure in Excel than to learn how to use PGFPlots.

But there is one disadvantage that must be mentioned: because PGF is written in TeX, also all the calculations need to be done in TeX, and TeX is a programming language for typesetting text, not for fast calculations. So, the processing of substantial data sets requires time.

natbib

The automatic handling of bibliographies by LaTeX is one of its strong points. The basic facilities, \cite and \thebibliography already cover simple use cases. More power becomes available with the use of BibTeX, or the more modern biber/Biblatex combo’s.

natbib adds a whole number of variants of \cite, allowing not only fine tuning, but also author-year schemes. It provides an easy way to tune settings such as the shape of the marks surrounding the citation, automatic compression of successive numbers for multiple citations. The overview sheet natnotes.pdf provides a good overview and reference to this functionality.

As usual, there are a number of other packages that provide this type of functionality, and I never really delved into them, and neither did I spend much time on the alternatives for the underlying BibTeX processor (biber/Biblatex). For my purposes, the combination of natbib with BibTeX is sufficient, but as your situation extends beyond the world of ASCII characters, you may look a bit further.

As far as I can see, the hypernat package is not needed anymore.

I may in the future dig into the subject of LaTeX bibliographies deeper, especially if I ever feel the need to leave BibTeX behind and move on the biber/Biblatex combination.

cleveref

I only recently started using cleveref, a package that modifies the \ref facility. “Modifies” should be read as “make more clever”, in that it can determine the type of the object referenced (equation,figure, table, section,…) and that it can handle multiple references in a single command.

Loading with the standard \usepackage{cleveref} makes, among others, the command \cref available. As a short example, This can be seen from \cref{eq2,fig1}. would under the default settings expand to “This can be seen from eq.~(2) and fig.~1.” At the beginning of a sentence, you should use \Cref to make the sentence start with a capital and without an abbreviation. It also has language support in case you are not writing in English.

The only comment I have is that the manual does not have a list with the default “types” (equation, figure,…) it tries to recognise this way, and with the default texts are that it attaches to them. This would require a bit of trial and error. One thing to know in this respect is the [capitalise] package option, which gives you ‘Eq.’ and the [noabbrev] option, which gives you “equation”.

If you are using cleveref in combination with hyperref (see below, then you have to load hyperref (which in general should be loaded as one of the last packages) first, and only then cleveref.

babel

The babel package defines the language in which the document has been written. This has two important consequences: it defines the hyphenation rules to use, and it redefines a number of command which generate output in a specific language. The latter means that for \usepackage[english]{babel}, \chapter will generate a “Chapter”, where for \usepackage[dutch]{babel}, it will become a “Hoofdstuk”.

It can also be used for multilingual documents, by loading the package with \usepackage[english,dutch]{babel} (which gives Dutch as the main language), and using \selectlanguage{} whenever it is needed to change the language.

caption

The caption package, very imaginatively, allows the modification of captions. After loading the package with the usual \usepackage{caption}, I suggest you use \captionsetup{<options>}, where the options are key=value pairs that control virtually every possible aspect of a caption, if needed for each float type separately. These aspects include fonts, labeling, alignment and margins.

Even if I do not intent to use the specific features of caption, I still load it, because it changes the separations below and above the text generated by a \caption command to correct values. I could copy here the explanation from section 2.6 Skips from the manual, but you better read it there. LaTeX’s default mechanisms put the caption at the top of a float (like for a table) too low, making it nearly run into the table, and loading caption corrects this.

It is also the only LaTeX package I know of that has a list and explanation of its warning and error messages in the manual.

For the history of this package, you should be aware that at the certain moment a package caption2 was developed, with some, at the time, new experimental features. It took considerable time before all these features were added to the original caption package, and caption2 became obsolete. So, occasionally you may see older code or explanations mentioning caption2, that just means that you will have to translate it to the new caption.

hyperref

The summary of the hyperref package says it al: “Extensive support for hypertext in LaTeX”. It essentially makes of \ref and \cite commands hyperlinks to the actual places in the document. By the way: in combination with natbib, doi, appropriate bibstyles and a .bib file that includes the doi information, it also generates hyperlinks to the cited publications generated from their doi.

For most of your applications, just loading the package will suffice. You only have to make sure that the \usepackage{hyperref} appears last of the packages that you include (with some exceptions, like cleveref above). The reason for that is that hyperref needs to redefine commands and also modifies the way LaTeX outputs typesetted text, and preferably no other packages should interfere with that.

On the other hand, the alphabetical list of package options stretches for more than two pages in the manual, and after that, there is still more information about extra commands that the package defines. However, you will need a more knowledgeable person than me for the details.

Memoir

Memoir is not package, but a documentclass, meaning that you use \documentclass{memoir} as your first line. It extends the book class, more or less “inlining” functionality from a lot of packages. For example, the functionality of booktabs and geometry is embedded in the memoir class. The idea of memoir is to have all necessary possibilities for the flexible typesetting of a long document in one place, without having to care about external packages and their possible incompatibilities.

If you ever need to start on a large, book-like project and need large modifications in the page layout compared to the default LaTeX classes, than you should have a look at memoir.

Sources

The information above is drawn over the years from various sources, and is collected by experience and necessity in the preambles of my LaTeX documents. These sources include:

  • CTAN, the Comprehensive TeX Archive Network, which collects everything related to TeX and LaTeX.
  • The TeX Live documentation, or better formulated the documentation of the individual packages.
  • TeX.SE, the Q&A website for TeX and LaTeX problems.
  • UK TeX FAQ, a whole lot of documentation.
  • The TeX Catalogue Online, is a kind of “manual” with additional information on the packages on CTAN.