\caption* 命令导致图片列表中的子图引用错误

\caption* 命令导致图片列表中的子图引用错误

我创建了一个自定义\source命令,使用命令将第二个标题放在图形下方\caption*(请参阅Xavi 对这篇文章的回答)。另外,我还启用了包list的选项subcaption,以将子图包含在图形列表中。

但是,当\source对包含子图的图形使用该命令时,子图的标题会错误地放置在图形列表中,请参见以下示例。

\documentclass{scrartcl}
\usepackage{graphicx}
\usepackage{caption}
\usepackage[list]{subcaption}
\usepackage[colorlinks]{hyperref}

\setcapindent{1em} 
\newcommand{\source}[1]{\caption*{ {\small Source: {#1}}} }

\begin{document}
\listoffigures

\section{Subcaption Test}
\begin{figure}[h]
    \centering
    \includegraphics[width=0.35\linewidth]{example-image}
    \caption{First Figure}
\end{figure}
Some text
\begin{figure}[h]
    \centering
    \begin{subfigure}{0.35\linewidth}
        \centering
        \includegraphics[width=\linewidth]{example-image-a}
        \caption{First Subfigure}
        \label{fig:first}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.35\linewidth}
        \centering
        \includegraphics[width=\linewidth]{example-image-b}
        \caption{Second subfigure}
        \label{fig:second}
    \end{subfigure}
    \source{https://tex.stackexchange.com/}
    \caption{Second figure with two subfigures}
\end{figure}

Subfigures \ref{fig:first} and \ref{fig:second} have the correct reference but appear wrong in the list of figures.

\end{document}

该示例产生以下输出。

MWE 的输出

第二幅图中的两个子图错误地列在了第一幅图下。但是,标签仍然正确地引用了两个 2a 和 2b。因此,我不明白是什么导致了这个问题。

有人能解释一下这种行为并告诉我如何修复它吗?或者我不应该使用\caption*命令来创建源?

注意:我还包含了 Koma-Script 命令\setcapindent{1em},但我不认为这会导致问题。

此外,由于这是我的第一个问题,而且我对 Latex 世界还很陌生,因此对于在 tex.stackexchange.com 上提问的任何建议,我将不胜感激

答案1

这是由于您定义的\source命令而发生的。如果您将源命令放在\caption图形环境中的下方,那么您将获得预期的输出。但是,如果您想将源放在标题前面,那么您可以使用命令\addcontentsline(有关更多信息,请参阅 LaTeX Companion 第 2 版的第 2.3 节)。

\documentclass{scrartcl}
\usepackage{graphicx}
\usepackage{caption}
\usepackage[list]{subcaption}
\usepackage[colorlinks]{hyperref}

\setcapindent{1em} 
\newcommand{\source}[1]{\caption*{ {\small Source: {#1}}} }

\begin{document}
\listoffigures

\section{Subcaption Test}
\begin{figure}[h]
    \centering
    \includegraphics[width=0.35\linewidth]{example-image}
    \caption{First Figure}
\end{figure}
Some text
\begin{figure}[h]
    \centering
    \begin{subfigure}{0.35\linewidth}
        \centering
        \includegraphics[width=\linewidth]{example-image-a}
        \caption{First Subfigure}
        \label{fig:first}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.35\linewidth}
        \centering
        \includegraphics[width=\linewidth]{example-image-b}
        \caption{Second subfigure}
        \label{fig:second}
    \end{subfigure}
    %-----------------------------added---------------------------
    \addcontentsline{lof}{figure}{%
        \protect\numberline {\getrefnumber{fig:sec}}Second figure with two subfigures} 
    %--------------------------------------------------------------
    \source{https://tex.stackexchange.com/}
    \renewcommand{\addcontentsline}[3]{} % <-------- added
    \caption{Second figure with two subfigures}
    \label{fig:sec}
\end{figure}

Subfigures \ref{fig:first} and \ref{fig:second} have the correct reference but appear wrong in the list of figures.

\end{document}

在此处输入图片描述

相关内容