Why does an environment's label have to appear after the caption?
This is in common with all captions: it's the \caption
command that generates the number, so a \label
before it can't know that number.
This could have been designed differently, of course, but you can think to a figure
environment where two figures with distinct captions are placed, side by side in order to save space, for example.
So, always place the \label
after the \caption
.
Since \label
is a "general purpose" command, it will always refer to the last previously generated number (be it a chapter, section, figure caption number or directly from the user with a \refstepcounter
command) and to the page where it eventually falls. Actually it's not really the last generated number, but the one lastly generated at the same or higher grouping level.
The \caption
macro increases the used counter and sets the current label text which is used by \label
. If you use \label
before it the old label text is used instead, which leads to a wrong number. Always use \label
after \caption
and not before or in it.