When using the subcaption
package to place tables1 side-by-side, the spacing between a table and its subtable caption is too small if the table caption is placed above the tables and the subtable captions are placed below the tables, as shown in the following example:
\documentclass{article}
\usepackage{booktabs,caption,subcaption}
\newcommand\exampletable{
\begin{tabular}{ll}
\toprule
A & B\\
\bottomrule
\end{tabular}
}
\begin{document}
1) This looks OK:
\begin{table}[!h]
\centering
\begin{subtable}[t]{10em}
\centering
\exampletable
\caption{Subcaption 1}
\end{subtable}
\begin{subtable}[t]{10em}
\centering
\exampletable
\caption{Subcaption 2}
\end{subtable}
\caption{Main caption}
\end{table}
2) And this looks OK:
\begin{table}[!h]
\caption{Main caption}
\centering
\begin{subtable}[t]{10em}
\caption{Subcaption 1}
\centering
\exampletable
\end{subtable}
\begin{subtable}[t]{10em}
\caption{Subcaption 2}
\centering
\exampletable
\end{subtable}
\end{table}
3) Even this looks OK:
\begin{table}[!h]
\centering
\begin{subtable}[t]{10em}
\caption{Subcaption 1}
\centering
\exampletable
\end{subtable}
\begin{subtable}[t]{10em}
\caption{Subcaption 2}
\centering
\exampletable
\end{subtable}
\caption{Main caption}
\end{table}
4) But in this case, the spacing isn't nice:
\begin{table}[!h]
\caption{Main caption}
\centering
\begin{subtable}[t]{10em}
\centering
\exampletable
\caption{Subcaption 1}
\end{subtable}
\begin{subtable}[t]{10em}
\centering
\exampletable
\caption{Subcaption 2}
\end{subtable}
\end{table}
\end{document}
This produces something that looks like this:
Table 4 is the problematic case. I would prefer it to look more like this instead:
Any ideas on how to fix this? Ideally, I would want to have the right spacing "automagically" without having to insert explicit vspaces or anything similar inside the subtable environments.
1: the same would apply to figures, or other kinds of subfloats.
Best Answer
After Johannes_b pointed me in the right direction, it turned out that using
\captionsetup[subtable]{position=auto}
fixes the spacing in all four cases.Long answer
After some experimentation, it seemed to me that the
\caption
of the outertable
environment somehow affected the\caption
used in the innersubtable
environment. This led med to attempt wrapping the table\caption
in a\begingroup
-\endgroup
pair to isolate any side-effects. In the following MWE, this was done with the problematic case 4:And the result confirms the suspicion:
Table 2 in the image above corresponds to the case where
\caption
was used within a group.After some digging through the
caption
source code, it turns out that the\caption@position
macro is set as a result of using\caption
in thetable
environment (it is undefined initially, unless overridden globally), and it is tested for with the\caption@iftop
macro when\caption
is used again in thesubtable
environment. In other words, the position attribute of the caption in the outer environment will override the caption position in nested environments, if their\caption
commands succeed the\caption
command of the outer environment, and the caption positions of the nested environments haven't been set explicitly.To confirm this, I tried to reset the value of
\caption@position
after the table caption:and it turned out that this had the same effect as wrapping the
\caption
in a group.Note that the code above should not be used as a solution to this problem! Even the caption package source code states that the
\caption@position
macro should not be depended on.So why does position=auto work?
It turns out that using
\captionsetup[subtable]{position=auto}
will result in a call to\caption@setposition{auto}
before the subtable caption is output, and theauto
argument will simply do\let\caption@position\@undefined
, which is exactly what we wanted.