How to Avoid Underfull \hbox Warnings with Subfigures
At a glance
- 1. Check the warning
Use the log location and nearby source lines to find which paragraph or box TeX could not set well.
- 2. Fit the subfigures
Choose subfigure widths whose total leaves room for the gap, and use linewidth for content inside each one.
- 3. Check paragraph breaks
Keep side-by-side subfigure environments in the same paragraph; avoid empty lines between them.
- 4. Fix the actual cause
Adjust widths or text as needed; do not hide the warning unless you have verified the output is acceptable.
An underfull \hbox warning means TeX had to stretch the horizontal glue in a line or box more than it considers acceptable. It does not mean that subfigures are inherently invalid, and the warning alone does not identify the precise cause.
For subfigures placed side by side, first check that their widths and the space between them fit within the text width. Then use the log to locate any loose line in the subfigure content or caption. The example below shows a standard layout using the subcaption package.
A minimal side-by-side example
The following document compiles without external image files. The rules stand in for images so you can test the layout directly.
\documentclass{article}
\usepackage{subcaption}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{subfigure}[t]{0.48\textwidth}
\centering
\rule{\linewidth}{35mm}
\caption{First panel}
\label{fig:first}
\end{subfigure}\hfill%
\begin{subfigure}[t]{0.48\textwidth}
\centering
\rule{\linewidth}{35mm}
\caption{Second panel}
\label{fig:second}
\end{subfigure}
\caption{A figure with two panels.}
\label{fig:combined}
\end{figure}
See Figure~\ref{fig:combined}.
\end{document}
Each subfigure takes 0.48\textwidth, so the two boxes use 96% of the line width and leave room for \hfill to provide a gap. The % prevents a line-ending space from being inserted after \hfill and before the second subfigure. Use \linewidth for content inside a subfigure: there it refers to the width available inside that subfigure, rather than the full page text width.
For real images, replace each \rule with, for example, \includegraphics[width=\linewidth]{image-file} and load graphicx with \usepackage{graphicx}. The subcaption package provides the subfigure environment and works with caption settings.
What the warning means
TeX builds a line by placing words and glue (flexible horizontal space) into a horizontal box, or \hbox. If a line cannot be set with an acceptable amount of stretching, TeX reports an underfull box. A corresponding overfull warning means the material is too wide to fit without extending past the allowed width.
The warning often points to a paragraph inside a subfigure rather than to the subfigure environment itself. For instance, a narrow panel may make a caption or a paragraph wrap awkwardly. The log usually includes a line number and wording such as in paragraph at lines ...; inspect that source area and the lines just before it. Warnings can also come from unrelated text near the figure, so do not assume the nearest \begin{subfigure} is responsible.
An underfull warning is not necessarily a visible problem. TeX reports the amount of stretch, not whether the final page looks bad. If the output has no conspicuously wide spaces or poor alignment, the warning may be harmless; otherwise, adjust the layout or wording.
Check widths and paragraph breaks
For two panels, widths such as 0.48\textwidth each leave a small amount of flexible space for a gap. If you use widths that add up to exactly \textwidth, any ordinary interword spaces between them can make the row too wide, and there is no positive-width gap; \hfill can stretch only if space remains. If their combined widths are greater than \textwidth, the row can become overfull. For three panels, choose smaller widths and check that all three boxes plus their gaps fit.
Keep adjacent subfigure environments in the same paragraph when you want them on one row. A blank line in LaTeX starts a new paragraph, which can push the next subfigure to a separate line. A normal source-code line break is usually just whitespace; the % in the example makes the intended join explicit.
If the warning points to caption text, try a shorter caption or a slightly wider subfigure. If it points to a paragraph inside a panel, rephrase it, adjust the panel width, or use a deliberate alignment style such as \raggedright where appropriate. Do not add manual \\ line breaks merely to silence the message; they can create uneven spacing or new layout problems.
Common mistakes
- Using widths that leave no room for a gap. Add the subfigure widths first, then allow space for the separator. For two equal panels with a visible gap, values below
0.5\textwidthare a useful starting point. - Using
\textwidthfor panel contents. Inside a subfigure,\textwidthstill refers to the document’s text block. For an image or other material intended to fill the panel, use\linewidthinstead. - Putting a blank line between panels. This creates a paragraph break and usually stacks the panels rather than placing them side by side.
- Treating every warning as a subfigure failure. Read the log location and check the actual rendered page. The warning may arise from a caption or another nearby paragraph.
- Suppressing the diagnostic without fixing anything. Increasing
\hbadnesscan hide some underfull-box messages, but it does not improve spacing. Change it only if you intentionally want fewer diagnostics and have checked the output.
When the panels should stack
If you want subfigures on separate rows, do not force them to share a line with \hfill. Give each its own paragraph, usually by inserting a blank line between environments, and choose a width that suits the page. The same diagnostic rule applies: investigate the line reported in the log, rather than trying to eliminate every warning indiscriminately.
References
- The subcaption package
- The LaTeX Companion, Third Edition
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.