Typevia
References and Citations

Introduction to Cross-Referencing

One of the most powerful features of LaTeX is its ability to handle cross-referencing automatically. In a traditional word processor, if you add a new chapter at the beginning of your book, you have to manually update every sentence that says "As seen in Chapter 3" to "As seen in Chapter 4."

In LaTeX, you never hard-code numbers. Instead, you give a specific point in your document a "name" (a label) and then tell LaTeX to "refer" to that name. LaTeX tracks the numbering for you, ensuring that even if you move sections around or add new figures, your references always point to the correct location.

The Basic Commands: \label and \ref

To create a cross-reference, you need two parts: the marker and the call-out.

  1. \label{key}: This marks the spot you want to refer to. The "key" is a unique identifier you choose.
  2. \ref{key}: This prints the number of the section, figure, or table associated with that label.
  3. \pageref{key}: This prints the page number where the label is located.

Basic Example: Referencing a Section

\section{Introduction}
\label{sec:intro}

In this section, we discuss the basics of LaTeX. 

\section{Advanced Methods}
As we mentioned in Section \ref{sec:intro} on page \pageref{sec:intro}, LaTeX is powerful.

Note: When you first add a reference or move a label, you may see ?? in your compiled PDF. This is normal! LaTeX needs to compile the document twice: once to find the labels and a second time to fill in the correct numbers.


Referencing Figures, Tables, and Equations

While referencing sections is straightforward, referencing floats (like figures and tables) and math equations requires specific placement of the \label command.

1. Figures and Tables

For figures and tables, the \label command must be placed either inside or immediately after the \caption command. This is because the caption generates the number that the label "grabs."

\begin{figure}[h]
    \centering
    \include{my_image.png}
    \caption{A diagram of the solar system.}
    \label{fig:solar_system}
\end{figure}

Refer to Figure \ref{fig:solar_system} for more details.

2. Equations

To reference equations, use the equation environment. It is common practice to use the amsmath package, which provides the \eqref{key} command. Unlike \ref, \eqref automatically puts parentheses around the number (e.g., "(1)" instead of "1").

\usepackage{amsmath} % Required for \eqref

\begin{equation}
    E = mc^2
    \label{eq:einstein}
\end{equation}

Einstein's famous equation is shown in \eqref{eq:einstein}.

Best Practice: Using Naming Conventions

As your document grows to 50 or 100 pages, you might have dozens of labels. If you name a section label \label{intro} and a figure label \label{intro}, LaTeX will get confused and throw a warning.

Professional LaTeX users use prefixes to keep labels organized. This makes it easier to remember what you are referencing and prevents duplicate names.

TypePrefixExample
Sectionsec:\label{sec:results}
Figurefig:\label{fig:chart}
Tabletab:\label{tab:data}
Equationeq:\label{eq:gravity}
Chapterch:\label{ch:conclusion}

Enhancing References with hyperref and cleveref

The standard LaTeX referencing system is functional but can be improved with packages. Two of the most essential packages for references are hyperref and cleveref.

1. Making References Clickable with hyperref

The hyperref package turns your references into clickable blue links in the PDF.

\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,      
    urlcolor=cyan,
}

2. Smart Referencing with cleveref

Normally, you have to type the word "Figure" or "Section" before every \ref. The cleveref package automates this. It detects what you are referencing and adds the appropriate word for you.

Important: Always load cleveref after hyperref.

\usepackage{hyperref}
\usepackage{cleveref}

% Inside the document:
See \cref{fig:solar_system} and \cref{sec:intro}. 
% This will render as: "See fig. 1 and section 1."

One major advantage of cleveref is that it handles multiple references gracefully: \cref{fig:1,fig:2,fig:3} will automatically render as "figs. 1 to 3."


Common Pitfalls and Troubleshooting

1. The "Label Before Caption" Trap

If you put \label before \caption in a figure, the reference might point to the Section number instead of the Figure number.

  • Wrong: \label{fig:1} \caption{My figure}
  • Right: \caption{My figure} \label{fig:1}

2. Duplicate Labels

If you copy and paste a figure block, you might forget to change the label. LaTeX will give you a warning: LaTeX Warning: There were multiply-defined labels. Check your log file to find the duplicates.

3. Missing References (The "??" Problem)

If you see question marks in your document:

  1. Check that you spelled the key exactly the same in both \label and \ref (it is case-sensitive).
  2. Re-compile. Remember:
    • Run 1: LaTeX records labels in an auxiliary file (.aux).
    • Run 2: LaTeX reads the .aux file and replaces \ref with the numbers.

Summary Checklist

  • Use \label{key} to mark a location.
  • Use \ref{key} to display the number.
  • Use \pageref{key} for the page number.
  • Always place the figure label after the caption.
  • Use prefixes like fig: and sec: to stay organized.
  • Compile your document twice to resolve references.