1

I would like to ensure that content is always centred within subfloats (without the need for \centering in each one). This question was asked and answered over 6 years ago but, of the answers, the only one that still works as of January 2020 is:

\usepackage{etoolbox}
\makeatletter
\gappto\@floatboxreset{\centering\appto\@minipagerestore{\centering}}
\makeatother

Unfortunately, to quote the answerer:

but I wouldn't do it, as this would mean that all minipages in a float would get \centering; there are macros that internally use minipage that might be used in a float: also those would receive \centering, which could so end up in unwanted places.

Here is my MWE which uses the linked approach:

\documentclass{article}

    \usepackage{subcaption}
    \usepackage{etoolbox}

    \makeatletter
    \g@addto@macro\@floatboxreset\centering % Centre within every float
    \makeatother

    \makeatletter
    \gappto\@floatboxreset{\centering\appto\@minipagerestore{\centering}} % Centre within every subfloat
    \makeatother

\begin{document}

    \begin{table}
        \caption{Table}\label{fig}
        \begin{subtable}[H]{0.5\textwidth}
            \caption{Subtable}
            \begin{tabular}{c}
                \hline
                Tabular\\
                \hline
            \end{tabular}
        \end{subtable}
    \end{table}

\end{document}

Is there an alternate solution which circumvents the issues quoted above?

user1476176
  • 1,225
  • 2
    I do not want to disappoint you but IMHO if anything this shows that one should not do these hacks in order to just spare you from typing \centering in a few places. Any of such hacks has a great chance to cause trouble if another user has placed a feature request for a key doing this, and the feature request went through. –  Jan 22 '20 at 02:10

1 Answers1

0

This should work:

\documentclass{article}

\makeatletter
\g@addto@macro\@floatboxreset\centering % Centre within every float
\makeatother

\usepackage{subcaption}
\DeclareCaptionOptionNoValue{centering}{\centering}
\captionsetup[sub]{centering}

\begin{document}

\begin{table}
    \caption{Table}\label{fig}
    \begin{subtable}{0.5\textwidth}
        \caption{Subtable}
        \begin{tabular}{c}
            \hline
            Tabular\\
            \hline
        \end{tabular}
    \end{subtable}
\end{table}

% This could be overridden for single figures or tables:
\begin{table}
    \clearcaptionsetup[centering]{sub} % Remove "centering" from "sub" options
    \caption{Table}\label{fig2}
    \begin{subtable}{0.5\textwidth}
        \caption{Subtable}
        \begin{tabular}{c}
            \hline
            Tabular\\
            \hline
        \end{tabular}
    \end{subtable}
\end{table}

\end{document}

As an alternative \subcaptionbox could be used with option "c":

\documentclass{article}

\makeatletter
\g@addto@macro\@floatboxreset\centering % Centre within every float
\makeatother

\usepackage{subcaption}

\begin{document}

\begin{table}
    \caption{Table}\label{fig3}
    \subcaptionbox
       {Subtable}
       [0.5\textwidth][c]
       {\begin{tabular}{c}
            \hline
            Tabular\\
            \hline
        \end{tabular}}
\end{table}

\end{document}
  • 2
    There is NO mention of \DeclareCaptionOptionNoValue in the documentation of subcaption. And it is not mentioned in caption either. How do you think that people can know about this? I hope that the next version of the package will mention it, after a six year delay. – egreg Jan 22 '20 at 18:35
  • One could use \DeclareCaptionOption instead (and passing a non-used value to the centering option) and \DeclareCaptionOption is at least partly documented. 2. \subcaption@iminipage isn't documented either. I agree that using not-yet-documented commands is only marginal better than using internal commands, and that the situation about the caption package documentation is far from being optimal: https://gitlab.com/axelsommerfeldt/caption/issues/1 (Whoever is willing to help me is invited to do so.) However, I'm only here at SX to do damage control.
  • –  Jan 22 '20 at 18:42
  • You should at least remove the gratuitous sarcasm in the first paragraph. – egreg Jan 22 '20 at 20:38
  • @egreg I agree. I apologize, it wasn't meant as sarcasm but I tried to be funny. But I understand that it's too easy to misunderstand it, therefore I changed it. Thanks for the feedback. –  Jan 22 '20 at 20:42