2

I want to reference a subfigure like so:

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

\begin{document}

\begin{figure}
     \subfloat[A subcaption.]{\label{figure:subfigure1}\includegraphics[width=2cm]{logo}}
     \caption{A caption.}
     \label{figure:figure1}
\end{figure}

\noindent
Subfigure \ref{figure:subfigure1}, Figure \ref{figure:figure1}.

\end{document}

However, I am getting a "Reference undefined" warning and the output is:

enter image description here

When I am using

\usepackage{subfig} 

instead of

\usepackage{caption}
\usepackage{subcaption}

the output is as expected (references are all found).

What am I doing wrong?

Update

I checked the package versions. Indeed, they are old:

Package: subcaption 2008/08/31 v1.0b 
Package: caption 2009/10/09 v3.1k caption kernel (AR)
7
  • Welcome to TeX.SE! Your first MWE doesn't compile for me; \subfloat is not from the subcaption package. Instead, use \begin{subfigure}{<width>}<Figure code>\caption{<caption>}\label{<label>}\end{subfigure} and all will be good :)
    – cmhughes
    Commented Jan 2, 2013 at 18:53
  • silly question, why not use subfig then if it works? I don't know subcaption, so the syntax may be correct, but by the look of it the syntax you are using is that of subfig.
    – ArTourter
    Commented Jan 2, 2013 at 18:53
  • @cmhughes Thank you! Yes this would work, however I am using subfloats for a reason (to span a figure over multiple pages using continuedfloat).
    – Tobias
    Commented Jan 2, 2013 at 18:57
  • @ArTourter Cannot use subfig since it doesn't play nicely with hyperref.
    – Tobias
    Commented Jan 2, 2013 at 18:57
  • @ArTourter Yes, might be wrong about that, I don't have a MWE. In the example above, hyperref works properly. Still it doesn't in my main document. A major benefit of using subcaption is IMHO that I can use both subfigure and subfloat. It would therefore be nice to solve the issue at hand.
    – Tobias
    Commented Jan 2, 2013 at 19:09

1 Answer 1

2

First of all \subfloat was indeed defined in very early versions of the subcaption package but this was never a documented feature (and has proven to be a very bad idea anyway since it was not 100% compatible with \subfloat offered by the subfig package). As equivalent the subcaption package offers the (well documented) command \subcaptionbox with a slightly different (and enhanced) syntax and behaviour:

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

\begin{document}

\begin{figure}
     \subcaptionbox{A subcaption.\label{figure:subfigure1}}{\includegraphics[width=2cm]{logo}}
     \caption{A caption.}
     \label{figure:figure1}
\end{figure}

\noindent
Subfigure \ref{figure:subfigure1}, Figure \ref{figure:figure1}.

\end{document}

(Please note when using \subcaptionbox the \label needs to be placed inside the caption argument, not inside the content argument.)

2
  • This seems to work. Didn't know about \subcaptionbox, thank you!
    – Tobias
    Commented Jan 2, 2013 at 19:43
  • BTW: \subcaptionbox has some additional optional parameters, better take a (short) look at the subcaption package documentation.
    – user2574
    Commented Jan 2, 2013 at 20:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .