考虑以下 MWE:
\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{subcaption}
\usepackage{caption}
\begin{document}
\begin{figure}
\subcaptionbox{\label{fig:img1}}{%
\includegraphics[width=0.5\columnwidth]{image.jpg}}%
\subcaptionbox{\label{fig:img2}}{%
\includegraphics[width=0.5\columnwidth]{image.jpg}%
}
\caption{Caption}
\end{figure}
\end{document}
这一切都很好,但我希望实现的是主标题自动为我提供子图的范围(不用管这里的子标题)。所以在Figure 1
这种情况下,我希望 LaTeX 打印Figure 1a–b
(如果有更多子图,则进行相应调整)。
到目前为止,我已经用 手动重新定义了标题\renewcommand{\thefigure}{\arabic{figure}a--b}
,但我希望能够自动完成。
答案1
\subcaptionbox
可以通过以下方法解决此问题:
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{caption}
\usepackage{etoolbox}
\newcommand*\MySubcaptionFirst{}
\newcommand*\MySubcaptionLast{}
\makeatletter
\newcommand*\MySubcaptionRange
{%
\MySubcaptionFirst
\ifx\MySubcaptionLast\@empty
\else
--\MySubcaptionLast
\fi
}
\newcommand*\SubCaptionBoxPatch
{%
\ifx\MySubcaptionFirst\@empty
\expandafter\def\csname the\@captype\endcsname
{\arabic{\@captype}\MySubcaptionRange}%
\edef\MySubcaptionFirst{\csname thesub\@captype\endcsname}%
\else
\edef\MySubcaptionLast{\csname thesub\@captype\endcsname}%
\fi
}
\apptocmd \caption@iiibox
{\SubCaptionBoxPatch}
{}
{\GenericError{}{Patching failed}{}{}}
\makeatother
\begin{document}
\begin{figure}
\subcaptionbox{\label{fig:img1}}
{\includegraphics[width=0.45\columnwidth]{example-image-duck}}
\subcaptionbox{\label{fig:img2}}
{\includegraphics[width=0.45\columnwidth]{example-image-duck}}
\subcaptionbox{\label{fig:img3}}
{\includegraphics[width=0.45\columnwidth]{example-image-duck}}
\caption{Caption}
\end{figure}
\begin{figure}
\subcaptionbox{\label{fig:img1}}
{\includegraphics[width=0.5\columnwidth]{example-image-duck}}%
\subcaptionbox{\label{fig:img3}}
{\includegraphics[width=0.5\columnwidth]{example-image-duck}}%
\caption{Caption}
\end{figure}
\begin{figure}
\subcaptionbox{\label{fig:img3}}
{\includegraphics[width=0.5\columnwidth]{example-image-duck}}%
\caption{Caption}
\end{figure}
\begin{figure}
\caption{Caption}
\end{figure}
\end{document}
答案2
这里有一个使用 cleveref 来格式化标签范围的选项。您只需指定分配给子图的第一个和最后一个标签。
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{caption}
\usepackage{cleveref}
\crefrangeformat{figure}{#3#1#4--#5\crefstripprefix{#1}{#2}#6}
\newcommand{\subfigurecaption}[3]{%
\begingroup
\renewcommand{\thefigure}{\crefrange{#1}{#2}}
\caption{#3}
\endgroup
}
\begin{document}
\begin{figure}
\subcaptionbox{\label{fig:img1}}{%
\includegraphics[width=0.5\columnwidth]{example-image-duck}}%
\subcaptionbox{\label{fig:img2}}{%
\includegraphics[width=0.5\columnwidth]{example-image-duck}%
}
\subfigurecaption{fig:img1}{fig:img2}{Caption}
\end{figure}
\begin{figure}
\subcaptionbox{\label{fig:img3}}{%
\includegraphics[width=0.3\columnwidth]{example-image-duck}}%
\subcaptionbox{}{%
\includegraphics[width=0.3\columnwidth]{example-image-duck}}%
\subcaptionbox{\label{fig:img4}}{%
\includegraphics[width=0.3\columnwidth]{example-image-duck}%
}
\subfigurecaption{fig:img3}{fig:img4}{Caption}
\end{figure}
\end{document}