Modern packages for LaTeX

TeX is an advanced typesetting system in wide use in the sciences, mostly because of its hassle-free mathematical typesetting capabilities. Pretty much all papers found on the preprint server arXiv, for example, were typeset using TeX. It has been around since the late 1970s, and that heritage is quite visible in its technical limitations as well as on the user-facing side in many situations.

Most people do not use the plain TeX language, but instead rely on macro collections on top of TeX. The one that is most widely used, even to the point that many people use its name and TeX interchangeably, is LaTeX, started in the 1980s (There are alternatives to LaTeX, however, we do not dwell on those here. Look up ConTeXt for a completely different approach to typesetting using TeX). The main idea behind LaTeX as compared to TeX is that in LaTeX the markup used is supposed to be logical, i.e. ideally you use commands to assign a meaning to the text instead of a style. As an example, instead of setting “Introduction” in Helvetica, Bold, 16pt, you tell LaTeX that “Introduction” is the name of a new chapter and it chooses the text style automatically. This enables LaTeX to do a whole lot with the meta information it thus receives: Automatic page numbering, indexing, table of contents, etc.

LaTeX is not a monolithic system, instead it relies on a plethora of document classes and packages to deliver its functionality. Most of those packages are provided by independent developers and are not necessarily up to date or working together well. There is a huge repository of LaTeX packages that is accessible through the CTAN sites, and searching for keywords there is usually a very good starting point. However, it is still a challenge to find the appropriate package for any given need, especially when several alternatives are available. (Thanks to Kevin Dungs for comments and additions)

Keeping your TeX installation up to date

There are about 900 packages available in the CTAN directory. Many of those have not seen any updates in 20 years, but many are also new and in highly active development. To profit from new features and packages it is necessary to keep your TeX installation up to date. For TeXLive-based distributions, such as MacTeX, the following commands can be used to update all installed packages:

sudo tlmgr update -self 
sudo tlmgr update -all

With the exception of the latest development version of the microtype package, all packages introduced here are already part of your TeXLive 2012 installation.

What engine to use?

TeX is older than PostScript, Type 1 fonts, TrueType fonts, Unicode, or PDF, which is why it comes with its own technologies in those areas, that are now mostly obsolete. We want to produce high-quality PDFs with hypertext, modern fonts and maybe Unicode support. 

A TeX engine is responsible for compiling your source code into the finished document, sometimes loading appropriate macro packages along the way. You typically invoke it on the command line or via your editor. When writing complex documents, often more than one run of the engine is necessary in order to process all intermediary auxiliary files. In that case, latexmk can be used to simplify the compilation process, or you can write your own Makefile.

There are four main engines to choose from, which I list here using the name you would use to invoke them on the command line:

latex + dvips/dvipdf

This approach uses an intermediary DVI file that is then converted to PostScript or PDF. This is the old-fashioned way to use LaTeX, going back to the 1980s. Modern fonts, Unicode, and micro typography are not supported. You may also run into the hard-coded memory limit of the TeX system on some occasions. Unless you really really need the package pstricks (and you should not, as there are better alternatives), there is no reason to use this. 

pdflatex

This engine is a big step up in terms of typography. It produces PDFs directly and has support for microtypography tweaks. However, OpenType fonts are not supported and multi-language support with Unicode character sets is not there either. The memory limit of TeX is a problem when using PDFTeX as well. In most situations, especially when only western character sets are used, PDFTeX is still a good choice, though.

xelatex

XeTeX is an alternative to PDFTeX that is mostly known for its good OpenType and Unicode support and has the most advanced multi-language package available. It is also subject to the memory limitation and somewhat slower than PDFTeX when using OpenType fonts. Its microtypography support is still not as good as PDFTeX’s.

lualatex

This is the engine I recommend for most uses. It is based on a rewrite of the TeX system using the Lua language, offering many new capabilities to the developers of packages. Thankfully, it also removes the memory limitations of the old TeX system, which can be incredibly useful in some circumstances. OpenType fonts, Unicode and microtypography are also supported. The speed is mostly on par with XeTeX. LuaTeX is still not “finished,” but I found it to be perfectly usable and have not run into serious issues even when writing long and complex documents.

What packages to use?

