# Subfig reference index off, but only when caption is above figure

I want to reference a subfigure, but I found the index referencing is one off if the caption is above the big figure. MWE:

\documentclass[12pt,a4paper]{scrartcl}

\usepackage{subfig}
\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}

\begin{figure}%
\centering
\caption{Example of powerset transformation}%
\label{fig:powerset_ex}
\subfloat[NFA] {
\label{subfig:powerset_nfa}
Some NFA%
}
~ $\rightarrow$ ~
\subfloat[DFA] {
\label{subfig:powerset_dfa}
Some DFA%
}
\end{figure}

In \Cref{fig:powerset_ex} we turn the NFA \Cref{subfig:powerset_nfa} into the DFA \Cref{subfig:powerset_dfa}

\end{document}


This results in the output:

In Figure 1 we turn the NFA Figure 2a into the DFA 2b.

when it should read 1a and 1b, respectively. I found that the labels are referenced correctly if I move the caption and label of the whole figure below both subfigures. But I want the figure caption above. How can I achieve this?

• Please make your code compilable. Where does the align=c come from? I only know of the valign=c option that is available when using \usepackage[export]{adjustbox}. – leandriis Mar 30 at 13:12
• You could consider using subcaption instead of subfig, see tex.stackexchange.com/a/64618 for a related discussion. – Marijn Mar 30 at 13:16
• Completely unrelated to the alignment issue but in your text you write "we turn the NFA \Cref{subfig:powerset_nfa} into the DFA \Cref{subfig:powerset_dfa}" while the captions under the subfigures seem to be the other way around. – leandriis Mar 30 at 13:19
• Got rid of align by removing images since they're not relevant anyway – PattuX Mar 30 at 13:32
• @Marijn I tried, but as the top answer also confirms this also happens with subcaption. – PattuX Mar 30 at 16:34

At first I thought this was peculiar to figures, but it also affects tables. I guess it is a subfig bug which was preserved in subcaption.

One solution is to play with the figure counter, but I prefer to use saveboxes.

BTW, there is no align option for \includegraphics. Instead one can use \raisebox, but subfig and subcaption require different solutions.

\documentclass[12pt,a4paper]{scrartcl}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}

\begin{figure}%
\sbox0{\subfloat[DFA\label{subfig:powerset_nfa}] {%
\includegraphics[width=0.4\textwidth]{example-image-a}%
}}%
\sbox1{\subfloat[NFA] {% works either way
\label{subfig:powerset_dfa}
\includegraphics[width=0.4\textwidth]{example-image-b}%
}}%
\centering
\captionsetup[figure]{position=top}%
\caption{Example of powerset transformation}%
\label{fig:powerset_ex}
\raisebox{-0.5\height}{\usebox0}
~ $\rightarrow$ ~
\raisebox{-0.5\height}{\usebox1}
\end{figure}

In \Cref{fig:powerset_ex} we turn the NFA \Cref{subfig:powerset_nfa} into the DFA \Cref{subfig:powerset_dfa}

\end{document}


• Thanks, works perferctly. Btw I used align via the adjustbox package, just deleted it in the preamble when extracting the MWE. Result is the same as with raisebox then. – PattuX Mar 30 at 16:33