Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 38 ยท Packages and Templates

Using Packages in LaTeX

Learn how \usepackage works, how to pass options, install packages with TeX Live or MiKTeX, avoid option clashes and choose a sensible set of essential packages.

LaTeX itself is small. Almost everything you do beyond the basics, from wider margins to colours, tables and code listings, comes from packages: extensions loaded in the preamble with \usepackage. There are thousands on CTAN, the Comprehensive TeX Archive Network. Knowing how to load, configure and diagnose them is a key skill.

Loading a package

LATEX
\documentclass{article}
\usepackage{graphicx}                         % one package
\usepackage[a4paper, margin=2.5cm]{geometry}  % with options
\usepackage{amsmath, amssymb}                 % several at once
\begin{document}
...
\end{document}

Packages go after \documentclass and before \begin{document}. Options go in square brackets, separated by commas. You can also pass options to the class itself so that several packages receive them:

LATEX
\documentclass[12pt, a4paper, british]{article}

Here british is a global option: packages such as babel pick it up automatically.

Finding documentation

Every package has a manual. From a terminal, run texdoc geometry to open it. Online, search CTAN for the package name. Learning to read the first page of a manual (the options table) answers most "how do I..." questions faster than a web search.

Installing packages

  • Overleaf and other online editors include nearly everything; there is nothing to install.
  • TeX Live (Windows, macOS, Linux): tlmgr install packagename. The full installation (about 7 GB) already includes almost all packages.
  • MiKTeX (Windows): installs missing packages on the fly the first time you compile, or via the MiKTeX Console.
  • Local file: if you have a .sty file, place it in the same folder as your .tex file.

The error File 'foo.sty' not found means the package is not installed. Install it or remove the \usepackage line.

A sensible starter set

Package What it does
inputenc, fontenc Encoding and fonts for pdfLaTeX (\usepackage[T1]{fontenc}); not needed with XeLaTeX/LuaLaTeX
babel Language rules: hyphenation, "Contents" in your language
geometry Page size and margins
microtype Subtle spacing for better justification
amsmath, amssymb Mathematics (see math lessons)
graphicx \includegraphics
booktabs Professional-looking tables
xcolor Colours
hyperref Clickable links; load it late
enumitem Control over lists
siunitx Units and number formatting
listings or minted Source code
csquotes Correct quotation marks (required by biblatex)

Use only what you need. Each package increases compile time and the chance of a clash.

The stand-in below models how LaTeX reacts when the same package is loaded twice with different options, one of the most common causes of the error "Option clash for package".

JS Option clash detector

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Option clashes and load order

When package A loads package B internally, B is loaded without options. If you then write \usepackage[opts]{B} afterwards, LaTeX reports an option clash. Solutions:

  1. Load B with its options before A.
  2. Pass the options through the class: \documentclass[dvipsnames]{article}.
  3. Use \PassOptionsToPackage{dvipsnames}{xcolor} before loading the package that pulls in xcolor (for example TikZ).

Two other order rules worth memorising:

  • Load hyperref almost last. Load cleveref after hyperref, and bookmark after hyperref.
  • Load babel before csquotes and before biblatex if you want language-aware quotes and bibliography text.

Checking what is loaded

Add \listfiles to the preamble and the log ends with a list of every file and version used; this is invaluable for bug reports and reproducing a build on another machine. Warnings such as Package xyz Warning: ... on input line 12 are not errors but should still be read.

Package or class? The \RequirePackage and \ProvidesPackage pair

If you write your own style file, call it mystyle.sty, start it with \ProvidesPackage{mystyle} and load other packages inside it with \RequirePackage. You can then keep your preamble in one file and reuse it across documents (see custom commands and environments).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where does \usepackage belong?

    Packages are loaded in the preamble: after the class declaration and before the document body.
  2. 2. What causes 'Option clash for package xcolor'?

    A package can only be loaded once. Loading it again with new options triggers the clash; load it first with all options or use \PassOptionsToPackage.
  3. 3. Which command opens a package's documentation from a terminal?

    texdoc searches the installed documentation and opens the PDF manual.
  4. 4. Where should hyperref be loaded relative to most other packages?

    hyperref redefines many commands, so it should come after the packages it patches; cleveref comes after hyperref.

Summary

  • Packages are loaded with \usepackage[options]{name} in the preamble.
  • texdoc name opens the manual; tlmgr or MiKTeX installs missing packages.
  • Keep a small starter set: geometry, microtype, amsmath, graphicx, booktabs, xcolor, hyperref.
  • Option clashes are solved by loading earlier, passing options through the class, or \PassOptionsToPackage.
  • Load hyperref late and cleveref after it; use \listfiles to record versions.

Next: Custom Commands and Environments