Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

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

2 Answers 2

up vote 8 down vote accepted

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

share|improve this answer
    
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
1  
@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}

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

    \begin{figure}
        \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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.