Typevia
Figures and Images

Introduction to Caption Customization

In LaTeX, captions provide essential context for figures and tables. While the default LaTeX settings are functional, they are often basic and may not meet the specific formatting requirements of a thesis, journal article, or professional report.

Customizing captions allows you to change font styles, alignment, labels (e.g., changing "Figure" to "Fig."), and the spacing between the image and the text. The most powerful and standard way to achieve this is by using the caption package.

Getting Started with the caption package

To begin customizing, you need to include the caption package in your document preamble (the area before \begin{document}).

\usepackage{caption}

The primary command for customization is \captionsetup. You can use this command in the preamble to apply settings globally (to every caption in the document) or inside a specific environment (like a figure or table) to apply settings locally.

The syntax for the command is: \captionsetup[type]{options}

  • type: (Optional) Specifies what you are targeting, such as figure or table. If omitted, settings apply to both.
  • options: A comma-separated list of "key=value" pairs that define the style.

Formatting Labels and Text

One of the most common requests is to change the appearance of the "Label" (e.g., Figure 1) and the "Text" (the actual description).

Changing Font Styles

You can modify the font weight, shape, and size using the labelfont and textfont keys.

\usepackage{caption}

% Set global caption styles
\captionsetup{
    labelfont=bf,      % Bold label (e.g., Figure 1)
    textfont=it,       % Italicized caption text
    font=small         % Smaller font for both label and text
}

Common Font Options:

  • Weights: bf (bold), md (medium).
  • Shapes: it (italic), sl (slanted), sc (small caps), up (upright).
  • Sizes: scriptsize, footnotesize, small, normalsize, large.

Changing the Separator

By default, LaTeX puts a colon and a space after the figure number (Figure 1: Description). You can change this using the labelsep key.

\captionsetup{labelsep=period} % Changes "Figure 1: " to "Figure 1. "

Other options include colon, space, quad, and endash.

Note: If you need to change the word "Figure" to something else, like "Image," you don't actually use the caption package. Instead, use: \renewcommand{\figurename}{Image}.

Alignment and Layout

When captions span multiple lines, their alignment becomes critical for readability. The caption package offers several ways to handle justification and margins.

Justification

You can control how the text is aligned relative to the page.

\captionsetup{
    justification=centering, % Centers the caption
    singlelinecheck=false    % Forces centering even if the caption is short
}
  • justified: Standard block justification (default).
  • centering: All lines are centered.
  • raggedright: Left-aligned text.

Hang and Indentation

The format key determines how the second and subsequent lines of a caption are treated.

  • plain: All lines start at the left margin.
  • hang: Subsequent lines are indented to start exactly where the caption text began on the first line.
\captionsetup{
    format=hang,
    margin=10pt % Adds extra space on both sides of the caption
}

Customizing Subfigures

When you have multiple images within one figure environment, you likely want to customize the "sub-captions" (e.g., (a), (b), (c)). For this, we use the subcaption package, which is designed to work seamlessly with the caption package.

Example: Customizing Sub-labels

In this example, we change the sub-labels to be bold and use square brackets.

\usepackage{subcaption}

% Customize subcaptions specifically
\captionsetup[subfigure]{
    labelfont=bf,
    labelformat=simple
}
% Add brackets manually to the label numbering
\renewcommand\thesubfigure{(\alph{subfigure})}

\begin{figure}[ht]
    \centering
    \begin{subfigure}{0.4\textwidth}
        \centering
        \includegraphics[width=\linewidth]{image1.png}
        \caption{Left view}
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.4\textwidth}
        \centering
        \includegraphics[width=\linewidth]{image2.png}
        \caption{Right view}
    \end{subfigure}
    \caption{A comparison of two views.}
\end{figure}

Best Practices and Tips

To maintain a professional look and avoid technical errors, keep these best practices in mind:

  1. Placement of the \label: In LaTeX, the \label command must come after the \caption command (or inside the caption argument). If you place the label before the caption, your cross-references (using \ref) will point to the wrong section or previous figure.
  2. Consistency is Key: Avoid changing caption styles for every individual figure. Define your \captionsetup in the preamble to ensure a uniform look throughout your document.
  3. Use Short Titles for Lists: If your caption is very long, use the optional argument for the List of Figures: \caption[Short Version for TOC]{Long, detailed version for the actual page.}.
  4. Tables vs. Figures: It is common practice to place table captions above the table and figure captions below the figure. You can automate the spacing for this:
    \captionsetup[table]{position=above, skip=10pt}
    \captionsetup[figure]{position=below, skip=10pt}

Common Mistakes to Avoid

  • Loading conflicting packages: Avoid using the older subfigure or subfig packages alongside the modern subcaption package, as they often conflict.
  • Manual Formatting: Don't use manual line breaks (\\) or font commands (\textbf{...}) inside the \caption{} command. Use \captionsetup instead to keep your code clean and your formatting consistent.
  • Ignoring Margins: On very narrow captions, justified text can look awkward with large gaps between words. In these cases, use justification=raggedright.

By mastering the caption package, you gain full control over the visual presentation of your data and imagery, ensuring your document adheres to any style guide with ease.