This question cannot be answered in general, of course, as your needs dictate the packages you want to include. I will try to outline some common scenarios and recommend solutions, however. For more detailed information I would like to point you to the texdoccommand found in TeXLive installations. Issuing texdoc nameofpackage on the command line will open the PDF documentation for said package. I try to only use and recommend packages that have good documentation available.

I have also hyperlinked the package headlines to their CTAN pages, where you can download the documentation or view it in your browser.

Document classes

KOMAScript

The choice of the document class determines many defaults in layout of your document. For pretty much all common uses, the KOMAScript classes are the best choice. KOMAScript contains the main document classes scrbook, scrreprt, and scrartcl. These replace the old book, report, and article classes. KOMAScript is a very complex package and can be customized to an astonishing degree. Luckily, it also has excellent documentation available through texdoc. Even without any customization, the defaults are great and can be used without hesitation.

For setting custom footers and headers, using scrpage2 is recommended as it belongs to the KOMAScript system and works together with the KOMA document classes nicely. For non-KOMAScript documents, the package fancyhdr is also an option.

Math and code packages

amsmath

Nobody should write math in LaTeX without this package. It provides a large number of environments for display equations and takes care of numbering them. Do not just use align all the time, there are great environments for many different scenarios: single equations, single equations that are too long to fit in one line with or without alignment, multiple equations with alignment, multiple equations without alignment, etc, subequation environments with “a,b,c” labels, etc. When using these environments properly, you should never have to use \nonumber to suppress equation numbering again! Consult the documentation for more information (and never ever use $$x$$ for display equations).

mathtools

AMSMath is a package that rarely receives updates. Mathtools is meant as an extension, providing bugfixes and some useful additions. It should always be used whenever amsmath is used. One very useful addition is extended symbols with text above or below, in the style of over- and underbrace.

amssymb

A huge number of useful math symbols are defined in amssymb. This is meant to be used with the old AMS symbol fonts, but including it does not hurt even when using Unicode math.

xfrac

The package xfrac provides fractions that look good in the body of your text. It can be seen as a replacement for nicefracs, offering better typography, compatibility with Unicode fonts and more flexible font styles. If you do not need to do any under-the-hood changes, loading the package and using the command \sfrac to produce inline fractions is all you need to know.

siunitx

Typesetting numbers with units has a few pitfalls that can be avoided by using a package to do the work. Nowadays, the package siunitx should be used for that purpose, as the units package is out of date. The package siunitx takes care of formatting numbers with exponents, complex quantities and multiplication. It also handles the proper typesetting of unit symbols and the correct spacing. So if an expression like $3.44\times 10^{-4}\,\mathrm{m}$ looks worse than $\SI{3.44e-4}{m}$ to you, siunitx might help.

minted

LaTeX provides a verbatim environment that can be used to typeset programming code examples in a very primitive way. To use more advanced features like syntax highlighting, labeled listings that can be referenced in the text, a list of listings in the backmatter of your document and many formatting options, take a look at the package minted. It requires a python back end to do its work.

Tables

Typesetting tables is one area where the LaTeX paradigm of not requiring the user to do design is not well-supported. This results in awful-looking tables that are hard to read. Using the following two packages helps to typeset better tables:

booktabs

Look at the tables in any professionally typeset book. There are two things you will find almost never: double rules and vertical rules. However, both seem to be favorites of many users of LaTeX. The package booktabs helps making great-looking tables. It mostly provides the three commands \toprule, \midrule, and \bottomrule and takes care of proper spacing between those rules. Look into the documentation and check out the examples. 

tabularx / tabulary

These packages (tabulary is newer) allow for tables with pre-defined widths and growing columns. This makes it possible to create a table that fills the entire width of the page with automatic column sizes.

threeparttable

Tables often have three parts: A caption above, the table below that, and table notes below. With threeparttable you can typeset such tables easily. This way you do not have to give all information in the caption above the table but can separate it into a proper short caption and more detailed notes concerning some of the more intricate points of the data presented below the table.

Graphics

graphicx

This is mostly used to include figures into your document, although there are more things you can do with it. The \includegraphics command provides many useful options for scaling and rotating your figures.

feynmf

