Introduction to Figures in LaTeX
Visualizing data and concepts is a critical part of academic and professional writing. While LaTeX is primarily a document preparation system for text, it handles images (referred to as "figures") with a high degree of precision.
In LaTeX, figures are typically handled as floats. This means LaTeX will automatically determine the best location for your image based on the surrounding text to ensure there are no awkward gaps or white spaces in your document. In this guide, we will cover everything from basic image insertion to managing complex layouts and cross-referencing.
1. Setting Up the Graphicx Package
Before you can insert any image, you must include the graphicx package in your document's preamble. This package provides the essential commands needed to process external image files.
Add this line to the very top of your LaTeX file:
\usepackage{graphicx}Note: Modern LaTeX engines (like pdfLaTeX) support various file formats, including
.png,.jpg,.eps. For the best results in digital documents, use high-resolution PNGs or vector-based PDFs.
2. Basic Image Insertion
The most basic way to insert an image is using the \includegraphics command. By default, LaTeX will insert the image at its original size.
Basic Syntax
\includegraphics{my-image-file}However, images are often too large or too small for the page. We use optional arguments inside square brackets [] to adjust the size and orientation.
Practical Example: Scaling and Resizing
% Scaling by a factor (0.5 = 50% of original size)
\includegraphics[scale=0.5]{beach_photo}
% Setting a specific width (relative to the text width)
\includegraphics[width=0.8\textwidth]{graph_data}
% Setting both width and height (can distort the image unless keepaspectratio is used)
\includegraphics[width=5cm, height=3cm, keepaspectratio]{logo}
% Rotating an image
\includegraphics[width=0.3\textwidth, angle=90]{vertical_map}Best Practice: Always use relative units like \textwidth or \linewidth instead of absolute units like cm or in. This ensures that if you change your page margins later, your images will resize automatically to fit the new layout.
3. Using the Figure Environment (Floats)
In professional documents, you rarely use \includegraphics on its own. Instead, you wrap it in a figure environment. This turns the image into a "float," allowing you to add captions, labels, and precise positioning.
The Standard Figure Template
\begin{figure}[htbp]
\centering
\includegraphics[width=0.7\textwidth]{experimental_setup}
\caption{This is the description of the experimental setup.}
\label{fig:setup}
\end{figure}Breaking Down the Components:
\begin{figure}[htbp]: Starts the float. The letters in brackets are positioning specifiers:h(here): Try to place the figure exactly where it appears in the code.t(top): Place at the top of a page.b(bottom): Place at the bottom of a page.p(page): Place on a separate page dedicated to floats.
\centering: Centers the image horizontally on the page.\caption{...}: Automatically numbers the figure (e.g., "Figure 1") and provides the text description.\label{fig:labelname}: Creates a hidden anchor so you can reference this figure later in your text using\ref{fig:labelname}.
Warning: Always place the
\labelcommand after the\captioncommand. If you place it before, LaTeX might incorrectly link the reference to the section heading instead of the figure.
4. Organizing Your Images
As your project grows, your main folder can become cluttered with image files. It is a best practice to keep all your images in a dedicated folder (e.g., a folder named images).
Instead of writing \includegraphics{images/my-photo}, you can tell LaTeX where to look for all images by adding this to your preamble:
\graphicspath{ {./images/} }Now, whenever you use \includegraphics, LaTeX will automatically look inside the images folder first.
5. Working with Subfigures
Sometimes you need to display two or more images side-by-side to compare results. To do this, we use the subcaption package.
Example: Side-by-Side Images
First, add \usepackage{subcaption} to your preamble.
\begin{figure}[ht]
\centering
\begin{subfigure}[b]{0.45\textwidth}
\centering
\includegraphics[width=\textwidth]{graph_a}
\caption{Result for Group A}
\label{fig:graph_a}
\end{subfigure}
\hfill % Adds horizontal space between the images
\begin{subfigure}[b]{0.45\textwidth}
\centering
\includegraphics[width=\textwidth]{graph_b}
\caption{Result for Group B}
\label{fig:graph_b}
\end{subfigure}
\caption{Comparison of experimental results between both groups.}
\label{fig:comparison}
\end{figure}In this example, each subfigure takes up 45% of the text width (0.45\textwidth), leaving a small gap in between. Each subfigure gets its own sub-caption (e.g., "1a", "1b"), while the main figure environment provides the primary caption.
6. Best Practices and Common Pitfalls
To ensure a smooth experience with figures, keep these tips in mind:
- Avoid spaces in filenames: Use underscores or hyphens (e.g.,
data_plot.pnginstead ofdata plot.png). Spaces often cause errors during compilation. - Don't fight the float: Beginners often get frustrated when LaTeX moves an image to the next page. Trust the system! If you absolutely must force an image to stay "here," use the
floatpackage and the[H](capital H) specifier. - Cross-referencing: Never type "as seen in Figure 1." Instead, use "as seen in Figure \ref{fig:my_label}." If you add another figure earlier in the document, LaTeX will automatically renumber all your figures and update the text for you.
- Resolution: Aim for 300 DPI (dots per inch) for print documents. If your image looks blurry, it's likely a resolution issue, not a LaTeX issue.
Troubleshooting: "File not found"
If LaTeX can't find your image:
- Check the file extension (is it
.jpgor.jpeg?). - Ensure the file is in the same folder as your
.texfile (or in the path specified by\graphicspath). - Check for typos in the filename. LaTeX is case-sensitive!
