2

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

1 Answer 1

1

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

4
  • Thanks, this works as expected. However, I find this approach a bit cumbersome. Of course, one could define a command \sourcecaption{source}{caption}{label} but I would like to maintain the flexibility of the \caption and \label commands. This leads me to the conclusion, that using \caption* for creating a source is not the right approach. How can I redefine the \source command to work as expected?
    – marv
    Commented Dec 15, 2021 at 12:16
  • As an extra note, is this behaviour of the \caption* command even correct? According to the captions manual, the \caption* command typesets the caption without label and without entry in the list of figures. Is this a bug?
    – marv
    Commented Dec 15, 2021 at 12:28
  • @MarvinStrebel Yes, it's cumbersome for something seemingly straightforward. In fact, I would insert the source as simple text above the caption and to get consistent spacing I might use \vspace.
    – Imran
    Commented Dec 15, 2021 at 13:37
  • @MarvinStrebel I think \caption* is working correctly. It didn't add any content in the LOF rather due to the use of both \caption and \caption* command, things were messed up. Someone experienced enough might explain the behavior better (I am a beginner).
    – Imran
    Commented Dec 15, 2021 at 13:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .