Cross-Referencing with LaTeX

Cross-referencing allows you to refer to almost anything in your document automatically through the use of several LaTeX commands. This can become extremely valuable if you have to edit your document and things change. With whatever updates you make the cross-referencing is update automatically.

There are many different ways to cross-reference in LaTeX but we will look at the following.

  • Tables
  • Items in a list
  • Pages

When using cross-referencing you must compile the document twice in order for the numbers to show up. The first time you will only see question marks.

Tables

Below is an example of LaTeX referring to a table.

\documentclass{article}
\begin{document}
   In Table~\ref{Example} we have an example of a table
   \begin{table}[h]
   \begin{center}
   \begin{tabular}{ll}
      \hline
         Fruits&Vegetables\\
      \hline
         Mango&Lettuce\\
         Papaya&Kale\\
      \hline
   \end{tabular}
      \caption{Example} \label{Example}
   \end{center}
   \end{table}
\end{document}

1.png

How a table is created has been discussed previously, what is new here are two pieces of code.

  • ~\ref{  }
  • \label{ }

The “\label” declaration gives the table a label that can be used in the text. In the example, we labeled the table “Example”. The “~\ref” declaration is used in the text and you put the label name on the table inside the curly braces. If you look at the text we never use the number 1 in the text. LaTeX inserts this for us automatically.

The same process can be used to label images as well.

Item in List

Cross-referencing an item on a list is not that complex either. Below is an example.

\documentclass{article}
\begin{document}
   Simple list
   \begin{enumerate}
      \item Mango
      \item Papaya \label{fruit2}
      \item Apple
   \end{enumerate}
   Number \ref{fruit2} is a common fruit in tropical countries.
\end{document}

1.png

As you can see, you can label almost anything anywhere.

Referring to Pages

It is also possible to refer to pages. This can save a lot of time if you update a document and page numbers change. Below is the code and example.

\documentclass{article}
\begin{document}
 Simple list \label{list}
 \begin{enumerate}
    \item Mango
    \item Papaya \label{fruit2}
    \item Apple
 \end{enumerate}
Number \ref{fruit2} in the list on page~\pageref{list} 
is a common fruit in tropical countries.
\end{document}

1.png

We made a label right above our list and then we used the “~\pageref” declaration with the name of the label inside. This provides us with the page number automatically.

Conclusion

There are more complex ways to cross-reference. However, unless you are developing a really complex document they are not really necessary for most practical applications. The ideas presented here will work in most instances as they are.

Leave a Reply