Typevia
Figures and Images

Introduction to Subfigures and Subtables

In technical and academic writing, you often need to group related images or data tables together. For example, you might want to show two different viewpoints of the same 3D model or compare two data sets side-by-side.

While you could simply place two images next to each other, LaTeX provides a more sophisticated way to handle this: subfigures and subtables. Using these allows you to:

  1. Give each element its own sub-caption (e.g., "Figure 1a", "Figure 1b").
  2. Have a main caption that describes the entire group.
  3. Reference individual sub-elements independently in your text.

In modern LaTeX, the gold standard for this functionality is the subcaption package.


Setting Up Your Document

Before you can create subfigures, you must include the subcaption package in your document's preamble. Since you will likely be working with images, you should also include the graphicx package.

\documentclass{article}

% Required packages
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{subcaption} % The key package for subfigures/subtables

\begin{document}
% Your content goes here
\end{document}

Note: You may encounter older tutorials mentioning the subfigure or subfig packages. These are now considered deprecated (obsolete). The subcaption package is more stable and works better with modern LaTeX classes.


Creating Your First Subfigures

The logic behind subfigures is hierarchical. You create a standard figure environment (the "parent"), and inside it, you place multiple subfigure environments (the "children").

Each subfigure requires a width argument. A common practice is to use a fraction of the \textwidth.

Example: Side-by-Side Images

\begin{figure}[htbp]
     \centering
     \begin{subfigure}[b]{0.45\textwidth}
         \centering
         \includegraphics[width=\textwidth]{graph_a.png}
         \caption{Data from Group A}
         \label{fig:group_a}
     \end{subfigure}
     \hfill
     \begin{subfigure}[b]{0.45\textwidth}
         \centering
         \includegraphics[width=\textwidth]{graph_b.png}
         \caption{Data from Group B}
         \label{fig:group_b}
     \end{subfigure}
     \caption{Comparison of experimental results between two groups.}
     \label{fig:comparison}
\end{figure}

Breakdown of the Code:

  • \begin{subfigure}[b]{0.45\textwidth}: This creates a container that is 45% of the page width. The [b] aligns the subfigures along their bottom edges.
  • \hfill: This command is crucial. It adds horizontal space between the two subfigures, pushing them to the far left and far right of the page margins.
  • width=\textwidth inside \includegraphics: Note that this \textwidth now refers to the width of the subfigure container (the 0.45), not the whole page. This ensures the image fits perfectly inside its assigned slot.

Arranging Layouts: Rows and Columns

By default, LaTeX will try to place subfigures side-by-side if there is enough horizontal space. If the combined width of your subfigures exceeds 1.0\textwidth, the next subfigure will automatically wrap to a new line.

Example: A 2x2 Grid

To create a grid, you simply group your subfigures and use a blank line or a double backslash (\\) to force a line break.

\begin{figure}[htbp]
    \centering
    % Top Row
    \begin{subfigure}[b]{0.4\textwidth}
        \includegraphics[width=\textwidth]{image1}
        \caption{First}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.4\textwidth}
        \includegraphics[width=\textwidth]{image2}
        \caption{Second}
    \end{subfigure}
    
    \vspace{1cm} % Add some vertical space between rows

    % Bottom Row
    \begin{subfigure}[b]{0.4\textwidth}
        \includegraphics[width=\textwidth]{image3}
        \caption{Third}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.4\textwidth}
        \includegraphics[width=\textwidth]{image4}
        \caption{Fourth}
    \end{subfigure}
    \caption{A complex 2x2 arrangement of images.}
\end{figure}

Working with Subtables

Subtables work exactly like subfigures, but you use the subtable environment instead. This is perfect when you have two small tables that relate to the same topic.

\begin{table}[htbp]
    \centering
    \caption{Global Experimental Parameters}
    \begin{subtable}[b]{0.45\textwidth}
        \centering
        \begin{tabular}{|l|l|}
            \hline
            Variable & Value \\ \hline
            Temp & 25C \\ \hline
        \end{tabular}
        \caption{Control Group}
    \end{subtable}
    \hfill
    \begin{subtable}[b]{0.45\textwidth}
        \centering
        \begin{tabular}{|l|l|}
            \hline
            Variable & Value \\ \hline
            Temp & 40C \\ \hline
        \end{tabular}
        \caption{Test Group}
    \end{subtable}
\end{table}

Cross-Referencing Sub-elements

One of the most powerful features of using these environments is the ability to refer to specific parts of your figure in your text.

  1. Place a \label{} inside the subfigure environment.
  2. Place a \label{} after the main caption of the figure.
As shown in Figure \ref{fig:comparison}, the overall trend is upward. 
However, Figure \ref{fig:group_a} shows a sharper increase than 
Figure \ref{fig:group_b}.

If Figure 1 contains your subfigures, this will output: "As shown in Figure 1... Figure 1a shows a sharper increase than Figure 1b."


Best Practices and Tips

1. Watch Your Widths

If your subfigures are jumping to the next line when you want them side-by-side, check your widths. If you have two subfigures at 0.5\textwidth, any tiny amount of whitespace between the code blocks will count as a space character, making the total width > 1.0, causing a wrap.

  • Tip: Use 0.48\textwidth or add a percent sign % immediately after the \end{subfigure} to comment out the end-of-line space.

2. Alignment

Use the optional position argument [t] (top), [c] (center), or [b] (bottom) to align subfigures of different heights.

  • Example: \begin{subfigure}[t]{0.3\textwidth} will align the tops of all images in that row.

3. Adding Space

  • Use \hfill for maximum horizontal separation.
  • Use \hspace{1cm} for a specific amount of horizontal space.
  • Use \vspace{0.5cm} to separate rows of figures.

4. Common Pitfall: Labels

Always put the \label after the \caption. If you put it before, LaTeX might reference the section number or the previous figure number instead of the current one.

5. Customizing Labels

If you want your subfigures to be labeled with Roman numerals (i, ii) or capital letters (A, B) instead of lowercase (a, b), you can add this to your preamble:

\renewcommand\thesubfigure{(\Alph{subfigure})}

This would change "1a" to "1(A)".