# Append \centering to \begin{subfigure} (from subcaption package)

How can I append \centering to \begin{subfigure} (from subcaption package)? I tried using the etoolbox , but for some reason it won't work for the subfigure enviroment (only working for the figure enviroment).

Here a MWE to demonstrate my problem.

\documentclass{article}

\usepackage{etoolbox}
\usepackage[margin=1em]{subcaption}

\usepackage{mwe}

\apptocmd\figure{\centering}
\apptocmd\subfigure{\centering}

\begin{document}

\begin{figure}
\includegraphics[width=4cm]{example-image}
\caption{qqq qqq qqq qqq qqq qqq }
\end{figure}

\begin{figure}
\begin{subfigure}{4cm}
\includegraphics[width=2cm]{example-image-a}
\caption{abc abc abc abc abc  }
\end{subfigure}%
\begin{subfigure}{4cm}
\includegraphics[width=2cm]{example-image-b}
\caption{abc abc abc abc abc abc abc abc abc abc }
\end{subfigure}
\caption{qqq qqq qqq qqq qqq qqq }
\end{figure}
\end{document}


result (with image a, b misplaced):

I also tried \AtBeginEnvironment{subfigure}{\centering} and to manually patch it with \let, but no luck.

-
Have you tried \AtBeginEnvironment{subfigure}{\centering}? – clemens Nov 29 '13 at 16:38
Yes, but also not working. I also tried the manually with \let, but no luck. (sry, forgot to mention that in the question) – someonr Nov 29 '13 at 16:42
@someonr You're forgetting that \apptocmd has four arguments (usually the last two are {}{}). – egreg Nov 29 '13 at 17:24

Your patch to \figure is not correct: you lose the possibility of specifying the float positioning argument, so no \begin{figure}[htp] with that.

It's better to go deeper. I propose two patches for figure: the first adds \centering only to it (uncomment the three lines if you want it), the second just adds it to all floats.

It's difficult to distinguish between subfigure and subtable. The patch for centering them can be found in Add \centering to all subfigures in subcaption using \g@addto@macro

\documentclass{article}

\usepackage{etoolbox}
\makeatletter

% This patch adds \centering only to figure
%\apptocmd{\@xfloat}{\position@float{#1}}{}{}
%\def\position@float#1{\csname position#1\endcsname}
%\def\positionfigure{\centering}

% This patch adds \centering to all floats
\appto\@floatboxreset{\centering}
\makeatother

\usepackage[margin=1em]{subcaption}
\makeatletter
\apptocmd\subcaption@minipage{\centering}{}{}
\makeatother

\usepackage{mwe}

\begin{document}

\begin{figure}

\includegraphics[width=4cm]{example-image}

\caption{qqq qqq qqq qqq qqq qqq }

\end{figure}

\begin{figure}

\begin{subfigure}{4cm}
\includegraphics[width=2cm]{example-image-a}
\caption{abc abc abc abc abc  }
\end{subfigure}%
\begin{subfigure}{4cm}
\includegraphics[width=2cm]{example-image-b}
\caption{abc abc abc abc abc abc abc abc abc abc }
\end{subfigure}

\caption{qqq qqq qqq qqq qqq qqq }

\end{figure}
\end{document}


Note that you must patch \@xfloat before loading subcaption or caption.

-
Thx, for the working patch. How did you know the commands to patch? By reading the source or is there some shortcut ;) ? Since the macros aren't part of the "public interface", this could break with any update? – someonr Nov 29 '13 at 19:26
@someonr \@floatboxreset has been there from the beginning; for subfigure I believe that Axel has some ideas about an interface. – egreg Nov 29 '13 at 20:17
@egreg -- although what you have is what was in the original question, it would look nicer if the bottoms of the subfigures were aligned, and similarly, the top lines of their captions. – barbara beeton Nov 29 '13 at 21:53
@barbarabeeton It suffices to use \begin{subfigure}[t]{4cm} in both; this assuming that the two pictures have the same height. – egreg Nov 29 '13 at 21:56
Once again, a tabular would work better than subfigure. – John Kormylo Nov 29 '13 at 22:31

Speaking of using a tabular...

\documentclass{article}

\usepackage{mwe}

\begin{document}

\null\vfil\begin{figure}[h]
\centering
\includegraphics[width=4cm]{example-image}
\caption{qqq qqq qqq qqq qqq qqq }
\end{figure}

\begin{figure}[h]
\centering
\begin{tabular}{cc}
\includegraphics[width=2cm]{example-image-a}&
\includegraphics[width=2cm]{example-image-b}\\
(a) abc abc abc abc abc&
\parbox[t]{4cm}{(b) abc abc abc abc abc abc abc abc abc abc}
\end{tabular}
\caption{qqq qqq qqq qqq qqq qqq }
\end{figure}
\end{document}


-
why the figures go to the top of tha page instead of being in the middle of the text I would like to put them? – Francisco Maerle May 10 '15 at 0:03
Vertical position is controlled by the options of figure[tbhp]. I modified the above to center the "text" and use the "here" option. A more reliable method would be to use one full page figure (with multiple captions). – John Kormylo May 10 '15 at 15:09