This section will give an overview the labeling capabilities of LaTeX. We will learn to label the float environments we have already covered.
Labels are a necessary part of typesetting as they are efficient pointers to information. It is better to reference Table 2 rather than "that table where I list all of those things." It is exceptionally important for equations.
One of the most useful (and occasionally underrated) properties of LaTeX is the ease and power of its labeling system. This allows one to reference equations, figures, tables, etc, with ease and flexibility. Unlike word processing software, LaTeX will automatically number and reference and change the numbering based on additions and deletions with no extra input from the writer.
There are ways to label many of the examples we have already covered. Try the following code.
\documentclass{article}
\usepackage{float}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{hyperref}
\begin{document}
We can describe atoms through use of the Schr\"{o}dinger
equation which is given as
\begin{equation}
\label{schro}
E\Psi=\textbf{H}\Psi.
\end{equation}
If we carry out equation \ref{schro} to its fullest consequences,
we can describe figure \ref{fig:Atoms}.
\begin{figure}
\centering
\begin{subfigure}{.45\textwidth}
\centering
\includegraphics[width=\textwidth]{atom.png}
\caption{Big atoms}
\label{subfig:big}
\end{subfigure}
%%%%%%%%%%%%%%
\begin{subfigure}{.45\textwidth}
\centering
\includegraphics[scale=.25]{atom.png}
\caption{Small atom}
\label{subfig:small}
\end{subfigure}
\caption{Atoms}
\label{fig:Atoms}
\end{figure}
We also can use this to describe the atomic masses of
elements from big, \ref{subfig:big}, to small, \ref{subfig:small}.
Some of these masses are given in table \ref{tab:atommass}.
\begin{table}[H]
\centering
\begin{tabular}{c|c}
Element&Atomic Mass (amu)\\\hline
H&1.007 825 032 07(10\\
He&3.016 029 3191(26)\\
Li&6.015 122 795(16)\\
Be&9.012 182 2(4)\\
B&10.012 937 0(4)\\
C&12.000 000 0(0)
\end{tabular}
\caption{Atomic Masses}
\label{tab:atommass}
\end{table}
We don't have a \ref{noref}
\end{document}
We can see that we never explicitly label any of the equation, tables, figures, or subfigures. If LaTeX cannot find the proper label, you will see the ?? symbol.
When run is pressed in the environments you are most likely using (all of the ones in the installation section), LaTeX is actually compiling multiple times. There are several reasons for this, but one is due to labeling. The program first goes through the document and finds all the labels and writes them to an auxiliary file. When run again, it can properly write and link to the labels.
Since the TeX program that does the base compilation is old, it was written for computer that had very little RAM. Consequently, LaTeX stores data between runs in output files of text such as the .log and .aux files. It then reads in these files in the next run. Modern computers could easily store all of this data, but this is a case where nobody wants to change and debug something that works so well.
Try adding in more equations and changing their order, you will find that LaTeX will handle all the labeling for you.
Warning! It matters where the \label command is placed relative to the \caption command.
In this example we have also used the hyperref package. It creates a linked page where we can click on the numbers and the the pdf will automatically take us to the location in the document. For a longer document this can be very useful.
Note: the naming scheme I have used as the argument for the \label command is not necessary. However, it is a good habit since it can help keep the labels disparate among different float types and ,perhaps more importantly, organized in your own head. Furthermore, some packages require this sort of naming convention.
Created by Zachary Glassman. Contact at zach.glassman@gmail.com