1

Helloo everyone,

I've been working with this community for quite a while now. Thanks for that! But now I seem to have run into a problem mentioned nowhere else. To be more precise, none of the solutions worked for my case.

What I want to do is automatically centering the content of the subfigure environment from the subcaption package in the entire document. I've seen the answers from egreg and Mico, and this works just fine for a normal figure environment. However, I want to include figures with less than width=1\linewidth, causing the included figures within the subfigure to be left-aligned. If I use the \centering command manually, everything works fine.

I think the problem is that patching the subfigure somehow fails, as can be seen form my auxiliary variable/commands \figureOK and \subfigureOK. But that's where I'm stuck.

Following Mico's approach for the figure (or rather all floats), I also tried to use \@subfloatboxreset suggested by Axel Sommerfeldt (in the comments) and provided by subcaption. But this seems to have no effect at all.

I hope my problem becomes clear. Here is a (hopefully) minimal working example.

\documentclass{scrartcl}
\RequirePackage[utf8]{inputenc}
\setlength{\parindent}{0mm}

\usepackage{graphicx}
\usepackage{subcaption}

\providecommand{\figureOK}{\relax}
\providecommand{\subfigureOK}{\relax}


%%%------------------------------------------------------ Anwser from egreg
% https://tex.stackexchange.com/a/144857
\usepackage{etoolbox}
\makeatletter
    % Try to patch floats to use \centering
    \apptocmd\@floatboxreset{\centering}            % Try Patch
    {\renewcommand{\figureOK}{Yes}}                 % Execute on success
    {\renewcommand{\figureOK}{No}}                  % Execute on failure

    % Try to patch subfigure to use \centering
    \apptocmd\subcaption@minipage{\centering}
    {\renewcommand{\subfigureOK}{Yes}}
    {\renewcommand{\subfigureOK}{No}}
\makeatother
%%%------------------------------------------------------


%%%------------------------------------------------------ Answer from Mico
% https://tex.stackexchange.com/a/134889
%\usepackage{etoolbox}
%\makeatletter
%   % Try to patch floats to use \centering
%   \patchcmd{\@xfloat}%
%       {\@floatboxreset}%                  % Replace this      % Also tried this with \@subfloatboxreset
%       {\@floatboxreset\centering}         % With this
%       {\renewcommand{\figureOK}{Yes}}     % On success
%       {\renewcommand{\figureOK}{No}}      % On failure
%   
%   % Try to patch subfigure to use \centering
%   \patchcmd{\subcaption@minipage}%
%       {\setcaptionsubtype}%
%       {\centering\setcaptionsubtype}
%       {\renewcommand{\subfigureOK}{Yes}}
%       {\renewcommand{\subfigureOK}{No}}
%\makeatother
%%%------------------------------------------------------



%%%------------------------------------------------------ For Test only
\usepackage{showframe}
\renewcommand*\ShowFrameColor{\color{red}}
\renewcommand*\ShowFrameLinethickness{0.4pt}
%%%------------------------------------------------------

\begin{document}
    \section{Debug}
    Figures automatically centered? \figureOK

    Subfigures automatically centered? \subfigureOK

    \section{Tests}
    \begin{figure}[htbp]
        \includegraphics[width=0.25\linewidth]{example-image-A}
        \caption{Patching figures works well}
    \end{figure}
    \vfill
    \begin{figure}[htbp]
        \begin{subfigure}[b]{.5\linewidth}
            \centering      %   <-- This is what I want to get rid of
            \includegraphics[width=0.5\linewidth]{example-image-B}
            \subcaption{A subfigure}
        \end{subfigure}%
        \begin{subfigure}[b]{.5\linewidth}
            \centering      %   <-- This is what I want to get rid of
            \includegraphics[width=0.5\linewidth]{example-image-C}
            \subcaption{Another subfigure}
        \end{subfigure}
        \caption{A figure where subfigures are manually centered $\rightarrow$ works just fine}
    \end{figure}
    \vfill
    \begin{figure}[htbp]
        \begin{subfigure}[b]{.5\linewidth}
            \includegraphics[width=0.5\linewidth]{example-image-B}
            \subcaption{A subfigure}
        \end{subfigure}%
        \begin{subfigure}[b]{.5\linewidth}
            \includegraphics[width=0.5\linewidth]{example-image-C}
            \subcaption{Another subfigure}
        \end{subfigure}
        \caption{A figure where subfigures should be centered automatically $\rightarrow$ doesn't work}
    \end{figure}
\end{document}

enter image description here

Update: log files are here on OneDrive.

1 Answer 1

1

\subcaption@minipage is not defined, so you can't patch it. Use the same hook you are using for the main float:

\usepackage{etoolbox}
\makeatletter
    % Try to patch floats to use \centering
    \apptocmd\@floatboxreset{\centering}            % Try Patch
    {\renewcommand{\figureOK}{Yes}}                 % Execute on success
    {\renewcommand{\figureOK}{No}}                  % Execute on failure

\apptocmd\@subfloatboxreset{\centering}{}{\fail} %<----
\makeatother
5
  • This doesn't seem to change anything at all. The subfigures without the explicit \centering command remain left-aligned.
    – Axel
    Feb 28, 2020 at 11:18
  • 1
    It worked fine for me, show your log file. Feb 28, 2020 at 11:23
  • Thanks! I updated my question with a link to the log file.
    – Axel
    Feb 28, 2020 at 14:37
  • I get centered subfigures from your document, but my system is newer. Feb 28, 2020 at 14:55
  • Thanks alot for the quick help! I updated my Miktex and it works like a charm now. Interesting enough, I freshly installed Miktex just last month.
    – Axel
    Feb 28, 2020 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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