[Tex/LaTex] No figure reference number using a font size in the caption + subfigure

subfloats

A strange behaviour: if I use the package subfigure and I add the caption in a font-changing environment, I have no figure number when I call the figure by reference.

MWE:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subfig}

\begin{document}

\title{Test}

\section{Test}
Bla bla bla, please refer to Figure \ref{fig:flowchart}.

\begin{figure}
    \centering
    \includegraphics[width=0.9\textwidth]{imgs/global_imgs/ffsmFlowChart3.pdf}
    \begin{footnotesize} \caption{FFSM++ Flowchart} \end{footnotesize}
    \label{fig:flowchart}
\end{figure}

\end{document}

Results in:
screenshot

If I remove subfig or I don't add the footnotesize, it works. Should I report it as a bug? Where?

Best Answer

The package caption that's automatically loaded by subfig (unless you pass it the caption=false option) makes further checks to the placement of \label for a caption, in order to warn users making the common error of having the label before the caption.

You have it after, but there's a catch: the \caption command is inside an environment, so the label it sets gets forgotten at \end{footnotesize}. You indeed get the warning

Package caption Warning: \label without proper \caption on input line 20.
See the caption package documentation for explanation.

If you remove subfig, you get no warning, but reference is wrong, as the following example shows:

\documentclass{article}
\usepackage[demo]{graphicx}
%\usepackage{subfig}

\begin{document}

\title{Test}

\section{A}

b

\section{Test}
Bla bla bla, please refer to Figure \ref{fig:flowchart}.

\begin{figure}
    \centering
    \includegraphics[width=0.9\textwidth]{imgs/global_imgs/ffsmFlowChart3.pdf}
    \begin{footnotesize} \caption{FFSM++ Flowchart} \end{footnotesize}
    \label{fig:flowchart}
\end{figure}

\end{document}

enter image description here

When \label is scanned, the most recent \current@label is the one issued by \section{Test}, because the one in the (inexistent) footnotesize environment is forgotten.

Use explicitly caption (it's better) and issue

\captionsetup{font=footnotesize}

One should avoid explicit markup like \footnotesize in the document as much as possible.

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{subfig}

\captionsetup{font=footnotesize}

\begin{document}

\title{Test}

\section{A}

b

\section{Test}
Bla bla bla, please refer to Figure \ref{fig:flowchart}.

\begin{figure}
    \centering
    \includegraphics[width=0.9\textwidth]{imgs/global_imgs/ffsmFlowChart3.pdf}
    \caption{FFSM++ Flowchart}
    \label{fig:flowchart}
\end{figure}

\end{document}

enter image description here