[Tex/LaTex] subfigures break figure numbering with sidecaptions from sidenotes package

captionssidenotessubcaption

I am setting my captions into the margin with the sidenotes package. I also use the subfigure environment from the subcaptions quite a lot to annotate subplots and refer to them.

I now noticed that this combination breaks the numbering. MWE:

\documentclass[a4paper,11pt]{scrbook}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{hyperref}

\usepackage{geometry}
\geometry{a4paper, top=10mm, left=33mm, right=46mm, bottom=46mm, headsep=\baselineskip, 
          marginparwidth=43mm, marginparsep=1em}

% ********************************************************************
% captions, taking use of the margins
% ********************************************************************
\usepackage{ragged2e}
\usepackage{sidenotes}
\DeclareCaptionStyle{stdstyle}{
    format=hang,                 % caption hangs behind label
    justification=justified,     % caption is a normal paragraph
    singlelinecheck=false,       % always left-align caption
    labelsep=quad,               % quad separator behind label
    labelfont=sc,                % label in scshape
    font=footnotesize            % all in small font size
}
\DeclareCaptionJustification{outerragged}{\ifthispageodd{\RaggedRight}{\RaggedLeft}}
\DeclareCaptionStyle{sidestyle}{
    format=plain, %indention=2em, % whole line for caption, with indent after first
    justification=outerragged,    % caption is a normal paragraph
    singlelinecheck=false,        % always left-align caption
    labelsep=newline,             % quad separator behind label
    labelfont=sc,                 % label in scshape
    font=footnotesize             % all in small font size
}
\DeclareCaptionStyle{marginfigure}{
    style = sidestyle,
    labelsep=quad
}
\DeclareCaptionStyle{widetable}{style = stdstyle}
\DeclareCaptionStyle{widefigure}{style = stdstyle}
\DeclareCaptionStyle{sidecaption}{
    style = sidestyle,
    format=plain,
    labelsep=newline
}
\usepackage[style = stdstyle]{caption}
\usepackage[
    format=hang,
    justification=raggedright,
    labelfont=sc
]{subcaption}

\begin{document}
\chapter{for the numbers}
\begin{figure}
    \sidecaption{caption of figure 1
    \label{fig:1}
    }
    \includegraphics[width=.6\textwidth]{example-image-a}
\end{figure}
some text and we talk about \autoref{fig:1}

\begin{figure}
    \sidecaption{caption of figure 2. this one has two images but nor subfigures
    \label{fig:2}
    }
    \includegraphics[width=.5\textwidth]{example-image-a}%
    \includegraphics[width=.5\textwidth]{example-image-a}
\end{figure}
some text and we talk about \autoref{fig:2}

\begin{figure}
    \sidecaption{caption of figure 3. this one has two images and uses subfigures with captions to address the subplots
    \label{fig:3}
    }
    \begin{subfigure}{0.5\textwidth}
        \includegraphics[width=\columnwidth]{example-image-a}%
        \caption{some text for 3a}
        \label{fig:3:a}
    \end{subfigure}%
    \begin{subfigure}{0.5\textwidth}
        \includegraphics[width=\columnwidth]{example-image-b}
        \caption{some text for 3b}
        \label{fig:3:b}
    \end{subfigure}
\end{figure}
some text and we talk about \autoref{fig:3} and then there is something important about \autoref{fig:3:a} and \autoref{fig:3:b} but why does this latter one increase the figure counter?

\begin{figure}
    \sidecaption{caption of figure 4
    \label{fig:4}
    }
    \includegraphics[width=.6\textwidth]{example-image-a}
\end{figure}
some text and we talk about \autoref{fig:4} but hey it's Figure 1.5 now...

\end{document}

I get properly numbered figures 1.1 and 1.2 which do both not use the subfigure environment. Figure 1.3 is correctly numbered but referring to the subfigures increases the counter to 1.4a and 1.4b instead of 1.3a.
Furthermore, the next figure is numbered 1.5 skipping 1.4 entirely.

Apparently the sufigure seems to open a new figure or at least to increase the figure counter. The observed behavior is independent of referencing the subfigures or not. The next figure is always numbered incorrectly.

Does someone have an idea what is actually happening? And, most importantly, how to fix it?

To directly adress some potential questions and to give you an idea what alternative approaches should provide in functionality, here some reasons why I do it this way:
I am putting the caption into the margin as there is (i) space and (ii) I also set the legend for the entire set of subplots there, this is much more convenient than putting it in one of the plots and I have more freedom in formatting than in a caption below the figure.
I am using the subcaption package because it works best and the syntax is easier and shorter than for the KOMA-sidecaption. Especially the caption may be longer than the figure itself is high and the caption text then runs next to the main text. The KOMA approach does not allow this (or I did not get it to work).

If someone has a different approach allowing the above features that would also be very welcome to me.

Best Answer

\sidecaption{...} increment caption number, consequently captions in subfigure "see" incremented value of the counter caption and not the value, which use \sidecaption. This means, that the \sidecaption{...} had to be after subfigure environments. With this change your MWE gives expected result:

enter image description here

Edit: In this change you need consider, that \sidecaption{...} is defined in package sidenotes and unfortunately hasn't option for choose its position regarding to float content. It only enable manually shifting of caption position. This can be exploited as shown in above figure by \sidecaption[][-6em]{...}:

\documentclass[a4paper,11pt]{scrbook}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{hyperref}

\usepackage{geometry}
\geometry{a4paper, top=10mm, left=33mm, right=46mm, bottom=46mm, headsep=\baselineskip,
          marginparwidth=43mm, marginparsep=1em}
% ********************************************************************
% captions, taking use of the margins
% ********************************************************************
\usepackage{ragged2e}
\usepackage{sidenotes}
\DeclareCaptionStyle{stdstyle}{
    format=hang,                 % caption hangs behind label
    justification=justified,     % caption is a normal paragraph
    singlelinecheck=false,       % always left-align caption
    labelsep=quad,               % quad separator behind label
    labelfont=sc,                % label in scshape
    font=footnotesize            % all in small font size
}
\DeclareCaptionJustification{outerragged}{\ifthispageodd{\RaggedRight}{\RaggedLeft}}
\DeclareCaptionStyle{sidestyle}{
    format=plain, %indention=2em, % whole line for caption, with indent after first
    justification=outerragged,    % caption is a normal paragraph
    singlelinecheck=false,        % always left-align caption
    labelsep=newline,             % quad separator behind label
    labelfont=sc,                 % label in scshape
    font=footnotesize             % all in small font size
}
\DeclareCaptionStyle{marginfigure}{
    style = sidestyle,
    labelsep=quad
}
\DeclareCaptionStyle{widetable}{style = stdstyle}
\DeclareCaptionStyle{widefigure}{style = stdstyle}
\DeclareCaptionStyle{sidecaption}{
    style = sidestyle,
    format=plain,
    labelsep=newline
}
\usepackage[style = stdstyle]{caption}
\usepackage[
    format=hang,
    justification=raggedright,
    labelfont=sc
]{subcaption}

\begin{document}
\chapter{for the numbers}
\begin{figure}[ht]
    \sidecaption{caption of figure 1
    \label{fig:1}
    }
    \includegraphics[width=.6\textwidth]{example-image-a}
\end{figure}
some text and we talk about \autoref{fig:1}

\begin{figure}[ht]
    \sidecaption{caption of figure 2. this one has two images but nor subfigures
    \label{fig:2}
    }
    \includegraphics[width=.45\textwidth]{example-image-a}\hfill%
    \includegraphics[width=.45\textwidth]{example-image-a}
\end{figure}
some text and we talk about \autoref{fig:2}

\begin{figure}[ht]
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\columnwidth]{example-image-a}%
        \caption{some text for 3a}
        \label{fig:3:a}
    \end{subfigure}\hfill%
    \begin{subfigure}{0.45\textwidth}
        \includegraphics[width=\columnwidth]{example-image-b}
        \caption{some text for 3b}
        \label{fig:3:b}
    \end{subfigure}
    \sidecaption[][-6em]% <-- addedoptions for manual adjustment of caption position
                        {caption of figure 3. this one has two images and uses subfigures with captions to address the subplots
    \label{fig:3}
    }
\end{figure}
some text and we talk about \autoref{fig:3} and then there is something important about \autoref{fig:3:a} and \autoref{fig:3:b} but why does this latter one increase the figure counter?

\begin{figure}[ht]
    \sidecaption{caption of figure 4
    \label{fig:4}
    }
    \includegraphics[width=.6\textwidth]{example-image-a}
\end{figure}
some text and we talk about \autoref{fig:4} but hey it's Figure 1.5 now...
\end{document}