Introduction to LaTeX Packages
Think of LaTeX as a powerful computer operating system. Out of the box, it can do many things, but to unlock its full potential for specialized tasks—like drawing complex diagrams, formatting professional tables, or adding hyperlinks—you need to install "apps." In LaTeX, these apps are called packages.
A package is a collection of files that provide extra commands and environments. Most LaTeX distributions (like TeX Live or MiKTeX) come with thousands of packages pre-installed. To use them, you simply "load" them in your document's preamble.
How to Use a Package
To load a package, you use the \usepackage command in the preamble (the area between \documentclass{...} and \begin{document}).
\documentclass{article}
% Loading a package without options
\usepackage{amsmath}
% Loading a package with specific options
\usepackage[margin=1in]{geometry}
\begin{document}
Hello, world!
\end{document}1. Essential Layout and Formatting Packages
Standard LaTeX documents often have very wide margins by default. While this is typographically sound for readability, most modern academic and professional documents require more control over the page layout.
geometry
The geometry package is the industry standard for changing page sizes and margins. Instead of wrestling with complex internal LaTeX variables, you can set your dimensions in plain English.
fontenc and inputenc
While modern engines like XeLaTeX handle fonts differently, if you are using the standard pdfLaTeX engine, these two packages are essential for ensuring your document displays special characters (like accents or umlauts) correctly.
Example: Setting up your page layout
\usepackage[utf8]{inputenc} % Allows input of special characters
\usepackage[T1]{fontenc} % Correctly renders fonts with accents
\usepackage[a4paper, margin=2.5cm]{geometry} % Sets page size and margins2. Advanced Mathematics with amsmath and amssymb
If you are using LaTeX for technical writing, the amsmath package (provided by the American Mathematical Society) is not just recommended—it is practically mandatory.
Why use amsmath?
- Better alignment: It provides the
alignenvironment for multi-line equations. - Improved symbols: It adds commands for complex fractions, matrices, and accents.
- Mathematical Logic: It improves how LaTeX handles spacing around operators.
The amssymb package provides a massive library of mathematical symbols, such as the "blackboard bold" letters used for number sets (like $\mathbb{R}$ for real numbers).
Example: Using amsmath and amssymb
\usepackage{amsmath}
\usepackage{amssymb}
% Inside the document:
\begin{align}
a^2 + b^2 &= c^2 \\
\mathbb{R} &\subset \mathbb{C}
\end{align}3. Including Images and Color
By default, LaTeX cannot "see" image files. You must load a package to handle external graphics.
graphicx
This package provides the \includegraphics command. It allows you to insert .jpg, .png, and .pdf files into your document, as well as scale, rotate, and crop them.
xcolor
Standard LaTeX only supports a handful of basic colors. The xcolor package allows you to access a vast library of predefined colors (like RoyalBlue or ForestGreen) and even create your own using RGB or CMYK values.
Example: Inserting an image with a colored caption
\usepackage{graphicx}
\usepackage{xcolor}
\begin{document}
\textcolor{blue}{This is a blue heading.}
\begin{figure}[h]
\centering
\includegraphics[width=0.5\textwidth]{ocean_photo.jpg}
\caption{A beautiful view of the sea.}
\end{figure}
\end{document}4. Professional Tables and Hyperlinks
Standard LaTeX tables can look a bit "clunky." Additionally, in the digital age, we expect table of contents entries and URLs to be clickable.
booktabs
The booktabs package is highly recommended for creating professional-quality tables. It discourages the use of vertical lines (which are generally considered poor style in formal publishing) and provides improved horizontal rules like \toprule, \midrule, and \bottomrule.
hyperref
The hyperref package turns your internal cross-references, citations, and URLs into clickable links. It also handles the PDF metadata (like the Title and Author shown in your PDF viewer).
Note: The
hyperrefpackage is unique because it should almost always be the last package loaded in your preamble. This is because it needs to "overwrite" commands from other packages to make them clickable.
Example: A professional table and a link
\usepackage{booktabs}
\usepackage[colorlinks=true, linkcolor=blue]{hyperref}
\begin{document}
Check out our results in Table \ref{tab:results}.
\begin{table}[h]
\centering
\begin{tabular}{lrr}
\toprule
Item & Quantity & Price \\
\midrule
Widgets & 50 & \$10.00 \\
Gadgets & 20 & \$15.00 \\
\bottomrule
\end{tabular}
\caption{Inventory list}
\label{tab:results}
\end{table}
Visit our website: \url{https://www.latex-project.org}
\end{document}5. Best Practices and Common Pitfalls
As you start adding more packages to your document, keep these tips in mind to avoid errors and "spaghetti code."
Keep it Minimal
Don't copy-paste a massive block of packages from an old template if you don't know what they do. Only load the packages you actually need. This makes troubleshooting much easier.
Check for Conflicts
Some packages don't play well together. For example, subfigure and subfig are old packages that often conflict with modern methods of handling sub-images. If you get strange errors, try commenting out a package to see if the error disappears.
Order Matters
As mentioned earlier, hyperref should usually be last. Similarly, some packages require amsmath to be loaded before them. If a package documentation mentions an order, follow it strictly.
How to Find Documentation
If you want to know everything a package can do, you don't need to search the web. Most LaTeX distributions include a tool called texdoc.
- Open your terminal or command prompt.
- Type
texdoc package-name(e.g.,texdoc geometry). - The official PDF manual will open immediately.
Common Beginner Mistake: Loading the Same Package Twice
If you try to load a package twice with different options, LaTeX will throw an "Option clash" error.
% This will cause an error!
\usepackage[margin=1in]{geometry}
\usepackage[a4paper]{geometry}
% Do this instead:
\usepackage[margin=1in, a4paper]{geometry}By mastering these common packages, you transition from basic text entry to creating professional, high-quality documents that meet the standards of the world's top publishers and universities.
