# Separation between caption and subcaption

Consider the following MWE

\documentclass{article}
\usepackage{caption}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\begin{subfigure}[t]{.5\linewidth}
\centering
\rule{3cm}{3cm}
\caption{First subfigure}
\end{subfigure}%
\begin{subfigure}[t]{.5\linewidth}
\centering
\rule{3cm}{3cm}
\caption{Second subfigure}
\end{subfigure}
\caption{A figure}
\end{figure}
\end{document}


How do I reduce from the preamble (either as a package option or a setting) the separation between the caption of the subfigures and the caption of the figure?

It is mostly an optical effect. The vertical space is the same as the one used between figure and caption, but it is measured from the bottom, which in the case of subfigures is at the lowest point in the parentheses.

Here's the proof:

\documentclass{article}
\usepackage{caption}
\usepackage{subcaption}

\DeclareRobustCommand{\vr}[1]{\smash{\rule{0.2pt}{#1}}}
\DeclareRobustCommand{\hr}{\makebox[0pt][l]{\rule{10cm}{0.2pt}}}

\begin{document}

\begin{figure}[htp]
\centering
\rule{6cm}{3cm}
\caption{A figure\vr{18pt}}
\end{figure}

\begin{figure}[htp]
\begin{subfigure}[t]{.5\linewidth}
\centering
\rule{3cm}{3cm}
\caption{\hr First subfigure}
\end{subfigure}%
\begin{subfigure}[t]{.5\linewidth}
\centering
\rule{3cm}{3cm}
\caption{Second subfigure}
\end{subfigure}

\caption{A figure\vr{18pt}}
\end{figure}
\end{document}


I added rules to show the spacing; the rule in the upper figure almost touches the black box; in the bottom case, it almost touches the level of the parentheses. Actually there is 1pt more in the bottom case, due to \lineskip.

If you want that the vertical spacing is measured from the baseline of the subfigures and your subcaptions are always one liners, then you might add

\AtBeginDocument{%
\def\endsubfigure{%
\par % ensure vertical mode
{\small\sbox0{()}\kern-\dp0}% back up by the depth of ()
\kern-\lineskip
\endminipage
}%
}


and the result would be

However, the backing up would be too much if a subcaption breaks across lines.

A much easier method would be

\captionsetup{skip=\dimexpr\abovecaptionskip-3pt}
\caption{A figure}


adjusting the 3pt to suit in the cases where this seems necessary. This will reduce the skip between the objects (subfigures and caption) by the stated amount.

Set the skip option, provided by caption. As suggested in the caption documentation, "[t]he vertical space between the caption and the figure or table contents is controlled by skip=<amount>":

\documentclass{article}
\usepackage{caption,subcaption,graphicx}
\begin{document}

\begin{figure}[t]
\begin{subfigure}[t]{.5\linewidth}
\centering
\includegraphics[width=3cm]{example-image-a}
\caption{First subfigure}
\end{subfigure}%
\begin{subfigure}[t]{.5\linewidth}
\centering
\includegraphics[width=3cm]{example-image-b}
\caption{Second subfigure}
\end{subfigure}
\caption{A figure}
\end{figure}

\begin{figure}[t]
\captionsetup{skip=0pt}
\begin{subfigure}[t]{.5\linewidth}
\centering
\includegraphics[width=3cm]{example-image-a}
\caption{First subfigure}
\end{subfigure}%
\begin{subfigure}[t]{.5\linewidth}
\centering
\includegraphics[width=3cm]{example-image-b}
\caption{Second subfigure}
\end{subfigure}
\caption{A figure}
\end{figure}

Hello world.

\end{document}


You can also set this in the document preamble.

This problem is quite difficult as there are several cases to look at. You could redefine your subfigure captions in order to subtract some vspace below, but this would look bad for multi-line subfigure groups.

Or you could get rid of some space above the caption by loading \usepackage[aboveskip=5pt]{caption} but this would be bad for normal figures.

Maybe it could be possible to check if there are subfigures in a figure but this would be quite a big hack. Or you could redefine the subfigure environment which should not be considered best practice neither.

I would recommend to put a special command in each figure which applies to this very problem. Or you write a special caption-command which you use in those cases. Both can be manipulated afterwards from your preamble.

I am showing both ways in the following:

% arara: pdflatex

\documentclass{article}
\usepackage{caption}
\usepackage{subcaption}
\newcommand*\myCaption[1]{\captionsetup{aboveskip=2pt}\caption{#1}} % 10 pt is the default
\newcommand{\mySubFigureHack}{\captionsetup{aboveskip=2pt}}

\begin{document}
\begin{figure}
\begin{subfigure}[t]{.5\linewidth}
\centering
\rule{3cm}{3cm}
\caption{First subfigure}
\end{subfigure}%
\begin{subfigure}[t]{.5\linewidth}
\centering
\rule{3cm}{3cm}
\caption{Second subfigure}
\end{subfigure}
\myCaption{A figure}
\end{figure}
\begin{figure}
\mySubFigureHack
\begin{subfigure}[t]{.5\linewidth}
\centering
\rule{3cm}{3cm}
\caption{First subfigure}
\end{subfigure}%
\begin{subfigure}[t]{.5\linewidth}
\centering
\rule{3cm}{3cm}
\caption{Second subfigure}
\end{subfigure}
\caption{A figure}
\end{figure}
\end{document}


To automatically shorten the vertical space between subfigure captions and figure captions (without affecting the vertical space before captions that don't have subfigures), the following code can be added to the preamble:

\usepackage{etoolbox}
\newcommand{\belowSubcaptionSkip}{-1ex}
\pretocmd{\figure}{%% \pretocmd{\figure}... is needed because \caption is not defined outside
\apptocmd{\endsubfigure}{\gdef\hackingMacroThatAlignsSubfigureCaptions{\vspace{\belowSubcaptionSkip}}}{}{}%% Remember that a subfigure occured. \gdef (global \def) is needed as normal \def does not work.
\pretocmd{\caption}{\hackingMacroThatAlignsSubfigureCaptions\gdef\hackingMacroThatAlignsSubfigureCaptions{}}{}{}%% Apply the skip if applicable. Reset the skip.
}{}{}
\def\hackingMacroThatAlignsSubfigureCaptions{} %% Avoid errors in case the first figure does not have subfigures


It turned out to be quite hacky due to some circumstances (Adding things to \endsubfigure works, but does not have an effect on following \captions, etc ...)