# How can I get correct labelling of sub-figures with subfig and floatrow?

I am using subfig and floatrow to manage sub-figures. Without floatrow, everything works OK, but I need floatrow to easily align the captions, as the subfig manual explains.

Adapting the example on page 37 of the manual and with some help from Gonzalo Medina, I've got this code:

\documentclass{article}
\usepackage{graphicx,subfig}
\usepackage{fr-subfig}

\begin{document}

\thisfloatsetup{subfloatrowsep=none}
\begin{figure}[!htbp]
\captionsetup[subfigure]{justification=centering}
\floatbox{figure}{%
\caption{%
Components of the system: Penguins (\ref{fig:a}), Jumpers (\ref{fig:b}), Thingies (\ref{fig:c}), Cabbages (\ref{fig:d}) and Nuclear Warheads (\ref{fig:e})%
}\label{fig:x}%
}{%
\begin{subfloatrow}%
\subfloat[Penguins were here and here and here]{%
\label{fig:a}\includegraphics[height=.1\textheight]{example-image-a}}
\subfloat[Penguin]{%
\label{fig:b}\includegraphics[height=.15\textheight]{example-image-b}}
\subfloat[Penguin]{%
\label{fig:c}\includegraphics[height=.125\textheight]{example-image-a}}
\end{subfloatrow}

\begin{subfloatrow}
\subfloat[Penguin]{%
\label{fig:d}\includegraphics[height=.15\textheight]{example-image-b}}
\subfloat[Penguins were here, there and everywhere!]{%
\label{fig:e}\includegraphics[height=.2\textheight]{example-image-a}}
\end{subfloatrow}}
\end{figure}
\ref{fig:x} \ref{fig:a} \ref{fig:c} \ref{fig:e}

\end{document}


But obviously something is wrong with the caption numbering. This should all be figure 1 yet it begins with 4 and then increments by 2 more. I've tried moving the labels to different places but, although LaTeX will tell me references have changed, I still get the same result. I've also tried removing the .aux file etc. But, again, I still get 4 for the figure and 6 for the sub-figures.

What is the correct way to do this?

• No idea how to solve this with subfig and switching to subcaption will imply a massive change in the approach. – Gonzalo Medina Feb 19 '15 at 22:50
• @GonzaloMedina I can't even figure out how to fake it. I can add -3 to the figure counter just before the figure environment and that gets the main figure numbered correctly. But everywhere I try to adjust the counter within the figure creates weird results. I can't work out what and where the counter is being stepped! [I'm prepared to switch packages after this project, but I really don't want to try doing so in the middle. And page/line breaks prior to the bit I'm currently working on must not change else the new bit won't tally with the old (already distributed) bit...] – cfr Feb 19 '15 at 23:01
• I think I found a solution (and I have an idea (a vague idea, really) of what was producing the undesired result). – Gonzalo Medina Feb 19 '15 at 23:20

This gives the correct output:

\documentclass{article}
\usepackage{graphicx,subfig}
\usepackage{fr-subfig}

\begin{document}

\begin{figure}[!htbp]
\captionsetup[subfigure]{justification=centering}
\ffigbox{%
\begin{subfloatrow}[3]%
\ffigbox[\FBwidth]{\caption{Penguins were here and here and here}\label{fig:a}}{%
\includegraphics[height=.1\textheight]{example-image-a}}
\ffigbox[\FBwidth]{\caption{Penguin}\label{fig:b}}{%
\includegraphics[height=.15\textheight]{example-image-b}}
\ffigbox[\FBwidth]{\caption{Penguin}\label{fig:c}}{%
\includegraphics[height=.125\textheight]{example-image-a}}
\end{subfloatrow}

\begin{subfloatrow}
\ffigbox[\FBwidth]{\caption{Penguin}\label{fig:d}}{%
\includegraphics[height=.15\textheight]{example-image-b}}
\ffigbox[\FBwidth]{\caption{Penguins were here, there and everywhere!}\label{fig:e}}{%
\includegraphics[height=.2\textheight]{example-image-a}}
\end{subfloatrow}%
}{%
\caption{Components of the system: Penguins (\ref{fig:a}), Jumpers (\ref{fig:b}), Thingies (\ref{fig:c}), Cabbages (\ref{fig:d}) and Nuclear Warheads (\ref{fig:e})\label{fig:x}}%
}
\end{figure}
\ref{fig:x} \ref{fig:a} \ref{fig:c} \ref{fig:e}

\end{document}


The problem seems to be combining floatrow commands with subfig's \subfloat (I guess that using commands from both packages makes the counters step in a wrong way (when each package takes control, it steps the counters)); once everything is done using floatrow commands only, the problem disappears; notice that this will also make

  \thisfloatsetup{subfloatrowsep=qquad}


behave as expected, so no manual adjustments are needed now (I've added a remark about this also in this other question).

• Weirdly, I was just about to edit my question to say that I'd found a solution for the labelling problem but that this solution broke the horizontal spacing again. However, setting subfloatrowsep=qquad solves the problem even without changing \floatbox{figure} to ffigbox. Thanks very much for looking into (& solving this)! – cfr Feb 20 '15 at 0:02