当包含图宽度小于子图宽度时,自动将子图居中

当包含图宽度小于子图宽度时,自动将子图居中

大家好,

我已经在这个社区工作了一段时间了。谢谢你们!但现在我似乎遇到了一个其他地方没有提到的问题。更准确地说,没有一个解决方案适合我的情况。

subfigure我想要做的是自动将包中的环境内容subcaption置于整个文档的中心。我已经看到了以下答案埃格尔米科,这对于正常环境来说工作得很好figure。但是,我想包含小于的数字width=1\linewidth,导致包含在内的数字subfigure左对齐。如果我\centering手动使用该命令,一切都正常。

我认为问题在于修补子图以某种方式失败了,这可以从我的辅助变量/命令\figureOK和中看出\subfigureOK。但这就是我陷入困境的地方。

按照 Mico 的方法figure(或者说所有浮点数),我还尝试使用\@subfloatboxresetAxel Sommerfeldt 建议的方法(在评论) 并由 提供subcaption。但这似乎根本没有效果。

我希望我的问题能得到解决。这是一个(希望)最小的工作示例。

\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}

在此处输入图片描述

更新: 日志文件这里在 OneDrive 上。

答案1

\subcaption@minipage未定义,因此您无法修补它。使用与主浮点相同的钩子:

\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

相关内容