\caption* command causes wrong reference in List of Figures for subfigures

captionssubcaptiontable of contents

I have created a custom \source command which places a second caption under a figure using the \caption* command (see Xavi's answer on this post). Also I have enabled the list option of the subcaption package to include subfigures in the List of Figures.

However, when using the \source command for a figures containing subfigures the caption of the subfigures are wrongly placed in the List of Figures, see the following example.

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

The example produces the following output.

Output of the MWE

The two subfigures in the second figure are incorrectly listed under the first figure. However, the labels still correctly refer two 2a and 2b. Therefore I don't understand what causes the problem.

Can somebody explain this behaivour and tell me how to fix it? Or should I not be using \caption* command to create sources?

Note: I have also included the Koma-Script command \setcapindent{1em}, but I don't think that causes the problem.

Also, since this is my first question and I am fairly new to the Latex world I would be gratefull for any advice regarding asking questions on tex.stackexchange.com

Best Answer

This happens due to your defined \source command. If you place the source command below the \caption in the figure environment, then you will get expected output. However, if you want to put the source ahead of the caption, then you can use the \addcontentsline command (see section 2.3 of the LaTeX Companion 2nd edition for more info).

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

enter image description here

Related Question