我起初要求提供一种解决方案,可以自动为我提供标题中的子图范围。Skillmon 想出了一种解决方案,它与我原来的 MWE 兼容,但会导致和cleveref
(besidecaption
来自komascript
)出现问题。
这是新的 MWE:
\documentclass{scrartcl}
\usepackage[demo]{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
\usepackage{cleveref}
\crefrangelabelformat{figure}%
{\emph{#3#1#4--#5\crefstripprefix{#1}{#2}#6}}
\crefrangelabelformat{subfigure}%
{\emph{#3#1#4--#5\crefstripprefix{#1}{#2}#6}}
\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}}
\subcaptionbox{\label{fig:img3}}{%
\includegraphics[width=0.5\columnwidth]{image.jpg}}%
\subcaptionbox{\label{fig:img4}}{%
\includegraphics[width=0.5\columnwidth]{image.jpg}}
\begin{captionbeside}
{Caption}
\subcaptionbox{\label{fig:img5}}{\includegraphics[width=0.5\columnwidth]{image.jpg}}
\end{captionbeside}
\end{figure}
\crefrange{fig:img1}{fig:img4}
\end{document}
这里有几个问题:
- 编译时会产生错误(
Argument of \@cref@firsttok has an extra }.
) - 产生的引用
cleveref
是错误的。它应该是1a–d
,但它却是1a––cd
。 - 标题在环境中缺少子标题框
captionbeside
,因此只能打印1a–d
而不是1a–e
。
我或多或少了解了 的情况下发生了什么captionbeside
,但与 的互动cleveref
却超出了我的能力范围。
答案1
这是一个可以与您的 MWE 配合使用的版本。我将其工作方式更改为使用全局定义,而不是更改其所用计数器的格式,而是通过更改包caption
打印其标题标签的方式来进行。
\documentclass{scrartcl}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{caption}
\usepackage{etoolbox}
\usepackage{duckuments}% random pictures not every time the same, totally useless for serious documents
\AtBeginEnvironment{figure}{\ClearMySubcaptionThings}
\AtEndEnvironment{figure}{\ClearMySubcaptionThings}
\AtBeginEnvironment{table}{\ClearMySubcaptionThings}
\AtEndEnvironment{table}{\ClearMySubcaptionThings}
\newcommand*\ClearMySubcaptionThings
{%
\gdef\MySubcaptionFirst{}%
\gdef\MySubcaptionLast{}%
}
\newcommand*\MySubcaptionFirst{}
\newcommand*\MySubcaptionLast{}
\makeatletter
\newcommand*\MySubcaptionRange
{%
\MySubcaptionFirst
\ifx\MySubcaptionLast\@empty
\else
--\MySubcaptionLast
\fi
}
\newcommand*\SubCaptionBoxPatch
{%
\ifx\MySubcaptionFirst\@empty
\xdef\MySubcaptionFirst{\csname thesub\@captype\endcsname}%
\else
\xdef\MySubcaptionLast{\csname thesub\@captype\endcsname}%
\fi
}
\apptocmd \caption@iiibox
{\SubCaptionBoxPatch}
{}
{\GenericError{}{Patching failed}{}{}}
\makeatother
\DeclareCaptionLabelFormat{subcaptions}
{#1 #2\MySubcaptionRange}
\captionsetup{labelformat=subcaptions}
\usepackage{cleveref}
\crefrangelabelformat{figure}%
{\emph{#3#1#4--#5\crefstripprefix{#1}{#2}#6}}
\crefrangelabelformat{subfigure}%
{\emph{#3#1#4--#5\crefstripprefix{#1}{#2}#6}}
\begin{document}
\begin{figure}
\subcaptionbox{\label{fig:img1}}
{\includegraphics[width=0.4\columnwidth]{example-image-duck}}
\subcaptionbox{\label{fig:img2}}
{\includegraphics[width=0.4\columnwidth]{example-image-duck}}
\subcaptionbox{\label{fig:img3}}
{\includegraphics[width=0.4\columnwidth]{example-image-duck}}
\subcaptionbox{\label{fig:img4}}
{\includegraphics[width=0.4\columnwidth]{example-image-duck}}\linebreak
\begin{captionbeside}{Caption}
\subcaptionbox{\label{fig:img5}}
{\includegraphics[width=0.4\columnwidth]{example-image-duck}}
\end{captionbeside}
\end{figure}
\crefrange{fig:img1}{fig:img4}
\begin{figure}
\centering
\includegraphics[width=.8\columnwidth]{example-image-duck}
\caption{Big duck\label{fig:big}}
\end{figure}
\end{document}