We are currently in the process of putting up the finishing touches to an article, and are running into problems with the way LaTeX manages the references to our figures. For example, we wanted to put a reference to Figure 6 somewhere in the article text. For this we used a \label{<name>} and \ref{<name>} pair as usual, but the figure, while correctly numbered as “Figure 6”, was referred to in the text of the compiled file as to “Figure 3.13”. We thought this was because we’d put the figure inside the text of Example 3.13, so we moved it outside, but then the reference appears to be to “Figure 3”.
This is not the only figure that is incorrectly referred to in the text of our article. We guess it is probably due to some problem with cross-references that is making LaTeX buggy, but we cannot figure out what causes it and how to solve it. We could just manually put the correct number in the article text, but we’d rather correct the problem if possible. So if anybody has run into a similar problem at some point, and has solved it, we would like to hear about it.
The figure’s \label should go after its \caption. If you put the \label at the start of the figure (which seems like a natural place for it) the active cross-reference will instead be the enclosing body section or subsection, as you found.
I’d tried this and it didn’t seem to help, but one of my colleagues sent an email saying he’s been able to get it to work right by changing the label names. I haven’t tried his suggestion yet, but I’ll try it tomorrow and post the results here.
I’m surprised that Omphaloskeptic’s suggestion didn’t work, because I had the same problem until I figured out that LaTeX worked that way.
One tip that’s in the “try turning it off and on again” category is to delete all the auxiliary files that LaTeX generates when it runs. Then regenerate those files by compiling your source enough times to get the cross-referencing right. (I’ve only seen this solve table-of-contents issues, but it might be worth a try.)
And I hope the OP realizes that it takes multiple runs of the latex command to get all the references refreshed? (If you make changes and run LaTeX just once, it uses outdated references from the previous run.)
Of course; that wasn’t the problem. We got bad results on both my advisor’s computer and on mine even when refreshing several times. As I’ve said my co-advisor says he’s been able to make it work so I’ll check (later) and tell you if it worked.
If changing the names helps, maybe you have the same \label in multiple places (this should give a warning when you run latex); or, as others have suggested, maybe the .aux file is not getting updated or cleaned as it should.
You can search the .aux file for your label to see if there are multiple definitions (it’s just a text file). Each \label{xxx} command emits a line
ewlabel{xxx}{{index}{page}} in the .aux, where “index” is the section number (e.g., 1.2.1) for a label in text and the figure or table number for a float label.