Sidenotes and label in LaTeX

Recently I have been using the sidenotes package in LaTeX. It provides many options which I find aesthetically pleasing. For example, instead of footnotes, it gives sidenotes. This particular typesetting has been used beautifully by Tufte in his books. But even before Tufte’s books this option has been used, for example, see the beautifully designed book The Evolution of Culture in Animals by John. T. Bonner.
bonner
Not the recent reprint, some idiots at Princeton University Press have completely killed the aesthetically pleasing landscaped typesetting to portrait one (and a not very good one)! In general Bonner’s book are a visual treat in terms of designing and of course the content as well. His meticulous detail to the images, and use of log-scales to depict biological scale in time and space it something that I have not seen very often.
The Evolution of Culture in Animals uses a landscape mode with sidenotes and figures in margins extensively. This is the exact functionality that the sidenotes package provides. Even the tufte-book and tufte-handout classes use this package in a modified manner.
When using sidenotes package, I found that the marginfigure and margintable environments worked without a problem when using a label and referring them to in the document. But when it came to the sidecaption option for wider figures and tables, somehow I could not get the referencing to work. The hyperref showed the correct page and even the captions had the correct numbers for tables and figures. But the label and \ref{} to it didn’t work.
For example, if a figure was created as such:

\begin{figure}
\includegraphics[width=0.8\textwidth]{eagle.jpg}
\sidecaption[][1cm]{This is an eagle.}
\label{eagle1}
\end{figure}

The first [] controls the text that appears in the List of Figures, while the second [] controls the vertical placement of the caption.
Now when I referred to this figure in the document using Figure~\ref{eagle1}, it gave an error and typeset it as Figure ??, instead of Figure 1.1 or something similar. The same problem was there for a sidecaption used for tables as well. A common mistake which usually causes this error is placing the label before the caption. So the golden rule seems to be:

Always place your \label{} after the \caption{}.

But it was not the case in my example. A bit of digging in the log file showed this error:


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

LaTeX Warning: Reference `eagle1′ on page 7 undefined on input line 415.

Now back to caption package documentation, it had this explanation for this error (page 44):

\label without proper \caption
Regarding \label the floating environments behave differently than its non-floating counter-parts: The internal reference will not be generated at the beginning of the environment, but at \caption instead. So you have to place the \label command either just after or inside the caption text (mandatory argument of \caption).

So that was it. You have to place the \label{} inside the \caption{} environment and the issue was solved. Placing it just after the caption did not work for me.
\begin{figure}
\includegraphics[width=0.8\textwidth]{eagle.jpg}
\sidecaption[][1cm]{This is an eagle.\label{eagle1}}
\end{figure}