禁忌中的子标题框:子图编号错误

禁忌中的子标题框:子图编号错误

我正在使用tabu图形内部的环境来对多面板图形进行良好的对齐。但是,这似乎给我带来了一些子图编号问题(第二个环境中子图的编号以 c) 而不是 a 开头),即继续从前面的图形计数)。请参见下图。

我在这里做错了什么?

PS:我正在使用 Texlive 2018,它似乎发生在 lualatex 和 pdflatex 上。

\documentclass{article}

\usepackage{mwe,graphicx,tabu,subcaption,float}

\begin{document}
    \begin{figure}[H]
        \subcaptionbox{}{\includegraphics[width=1cm]{example-image-a}}
        \subcaptionbox{}{\includegraphics[width=1cm]{example-image-b}}
    \end{figure}
    \begin{figure}[H]
        \begin{tabu} to \linewidth {XX}
            \subcaptionbox{}{\includegraphics[width=1cm]{example-image-a}}&%
            \subcaptionbox{}{\includegraphics[width=1cm]{example-image-b}}\\
            \subcaptionbox{}{\includegraphics[width=1cm]{example-image-a}}&%
            \subcaptionbox{}{\includegraphics[width=1cm]{example-image-b}}%
        \end{tabu}
    \end{figure}
\end{document}

在此处输入图片描述

答案1

resp caption.subcaption包需要跟踪当前图形中的标题或子标题是否已排版。如果到目前为止它们都未排版,并且出现\caption或,则需要增加计数器并重置计数器。\subcaption(box)figuresubfigure

听起来很简单,但是为什么这个简单的机制在这里不起作用呢?因为tabu环境在内部被排版了三次。

尝试一下这个:

\documentclass{article}

\usepackage{mwe,graphicx,tabu,subcaption,float}

\makeatletter
\newcommand\showsubcaptionflag{%
  \show\ifcaption@subcaption}
\makeatother

\begin{document}
    \begin{figure}[H]
        \subcaptionbox{}{\includegraphics[width=1cm]{example-image-a}}
        \subcaptionbox{}{\includegraphics[width=1cm]{example-image-b}}
    \end{figure}
    \begin{figure}[H]
        \begin{tabu} to \linewidth {XX}
            \showsubcaptionflag
            \subcaptionbox{}{\includegraphics[width=1cm]{example-image-a}}&%
            \subcaptionbox{}{\includegraphics[width=1cm]{example-image-b}}\\
            \subcaptionbox{}{\includegraphics[width=1cm]{example-image-a}}&%
            \subcaptionbox{}{\includegraphics[width=1cm]{example-image-b}}%
        \end{tabu}
    \end{figure}
\end{document}

您将会看到将\showsubcaptionflag被调用三次,并且第一次未设置内部子标题标志,但是它在第2次和第3次时被设置(因为\subcaptionbox已经出现),这导致包在第2次和第3次运行时subcaption不增加figure计数器并且不重置计数器。subfiguretabu

tabu包尽力避免内部对表格进行三次排版所引起的副作用,但是它不了解subcaption包的内部标志,因此注定会失败。

解决方案:我将查看该tabu软件包是否为软件包作者提供了一个钩子,以便我可以执行与软件包兼容所需的操作caption。(如果没有,我能做的就很少了,尤其是因为软件包的作者tabu不再进行更改。)

caption解决方法:每次tabu排版表格时重置包的内部标志:

\documentclass{article}

\usepackage{mwe,graphicx,tabu,subcaption,float}

\makeatletter
\newcommand\clearsubcaptionflag{%
  \@ifundefined{caption@chgflag}{}{%
    \caption@clrflag{subcaption}}}
% Dirty Hack: The following line will reset the
% `subcaption` flag on every internal run of `tabu`:
\g@addto@macro\tabu@setstrategy{\noalign{\clearsubcaptionflag}}
\makeatother

\begin{document}
    \begin{figure}[H]
        \subcaptionbox{}{\includegraphics[width=1cm]{example-image-a}}
        \subcaptionbox{}{\includegraphics[width=1cm]{example-image-b}}
    \end{figure}
    \begin{figure}[H]
        \begin{tabu} to \linewidth {XX}
%           \clearsubcaptionflag % no subcaptions so far
            \subcaptionbox{}{\includegraphics[width=1cm]{example-image-a}}&%
            \subcaptionbox{}{\includegraphics[width=1cm]{example-image-b}}\\
            \subcaptionbox{}{\includegraphics[width=1cm]{example-image-a}}&%
            \subcaptionbox{}{\includegraphics[width=1cm]{example-image-b}}%
        \end{tabu}
    \end{figure}
\end{document}

PS:我在这里填写了一个错误报告:https://gitlab.com/axelsommerfeldt/caption/issues/27

答案2

显然subcaption不喜欢tabu(而且我也是同一边)。

\documentclass{article}

\usepackage{graphicx,tabu,subcaption}

\begin{document}

\begin{figure}[htp]
\subcaptionbox{}{\includegraphics[width=1cm]{example-image-a}}
\subcaptionbox{}{\includegraphics[width=1cm]{example-image-b}}
\end{figure}

\begin{figure}[htp]
\setcounter{subfigure}{0}
\begin{tabu} to \linewidth {XX}
  \subcaptionbox{}{\includegraphics[width=1cm]{example-image-a}} &
  \subcaptionbox{}{\includegraphics[width=1cm]{example-image-b}} \\
  \subcaptionbox{}{\includegraphics[width=1cm]{example-image-a}} &
  \subcaptionbox{}{\includegraphics[width=1cm]{example-image-b}}
\end{tabu}
\end{figure}

\end{document}

在此处输入图片描述

相关内容