5

Sometimes I need to reduce the space between sub figures and sub captions, so I insert \vspace{-number cm} to do that. But, it will increase the space.

\documentclass{article}
\usepackage{lipsum,graphicx}
\usepackage{caption,subcaption}
\begin{document}
\lipsum[1]\par
\begin{figure}
\begin{subfigure}{0.5\textwidth}
\includegraphics[width=1\textwidth]{example-image-a}
\vspace*{-0.2cm}
\caption{Test1}
\end{subfigure}\hfill
\begin{subfigure}{0.5\textwidth}
\includegraphics[width=1\textwidth]{example-image-a}
\vspace*{-0.2cm}
\caption{Test2}
\end{subfigure}\\
\begin{subfigure}{0.5\textwidth}
\includegraphics[width=1\textwidth]{example-image-a}
\caption{Test3}
\end{subfigure}\hfill
\begin{subfigure}{0.5\textwidth}
\includegraphics[width=1\textwidth]{example-image-a}
\caption{Test4}
\end{subfigure}\\
\end{figure}
\lipsum[2]
\end{document}

enter image description here

It is very weird. How should I reduce the space between sub figures and sub captions?

Thanks.

1

2 Answers 2

7

LaTeX starts horizontal mode when a image is inserted by \includegraphic. I.e. the image is a first letter of a paragraph. Then a space is here from the end of line a then \vadjust which is a primitive used by the \vspace macro. Then the \caption finalizes the paragraph using \par and starts a new paragraph.

The width of the image is exactly equal to current \hsize value, so the space between image and \vadjust is good beak point and the paragraph is broken here. So, you have image at the first line and \vadjust plus glue from \parfillskip at the second (unwanted) line. After this line, the material from \vadjust (i.e. \vskip-0.2cm) is inserted.

If you remove the space between the image and the \vadjust, i.e. write:

\includegraphics[width=1\textwidth]{example-image-a}%

(note the percent character), then the following \vadjust immediately follows after the image and only single-line paragraph is created, no unwanted line is here.

Another solution: use \vskip-0.2cm primitive instead \vspace macro. This primitive inserts \par before it, i.e. the paragraph is finalized as one-letter paragraph with the image only and no unwanted line is here. This is because the \par primitive removes the last glue from the horizontal list (the glue inserted here from the space at the end of line).

I recommend to use \vskip instead \vspace always because \vspace brings much less trouble: \vskip doesn't insert any obscure \vadjust and the horizontal mode is finalized before\vskip which is more understandable behavior.

Note that you have another bug in your code: the last \\ in your code is used at the end of the paragraph which consists from four "letters": subfigure, subfigure, break, subfigue, subfigure. The \\ works like \vfil\break in this context. If it is used at the end of the paragraph then a new unwanted underfull empty line is created and the Underfull hbox warning message is printed as you can see in your log file.

4

The right tool for reducing the space between the subfigure and the subcaption is acting on the setup. The command \vspace is out of place here.

Use blank lines rather than \\. The code will be more readable and more easily maintainable. I also added some vertical separation between the two rows of subfigures.

\documentclass{article}
\usepackage{lipsum,graphicx}
\usepackage{caption,subcaption}

\begin{document}

\lipsum[1]

\begin{figure}[htp]
\captionsetup[subfigure]{skip=2pt}

\begin{subfigure}{0.5\textwidth}
\includegraphics[width=1\textwidth]{example-image-a}
\caption{Test1}
\end{subfigure}\hfill
\begin{subfigure}{0.5\textwidth}
\includegraphics[width=1\textwidth]{example-image-a}
\caption{Test2}
\end{subfigure}

\bigskip

\begin{subfigure}{0.5\textwidth}
\includegraphics[width=1\textwidth]{example-image-a}
\caption{Test3}
\end{subfigure}\hfill
\begin{subfigure}{0.5\textwidth}
\includegraphics[width=1\textwidth]{example-image-a}
\caption{Test4}
\end{subfigure}

\end{figure}

\lipsum[2]

\end{document}

enter image description here

3
  • Is there a way to change the space of the specified graphs?
    – Y. zeng
    Commented May 20, 2023 at 8:24
  • @Y.zeng Sorry, what space? If you want to modify a single subcaption, you can use \captionsetup inside the subfigure environment.
    – egreg
    Commented May 20, 2023 at 8:28
  • Good way. Just the space between figure and its caption.
    – Y. zeng
    Commented May 20, 2023 at 12:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .