How can I automatically center all floats including subfloats?

The most recent versions of subcaption provide a \@subfloatboxreset hook.

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{subcaption}

\makeatletter % inserts a "\centering" to every floatbox.
\g@addto@macro\@floatboxreset{\centering}
\g@addto@macro\@subfloatboxreset{\centering}
\makeatother

\begin{document}

\begin{figure}[htp]

X\dotfill X

\begin{subfigure}[b]{.4\linewidth}
  X\dotfill X

  \includegraphics[width=.5\linewidth]{qwerty}
\end{subfigure}\qquad
\begin{subfigure}[b]{.4\linewidth}
  X\dotfill X

  \includegraphics[width=.5\linewidth]{qwerty}
\end{subfigure}

\caption{Caption}\label{fig:label} 
\end{figure}

\end{document}

enter image description here


Original answers follow

If you're willing to use subcaption, then

\g@addto@macro\subcaption@minipage\centering

should work for subfigure and subtable environments.


Added November 13, 2013

A revision of subcaption has made \subcaption@minipage a macro with arguments, so the above patch doesn't work any more and

\usepackage{etoolbox}

\makeatletter
\apptocmd\subcaption@minipage{\centering}{}{}
\makeatother

becomes necessary.


I wouldn't add \centering to the code for general minipage environments, even locally in a float, because they might pop up in unexpected places when using macros that exploit minipage in their code.

You could use a “local” version of \g@addto@macro for adding \centering to all minipage environments in a float; \appto of etoolbox is good for this:

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

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.


You could insert the following instructions in the preamble of your document to center-set the contents of table, figure, subtable, and subfigure environments. To affect the positioning of the latter two environments, it is assumed that the subcaption package is loaded.

\usepackage{etoolbox}
\makeatletter
\patchcmd{\@xfloat}%
  {\@floatboxreset}%
  {\@floatboxreset\centering}{}{}
\patchcmd{\subcaption@minipage}%
  {\setcaptionsubtype}%
  {\centering\setcaptionsubtype}{}{}
\makeatother

The first macro that's patched, \@xfloat, is defined in the LaTeX kernel; the second patched macro, \subcaption@minipage, is provided by the subcaption package.