Typevia
Figures and Images

Understanding Layout Control: Mini-pages and Sub-figures

By default, LaTeX is designed to flow text like a river—from the top of the page to the bottom. While this is perfect for standard paragraphs, you will often need more control over your layout. Perhaps you want to place two paragraphs side-by-side, or maybe you need to display three related images in a single row with their own individual captions.

To achieve these layouts, we use two essential tools: the minipage environment and the subcaption package for sub-figures.


The minipage Environment

The minipage is one of the most versatile tools in LaTeX. Think of it as a "page within a page." It creates a localized box of a specific width where you can put almost anything—text, equations, or images—and LaTeX will treat that entire box as a single block.

Syntax

The basic syntax for a minipage is:

\begin{minipage}[alignment]{width}
    % Your content goes here
\end{minipage}
  • alignment (optional): Controls how the minipage aligns vertically with surrounding elements. Use [t] for top, [b] for bottom, or [c] for center (default).
  • width: Specifies how wide the box should be. It is best practice to use relative units like \textwidth (e.g., 0.45\textwidth).

Example: Side-by-Side Text

If you simply write two paragraphs, LaTeX will put one under the other. To put them side-by-side, we wrap them in minipages.

\noindent
\begin{minipage}{0.45\textwidth}
    This is the left column. By using a width of 0.45, we leave some 
    room in the middle for a gap.
\end{minipage}
\hfill % This command pushes the two minipages apart
\begin{minipage}{0.45\textwidth}
    This is the right column. It stays level with the left column 
    because they are placed on the same line in the code.
\end{minipage}

Note: Always use \noindent before starting side-by-side minipages. Otherwise, LaTeX might add a paragraph indent to the first box, potentially pushing the second box to the next line.


Mastering Sub-figures with subcaption

While you can use a minipage inside a figure environment to place images side-by-side, you won't get automatic "Figure 1a" and "Figure 1b" labels. For that, we use the subcaption package.

Setting Up

First, you must include the necessary packages in your document preamble:

\usepackage{graphicx}
\usepackage{subcaption}

The subfigure Environment

The subfigure environment acts exactly like a minipage, but it is designed specifically to hold images and sub-captions.

\begin{figure}[htbp]
    \centering
    \begin{subfigure}[b]{0.4\textwidth}
        \centering
        \includegraphics[width=\textwidth]{image1.jpg}
        \caption{Description of first image}
        \label{fig:left_image}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.4\textwidth}
        \centering
        \includegraphics[width=\textwidth]{image2.jpg}
        \caption{Description of second image}
        \label{fig:right_image}
    \end{subfigure}
    \caption{A main caption describing both images.}
    \label{fig:whole_group}
\end{figure}

Why this works:

  1. The Figure Wrapper: The outer \begin{figure} environment makes the entire group a "float" that can have a main caption.
  2. Widths: Notice that the subfigure is set to 0.4\textwidth, and the \includegraphics inside it is set to width=\textwidth. This ensures the image fills 100% of the 40% space you allocated to the subfigure.
  3. The \hfill: This command is crucial. It acts like a spring, pushing the two images to the far left and far right of the page margin.

Managing Spacing and Alignment

One of the biggest frustrations for beginners is when side-by-side elements unexpectedly wrap to the next line. This usually happens because of hidden whitespace.

The Percent Sign (%) Trick

LaTeX treats a single line break in your code as a space. If you have two minipages that total 1.0\textwidth, that extra "space" from your line break will make the total width 1.0\textwidth + 1 space, which is too wide for the page.

To prevent this, use a % immediately after the \end{minipage}:

\begin{minipage}{0.5\textwidth}
    Content...
\end{minipage}% <--- No space allowed here!
\begin{minipage}{0.5\textwidth}
    Content...
\end{minipage}

Vertical Alignment

When images have different aspect ratios, they might look "bouncy" if not aligned correctly. Use the [t], [c], or [b] options on the subfigure or minipage environment to align them by their top, center, or bottom edges.


Practical Application: A Mixed Layout

Often, you may want to place text next to an image. This is a common requirement for lab reports or textbooks.

\begin{figure}[h]
    \begin{minipage}{0.6\textwidth}
        \centering
        \includegraphics[width=\textwidth]{graph.png}
        \caption{The experimental data results.}
    \end{minipage}
    \hfill
    \begin{minipage}{0.35\textwidth}
        \small
        \textbf{Observations:}
        \begin{itemize}
            \item The trend is linear.
            \item Outliers are visible at $x=10$.
            \item The slope is approximately 2.5.
        \end{itemize}
    \end{minipage}
\end{figure}

In this example, we used a figure environment so we could have a caption, but we used a minipage for the text block because it doesn't need its own "sub-figure" label.


Best Practices and Common Pitfalls

Best Practices

  • Use Relative Widths: Always use 0.5\textwidth rather than 8cm. This ensures your layout scales correctly if you change your page margins later.
  • Label Everything: Give both the main figure and each sub-figure a \label{}. You can then refer to the whole group (\ref{fig:whole_group}) or a specific part (\ref{fig:left_image}).
  • Check the Total Width: Ensure the sum of your minipage widths plus any \hspace or \hfill does not exceed 1.0\textwidth.

Common Pitfalls

  • Missing subcaption Package: If you try to use \begin{subfigure} without the package, LaTeX will throw an error.
  • The "subfigure" vs "subfig" Confusion: You might find old tutorials online mentioning the subfigure (obsolete) or subfig packages. Modern LaTeX users should almost always use the subcaption package as it is more compatible with other modern tools.
  • Forgetting \noindent: If your side-by-side layout is shifting slightly to the right, add \noindent before the first box.

Warning: Be careful with the [h] (here) placement specifier. If your minipages are too large, LaTeX will struggle to fit them "here" and might move them to the end of the document. Using [htbp] gives LaTeX more flexibility to find a good spot.