LaTeX/Labels and Cross-referencing

Introduction
In LaTeX, you can easily reference almost anything that can be numbered, and have LaTeX automatically updating the numbering for you whenever necessary. The objects which can be referenced include chapters, sections, subsections, footnotes, theorems, equations, figures and tables. The commands to be used do not depend on what you are referencing, and they are:
 * Used to give the object you want to reference a marker — a name which can be used to refer to that object later.
 * Used to give the object you want to reference a marker — a name which can be used to refer to that object later.


 * Used to reference an object with the specified marker. This will print the number that was assigned to the object.
 * Used to reference an object with the specified marker. This will print the number that was assigned to the object.


 * Used to print the page number where the object with the specified marker is found.
 * Used to print the page number where the object with the specified marker is found.

LaTeX will calculate the right numbering for the objects in the document; the marker you have used to label the object will not be shown anywhere in the document. Instead, LaTeX will replace the string " " with the right number that was assigned to the object. If you reference a marker that does not exist, the compilation of the document will be successful but LaTeX will return a warning: LaTeX Warning: There were undefined references. and it will replace " " with "??" — so that it will be easier to find in the document.

As you may have noticed, this way of cross-referencing is a two-step process: first the compiler has to store the labels with the right number to be used for referencing, then it has to replace the  with the right number.

Because of that, you would have to compile your document twice to see the output with the proper numbering. If you only compile it once, then LaTeX will use the older information collected in previous compilations (which might be outdated), and the compiler will inform you by printing the following message at the end of the compilation:


 * LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.

Using the command  you can help the reader to find the referenced object by providing also the page number where it can be found. You could write something like:

Since you can use exactly the same commands to reference almost anything, you might get a bit confused after you have introduced a lot of references. It is common practice among LaTeX users to add a few letters to the label to describe what you are referencing. Some packages, such as, rely on this meta information. Here is an example:

Following this convention, the label of a figure will look like, etc. You are not obligated to use these prefixes, and can in fact use any string as an argument of  , but these prefixes can become increasingly useful as your document grows in size.

Another suggestion: try to avoid using numbers within labels. You are better off describing what the object is about. This way, if you change the order of the objects, you will not have to rename all your labels and their references.

If you want to be able to see the markers you are using in the output document as well, you can use the  package; this can become very useful as you develop your document. For more information see the Packages section.

Examples
Here are some practical examples, but you will notice that they are all the same because they all use the same commands.

Sections


You could place the label anywhere in the section; however, in order to avoid confusion, it is better to place it immediately after the beginning of the section. Note how the marker starts with sec:, as suggested before. The label is then referenced in a different section, where the tilde (~) indicates a non-breaking space.

Pictures
You can reference a picture by inserting it in the figure floating environment.

When a label is declared within a float environment, the  will return the respective figure/table number, but it must occur after the caption. When declared outside, it will give the section number. To be completely safe, the label for any picture or table can go within the  command, as follows:

For more, see the Floats, Figures and Captions section about the  and related environments.

Fixing wrong labels
The command  must appear after (or inside). Otherwise, it will pick up the current section or the list number instead of what is intended.

Issues with links to tables and figures handled by hyperref
In case you use the package  to create a PDF, the link to a table or a figure will point to its caption instead, which is always below the table or the figure itself. As a result, the table or the figure will not be visible if it is above the pointer, which means that some scrolling-up would be required. If you want the link to point to the top of the image, you can give the option  to the   package :

Formulae
Here is an example showing how to reference formulae:

Here, notice the eq: prefix in the label — and that the label is placed soon after the beginning of the math mode. To reference a formula, an environment with counter would have to be used. Most of the times, you will be using the  environment, as that's usually the best choice for one-line formulae whether you are using   or not.

eqref
The  package adds a new command for referencing formulae; it is. It works exactly like, but adds parentheses so that instead of printing a plain number as 5, it will print (5). This can be useful to help the reader distinguish between formulae and other things, without the need to repeat the word "formula" before any reference. Its output can be changed as desired; for more information see the  documentation.

tag
The  command is used to manually set equation numbers, where eqnno is the text string you want to display instead of the usual equation number. It is normally better to use labels, but sometimes hard-coded equation numbers might offer a useful work-around — such as the case where you want to repeat an equation that has already been used before (e.g. ).

numberwithin
The  package adds the   command, which replaces the simple   with a more sophisticated . For example,   in the preamble will prepend the section number to all equation numbers.

cases
The  package adds the   and the   commands, which produce multi-case equations with a separate equation number and a separate equation number plus a letter, respectively, for each case.

The varioref package
The  package introduces a new command called. This command is used exactly like the basic, but it has a different output according to the context. If the object to be referenced is in the same page, it works just like ; if the object is far away, it will print something like "5 on page 25", i.e. it adds the page number automatically. If the object is close, it can use more refined sentences such as "on the next page" or "on the facing page" automatically — according to the context and the document class.

This command has to be used very carefully. It outputs more than one word, so it may happen that its output falls on two different pages. In this case, the algorithm can get confused and cause a loop.

For example, you could label an object on page 23 and the  output could happen to stay between page 23 and 24. If it were on page 23, it would print like the basic, if it were on page 24, it would print "on the previous page", but it is on both, and this may cause some strange errors at compiling time that could be very difficult to fix.

And while for small documents, these situations might happen very rarely, for long documents spanning hundreds of references, these situations are more likely to happen. One way to avoid these problems during document preparation is to use the standard  all the way through at first, convert all to   when the document is close to its final version — before making the adjustment to fix any possible problem.

autoref
The  package introduces another useful command;. This command creates a reference with additional text corresponding to the target's type, all of which will be a hyperlink. For example, the command  would create a hyperlink to the   command, wherever it is. Assuming that this label is pointing to a section, the hyperlink would contain the text "section 3.4", or similar (the full list of default names can be found here). Note that, while there's an  command that produces an unlinked prefix (useful if the label is on the same page as the reference), no alternative   command is defined to produce capitalized versions (useful, for instance, when starting sentences); but since the capitalization of autoref names was chosen by the package author, you can customize the prefixed text by redefining   to the prefix you want, as in: This renaming trick can, of course, be used for other purposes as well.


 * If you would like to have a hyperlink reference without the predefined text  provides, then you change this with a command such as  . Note that you can disable the creation of hyperlinks in , and just use these commands for automatic text.


 * Keep in mind that the \label must be placed inside an environment with a counter, such as a table or a figure. Otherwise, not only will the number refer to the current section (as mentioned above), but the name might refer to the previous environment with a counter as well. For example, if you put a label after closing a figure, the label will still say "figure n", on which n is the current section number.

<tt>nameref</tt>
The  package also automatically includes the   package, and a similarly named command. It is similar to, but inserts text corresponding to the section name, for example.

Input:

Output:

In section MyFirstSection we defined...

Anchor manual positioning
When you define a  outside a figure, a table, or other floating objects, the label points to the current section. In some cases, this behavior is not what you'd like and you'd prefer the generated link to point to the line where the  is defined. This can be achieved with the command as in this example:

The <tt>cleveref</tt> package
The  package introduces the new command   which includes the type of referenced object like   does. The alternate  command works more like standard. References to pages are handled by the  command.


 * Use   and   commands for start and end label in either order and to provide a natural language (  enabled) range.
 * Use  command for multiple or single references (on single line), it will sort them and group into ranges automatically in very convenient format like ` item 2 to 4 and 6 to 19.`.
 * Use   where the numbers would be a references to the labels and [sorted] output as in previous paragraph

The format can be specified in the preamble.

Interpackage interactions for <tt>varioref</tt>, <tt>hyperref</tt>, and <tt>cleveref</tt>
Because, , and   redefine the same commands, they can produce unexpected results when their   commands appear in the preamble in the wrong order. For example, using, , then   can cause   to fail as though the marker were undefined. The following order generally seems to work: