## Referencing in LaTeX

Posted: 12th December 2009 by Tim in LaTeX
Tags: , , , ,

One of the nice things about LaTeX is that it sorts out reference numbers for you. This is done through the use of the \label, \ref and \pageref commands. This allows us to add, remove or re-arrange elements without worrying about where they are referenced in the rest of the document

The \label is a label given to a chapter, section, figure, etc, used later to refer to that element. Labels can be any textual label which has not been used elsewhere, but it is good practice to prepend chapters with cha:. figures with fig: and so on. \ref{label} provides the number of the element which is being referenced, and \pageref{label} gives the page that the element is on.

For example, we may have the following document, and we want to reference the Proof chapter from the Introduction chapter. At this stage we don’t know which chapter number the Proof chapter will be, nor which page it will start on. Note the lines in red:

\begin{document} ... \chapter{Introduction} This paper discusses the performance benefits of coding in C++ over Perl. Chapter \ref{cha:proof} on page \pageref{cha:proof} shows this more formally. ... \chapter{Proof} \label{cha:proof} This chapter formally proves that a compiled language, such as C++ will perform better than an interpreted language, such as Perl. ... \end{document}

The introduction will be printed as so:

## Introduction

This paper discusses the performance benefits of coding in C++ over Perl. Chapter 4 on page 273 shows this more formally.

Note that you will need to compile the document twice to get the correct page references (ie: run latex or pdflatex twice).