# \ref not working with tikz and externalize lib [duplicate]

Possible Duplicate:
Why does an environment's label have to appear after the caption?

I am using tikz with its externalize library. I create figures with the following coding style:

\begin{figure}
\centering
\begin{tikzpicture}
code code code..
\end{tikzpicture}
\label{fig:label}
\caption{Blah blah blah text.}
\end{figure}


when I use \ref{fig:label} it does not display the correct number. Another notable and bizarre behavior is that the number is in the format, e.g 5.2.1 and not 5.2; has anyone encountered this?

Also by trying to create a MWE I could not make the thing to even appear. I am not trying to do complex stuff, just to say "in the figure x.x ...". Anyway, here is the not working MWE:

\documentclass[a4paper,10pt]{report}
\usepackage{tikz} %pgf-tikz pakcage
\usepackage{pgfplots}
\usepackage{hyperref}

\hypersetup{
}

\begin{document}

\begin{figure}
\begin{tikzpicture}

\draw[fill = blue,fill opacity = 0.3](0,2) rectangle (1.5,3.5);
\draw[fill = yellow,fill opacity = 0.3](0.5,2.5) rectangle (1,3);

\draw[->] (0.75,0.75) -- (0.75,2.75);
\end{tikzpicture}
\label{fig:stuff}
\end{figure}

Hello \ref{fig:stuff} world
\end{document}

-

## marked as duplicate by lockstep, Torbjørn T., Gonzalo Medina, cmhughes, Stephan LehmkeAug 31 '12 at 21:44

Put \label after \caption. – lockstep Aug 31 '12 at 21:20