8

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):

result

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

3
  • 1
    Have you tried \AtBeginEnvironment{subfigure}{\centering}?
    – cgnieder
    Nov 29, 2013 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, 2013 at 16:42
  • 1
    @someonr You're forgetting that \apptocmd has four arguments (usually the last two are {}{}).
    – egreg
    Nov 29, 2013 at 17:24

2 Answers 2

9

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
% Do the patch before loading caption!

% 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.

enter image description here

5
  • 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, 2013 at 19:26
  • 1
    @someonr \@floatboxreset has been there from the beginning; for subfigure I believe that Axel has some ideas about an interface.
    – egreg
    Nov 29, 2013 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. Nov 29, 2013 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, 2013 at 21:56
  • Once again, a tabular would work better than subfigure. Nov 29, 2013 at 22:31
2

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}

tabular

2
  • 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? May 10, 2015 at 0:03
  • 1
    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). May 10, 2015 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .