# cleveref and marginfigure in memoir

I'm running into a problem when using cleveref to refer to marginfigures in memoir. The following code illustrates the problem (apologies for the somewhat long preamble, but I need the space in the margin to illustrate the problem):

\documentclass[a4paper,10pt]{memoir}
%%% formatting
\setstocksize{29.7cm}{21.0cm} % A4 stock
\settrimmedsize{27.94cm}{21.0cm}{*} % 27.94cm = 11in
\setlength{\trimtop}{0.0cm}
\setlength{\trimedge}{\stockwidth}
\settypeblocksize{23.94cm}{12.3cm}{*}
\setlrmargins{2.0cm}{*}{*}
\setulmargins{2.0cm}{*}{*}
\setmarginnotes{\onelineskip}{5.28cm}{\onelineskip}
\sidecapmargin{outer}
\setsidecaps{\onelineskip}{5.28cm}
\setsidecappos{t}
%%% clever referencing (should be loaded last)
\usepackage{cleveref}
\crefname{equation}{equation}{equations}
\Crefname{equation}{Equation}{Equations}
\crefname{figure}{figure}{figures}
\Crefname{figure}{Figure}{Figures}
%%%
\checkandfixthelayout
%%%
\begin{document}
$$\label{eq:pyth} a^2 + b^2 = c^2$$
\Cref{eq:pyth} is known as Pythagoras' theorem.
\begin{marginfigure}
\rule{1.0cm}{1.0cm}
\label{fig:dummyfig}
\caption{dummyfig1}
\end{marginfigure}
This should be a reference to figure 1, but it comes out as
\cref{fig:dummyfig}.
$$\label{eq:circ} x^2 + y^2 = r^2$$
$$\label{eq:ell} \left(\frac{x}{a}\right)^2 + \left(\frac{y}{b}\right)^2 = 1$$
\Cref{eq:circ} describes a circle, \cref{eq:ell} an ellipse.
\begin{marginfigure}
\rule{2.0cm}{2.0cm}
\label{fig:dummyfig2}
\caption{dummyfig2}
\end{marginfigure}
This should be a reference to figure 2, but it comes out as
\cref{fig:dummyfig2}.
\end{document}


When you run this, the references to the figures turn out as references to the equations. Do I need to tell cleveref about the marginfigure environment in some way (I could not find anything in the newest manual) or is the marginfigure not supported yet by cleveref?

-

The problem is simply that you have the \label before the \caption. Put the \label last of the two, and it works fine. See e.g., Where to put the \label on a figure environment?