5 Cross-References

5.1 Cross-References

One of the most useful features of LaTeX is its ability to handle cross-references. To use this, we first define a label at the section (or equation, table, etc.) we want to reference, using the \label{} command, e.g.
\section{Labels and References}\label{ref_section}

The argument to the \label command is just a text string that you'll use to reference that part. We add a reference to it using the \ref{} command, which just takes that same text string as an argument, e.g.
We can define labels in LaTeX, as we see in section \ref{ref_section}, and use these for cross-referencing.

You can put references to sections before or after the label command, so it's quite possible to have text such as "as you will see in chapter \ref{my_chapter}" which references a part you haven't got to yet.

You can reference the page on which an object appears using \pageref{}, e.g.
We can define page references easily in LaTeX, see the reference on page \pageref{ref_section}.

When you run LaTeX on a document with cross-references, it often doesn't know which section the references actually refer to. You will see a LaTeX message such as:
LaTeX Warning: There were undefined references.
LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.

The first time you run LaTeX it goes through making a note of all the labels and all the references and puts this information into the aux file, and then you need to run LaTeX again, without changing the source tex file, in order for LaTeX to get the information from the aux file and sort out which references are which.

If you keep getting the error message LaTeX Warning: There were undefined references then it means LaTeX can't find a label that matches one of your reference commands. This is usually because you've misspelled it!

If you put the same text string into two different labels, then of course LaTeX wouldn't know which one you meant when you referenced it. To prevent this, if LaTeX finds two identical label strings it will complain:
LaTeX Warning: There were multiply-defined labels.
and if you look further up in the output (or in the log file) you will see something like:
LaTeX Warning: Label `important_equation' multiply defined.

Exercise 5

Label some of your sections and subsections, and reference them from other parts of the document.

Try making some deliberate mistakes, e.g. misspell a label or use the same label name twice, and see what error messages LaTeX produces.


Previous page Next page