# subfigures break figure numbering with sidecaptions from sidenotes package

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

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

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

• That is a good point and solves the problem. However, then, the caption is aligned with the bottom of the figure not its top. Is there a quick solution for that, too? – Martin Mar 11 '17 at 20:19
• @Martin, I'm not familiar with scrbook, so in this I can't help you. – Zarko Mar 11 '17 at 21:13
• I also noticed that the sidecaption text starts below the float if the images take up all the space in the line (as in my case). That is good to know... – Martin Mar 11 '17 at 21:26
• @Martin, since \sidecaption is defined by sidenotes package, you can exploit its possibilities for manual adjustment of caption position. See edit of my answer. – Zarko Mar 11 '17 at 22:43
• That is true, but than I would have to know the height of my figure to not adjust every caption's positions manually... I will post an new quenstion on this. – Martin Mar 13 '17 at 21:19

As explained by Zarko, the subfigure environment increases the figure counter. A suggestion for an ugly hack would be to decrease the counter manually every time you call subfigures by adding a \addtocounter{figure}{-1} to your code. This has to be done within the figure environment just before you call the first subfigure to work for both figure numbering and referencing.

That way, your sidecaption will still be top-aligned with the figure without shifting it up manually for every single figure. I'm not sure though if messing with the counters will create any other nasty problems...

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