The packages feynmp and feynmp are probably only useful for high energy physicists. You can use them to semantically create Feynman graphs. They can be rescaled after the fact and use the standard fonts of your document for labels. The usage is pretty complex, so reading the manual is a must. The packages also requires an intermediary execution of mf(for feynmf) or mpost (for feynmp, which is recommended) in the compilation chain.

pgfplots

Also squarely in the realm of “using LaTeX semantically,” pgfplots can be used to create complex plots within LaTeX with the capability to replace GNUPlot for many uses. I often do not use pgfplots in my main LaTeX documents, because you do not always want it to run through large data files and recreate a plot on every run, but it is very useful nevertheless. One huge advantage is that it is trivial to use the same font choices and sizes for your plot labels as in your document as well as standard LaTeX math notations.

It makes sense to use pgfplots with LuaLaTeX, as it is easy to run into memory constraints when drawing complex plots otherwise.

A related technique for GNUPlot users is the tikz terminal within GNUPlot. Using it makes GNUPlot create a LaTeX file that uses the same underlying graphics library that pgfplotsuses.

tikz

I am not a huge proponent of using markup to create figures (except for Feynman graphs), but many people love using pstricks, e.g. Well, do not use pstricks. Use tikz instead. It provides all you need to create great-looking drawings of low or high complexities.

Captions and split floats

caption

KOMAScript already provides great-looking captions, but sometimes more customization is wanted or need. The caption package provides that.

subcaption

The subcaption package can be used to produce nice-looking subfigures and subtables, i.e. you can have a figure 1, that is split into 1a, 1b, and 1c, with smaller captions for each subfigure and a larger caption for the entire figure.

captcont

Sometimes it is not possible to fit all the images that belong together on one page. Using captcont you can retain the figure or table number across multiple pages, so figure 1 can start on one page and end on the next, with different or repeated captions.

Use this only as a last resort, though. The purpose of numbering the figures and tables in your document is to make finding them easier. Splitting them over several pages defeats that purpose.

Bibliographies

biblatex

The combination of biblatex and biber replaces the legacy BibTeX engine and has many advantages, especially multi-language support and creating style files, which now use TeX macros and are not generated by the BibTeX backend anymore. It uses standard BibTeX bibliography database files, so you can carry those over from old projects without any additional work. One big advantage of using biblatex is that the bibliography styles now have options that can be set in your LaTeX document, instead of having to modify style files. This way, you can for example decide if you want to show the titles of the articles or the e-print numbers just by switching a package option on or off. The provided phys style emulates the APS style used in most physics papers. Citations in the style of mcite with multiple entries per key are also directly supported. You should at least skim the documentation, as biblatex is a very powerful package with lots of options.

Glossaries and acronyms

glossaries

The glossaries package makes creating a list of acronyms or a glossary easy. You can define the list using package commands in the preamble or in a separate file and use simple commands to refer to the acronyms or glossary entries at the appropriate locations. The list is maintained automatically and hyperlinks are also supported. The package requires an additional run of the xindy tool in the compilation chain.

PDF Metadata and hyperlinks

hyperref

The package hyperref is not only responsible for enabling PDF hyperlinks (the red boxes around hyperlinks can be disabled, by the way), but it also allows you to set PDF meta data. Usually, no options need to be set for hyperlinks to work great, but its extensive documentation is a good place to start when looking for the meta data options.

Fonts, typography and languages

fontspec

The package fontspec is the most important building block when talking about OpenType fonts in LaTeX. It enables you to select standard fonts available in your operating system and use them in LaTeX documents. In many cases, the \setmainfont command is all you need to use to enable this, but there are many many options to customize fonts and font styles. Thankfully, the documentation is very complete and useful. Use this package if you are tired of always seeing LaTeX documents set in Computer Modern or Palatino.

To get started, try the following in your document and compile using lualatex (substitute Helvetica for any other OpenType font available on your system):

\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
\setmainfont{Helvetica}

In case you wonder what the second command does: It allows you to use the old TeX-style macros for some symbols, i.e. you can write --- instead of using the — symbol directly. This preserves compatibility with non-Unicode LaTeX documents.

unicode-math

This is the math mode equivalent to fontspec. It supports using special OpenType fonts with math support instead of the standard TeX math fonts. There are a few great such fonts available, such as the XITS Math fonts, which is Times-like, Asana MathLatin Modern MathTeX Gyre Math, and the commercial Cambria Math distributed by Microsoft . You can also use the package settings to do more complicated things, like replacing certain Unicode ranges with symbols from another font (For example, to use the italic style of your main body font for all variables, but still use the math font for operators and symbols).

To get started, install the XITS Math OpenType font from their github page and try the following:

\usepackage{unicode-math} 
\setmathfont{XITS Math}

microtype

The package microtype optimizes the microtypography of your document, mostly through character protrusion and font expansion. It can be tweaked extensively, but in most cases just loading the package is enough to visibly improve the microtypography. The microtypepackage requires the PDFLaTeX, XeLaTeX, or LuaLaTeX engines. As of now (10/2012), the experimental version from TLContrib is required for the newer TeX engines.

babel / polyglossia

The package babel is used to set the language of your document. It takes care of many language-specific things, like hyphenation, special characters and typographical conventions. The package polyglossia is a modern replacement for babel, that does however not support LuaLaTeX at this point.

Assorted tricks and comments

This is a grab bag of little things that you might want to consider when working with LaTeX. Many more such comments on good LaTeX style can be found in the l2tabu document. To automatically check your document for problematic commands and forbidden LaTeX, check out the nag package.

Encodings

LuaLaTeX and XeLaTeX are Unicode engines. Encode your files as UTF8 and do not use any special packages for character encodings. It just works.

White space

LaTeX is usually quite clever in taking care of typographic details. However, doing this properly is often impossible without knowing what the author means. This is especially true for spacing issues. As a general rule, whenever you use punctuation, LaTeX treats this as the end of a sentence or a part of a sentence. Depending on the language selected, it also sets a wide space after a period ending the sentence. This is often not wanted. To prevent LaTeX from issueing line breaks after punctuation marks, you can use the ~ symbol. This should be used in expressions like Prof.~Dr.~Huber, where line breaks after the titles are not wanted. You should also use ~ to prevent line breaks right before numbers or mathematical expressions, e.g. figure~\ref{this}, reference~\cite{that}, or the constant~$c$, although much of this is up to you personal preferences.

Much more on proper spacing in many situations can be found in style guides such as the Chicago Manual of Style.

Front, main and back matter

Do not forget to use the \frontmatter, \mainmatter, and \backmatter commands when writing a document in the scrbook class. This takes care of proper page numbers and chapter numbering, letting your main content start on page 1.

Font settings

Never use {\bf something}, {\it something}, etc. These old commands mess with LaTeX font attributes and result in bad spacing. Always use the newer \textbf{something} style commands.

Math issues

Try to be consistent with your nomenclature. Every letter you type in math mode is considered a variable by LaTeX, i.e. you have to explicitly tell LaTeX when a string of letters is something else. A very common mistake is writing something like $V_{min}$, which LaTeX interpretes as V_{m times i times n}. For abbreviations like “min” for “minimal” or even “L” for “left,” always use the \text command, as in $\text{min}$.

The symbols “e”, “d”, and “i” are often typeset confusingly. When referring to Euler’s constant, the infinitesimal “d”, or the imaginary unit “i”, always typeset those as $\mathrm{e}$, $\mathrm{d}$, and $\mathrm{i}$.

When writing integrals, inserting a small space after dx often looks good: $\int \mathrm{d}x\,x^2$.

I prefer to always number all equations, even when they are not referred to explicitely in the text. This makes it easier for other people to comment on or discuss your text. You can still structure the numbering by using the subequations environment liberally.

The automatic sizing of brackets using \left and \right is convenient, but not always the best solution. If you have more than one set of brackets on the same logical level, it often looks better to use the same size brackets, even though one set is then larger than necessary. Consider the expression $(x+y)^2 - (x^2+y^2)$. You might want to typeset both sets of brackets using \bigl / \bigr instead of using the automatic sizing of left/right.

The package hyperref does not like math expressions in titles, because those are not supported in the PDF meta data visible in PDF readers. The solution to this is the command \texorpdfstring{$A_4$ Symmetry}{A4 Symmetry}, which selects the first value for the TeX engine and the second value for the PDF meta data.

You surely know the \ref command, that is used to refer to labels, as in figure~\ref{fig:myfigure}. When referencing equations you can use the \eqref command that has the same syntax but automatically adds brackets around the equation number.

Always use \frac instead of \over.