LaTeX 交互參照:\label 與 \ref
在你想參照的對象之後緊接著放 \label{key}(在 \section{...} 之後、在 \caption{...} 之後、在方程式裡面),然後在需要編號的地方寫 \ref{key};Scissor LaTeX 排版時會自動填入編號。
先加標籤,再參照
交互參照分成兩半。\label{key} 標記一個有編號的對象,\ref{key} 則印出它的編號。鍵可以是你選擇的任何名稱;sec:、fig:、tab: 和 eq: 這類前綴只是讓鍵更好讀的習慣,並不是規則。
參照一個章節和一張圖片
\section{Results}
\label{sec:results}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\draw (0,0) -- (1,0.8) -- (2,0.3) -- (3,1.1);
\end{tikzpicture}
\caption{Measured values.}
\label{fig:values}
\end{figure}
Section~\ref{sec:results} shows the values in Figure~\ref{fig:values}.文字和參照之間的 ~ 是永遠不會斷行的空格,所以「Figure」和它的編號會留在同一行。
標籤要放在哪裡
標籤會取得它前面最近的那個有編號對象的編號:
| 要參照的對象 | \label 放在 | \ref 印出 |
|---|---|---|
| 章節 | 緊接在 \section{...}(或 \subsection、\chapter)之後 | 2、2.1 |
| 圖片或表格 | 浮動物件內、\caption{...} 之後 | 1 |
| 方程式 | equation 或編號的 align 裡面 | 1 |
| 清單項目 | enumerate 中的 \item 之後 | 2、2b |
| 定理 | theorem、lemma 及類似環境裡面 | 1 |
| 註腳 | \footnote{...} 的文字裡面 | 2 |
方程式
\eqref{key} 會以括號印出方程式編號,這也是方程式編號常見的呈現方式。\ref{key} 則只給出編號本身。
方程式的 \eqref 和 \ref
\begin{equation}
a^2 + b^2 = c^2
\label{eq:pythagoras}
\end{equation}
Equation~\eqref{eq:pythagoras} is famous; its number is \ref{eq:pythagoras}.在 Scissor LaTeX 中,align 或 gather 區塊只有一個編號,而不是每一列一個。區塊中只有第一個 \label 會被保留,所以參照後面某一列的標籤會印出 ??。細節請見方程式、對齊與編號。
會加上名稱的指令:\autoref 和 \cref
\autoref(來自 hyperref)和 \cref(來自 cleveref)會把對象的種類連同編號一起印出,所以你不必自己寫「Figure」。\cref 遵循 cleveref 的預設:小寫和簡稱(「fig.」、「eq.」),而在句首使用 \Cref 則會得到首字母大寫的完整名稱。
\autoref、\cref 和 \Cref
\section{Method}
\label{sec:method}
\begin{equation}
y = 44x
\label{eq:line}
\end{equation}
\begin{table}[h]
\centering
\caption{Settings.}
\label{tab:settings}
\begin{tabular}{lr} Runs & 44 \\ \end{tabular}
\end{table}
\autoref{sec:method} uses \cref{tab:settings} and \cref{eq:line}.
\Cref{tab:settings} lists the runs.cleveref 也能一次參照多個標籤,或參照一個範圍:
用 \cref 參照清單和範圍
\begin{equation} a = 1 \label{eq:a} \end{equation}
\begin{equation} b = 2 \label{eq:b} \end{equation}
See \cref{eq:a,eq:b}, or \crefrange{eq:a}{eq:b}.套件選項 capitalise(或 capitalize)、noabbrev 和 nameinlink 都會被讀取,導言區中的 \crefname 和 \Crefname 也一樣。
cleveref 選項和 \crefname
\documentclass{article}
\usepackage{amsmath}
\usepackage[capitalise,noabbrev]{cleveref}
\crefname{table}{Tab.}{Tabs.}
\begin{document}
\begin{equation} E = mc^2 \label{eq:e} \end{equation}
\begin{table}[h]
\centering
\caption{Masses.}
\label{tab:m}
\begin{tabular}{l} 44 kg \\ \end{tabular}
\end{table}
\cref{eq:e} uses the mass in \cref{tab:m}.
\end{document}| 指令 | 在 Scissor LaTeX 中 | 備註 |
|---|---|---|
\ref{key} | 可以使用 | 只有編號。 |
\eqref{key} | 可以使用 | 帶括號的編號。 |
\pageref{key} | 可以使用 | 標籤所在的頁碼。 |
\autoref{key} | 部分支援 | 「Section 2」、「Figure 1」、「Table 1」、「Theorem 1」。方程式則只印出編號,而不是「Equation 1」。 |
\cref、\Cref | 可以使用 | 一個或多個標籤;使用 cleveref 的預設名稱,或你用 \crefname 設定的名稱。 |
\crefrange、\cpageref、\namecref | 可以使用 | 範圍、頁碼,或只有名稱。 |
\nameref{key} | 部分支援 | 印出編號,而不是章節標題。 |
\vref{key} | 部分支援 | 印出編號,沒有「on the next page」之類的文字。 |
\hyperref[key]{text} | 可以使用 | 把你的文字印成指向該標籤的連結。 |
頁碼與缺少的標籤
\pageref 會印出標籤所在的頁碼。參照不存在的標籤會印出粗體的 ??,就像 LaTeX 在第二次執行之前一樣,而「問題」分頁會把每個缺少的標籤各列出一次:「References to labels that do not exist (shown as ??): sec:nowhere」。
\pageref 和缺少的標籤
\section{Data}
\label{sec:data}
The data is described on page~\pageref{sec:data}.
This label does not exist: Section~\ref{sec:nowhere}.「問題」分頁: References to labels that do not exist (shown as ??): sec:nowhere
與 TeX 安裝環境的差異
相關套件請見 cleveref 和 hyperref;完整的使用手冊位於 CTAN:cleveref、hyperref。
常見問題
為什麼 LaTeX 中的參照顯示 ??
標籤不存在,或者它的拼法和 \ref 中的不同。在 Scissor LaTeX 中,這也可能表示該標籤是 align 區塊中的第二個標籤,而這種區塊只有一個編號。
怎麼用括號參照方程式?
使用 amsmath 的 \eqref{key}。它會以括號印出編號,例如 (1)。
\ref 和 \autoref 有什麼不同?
\ref 只印出編號。\autoref 會加上種類,例如「Figure 1」或「Section 2」,省去你自己打這個字的功夫。
本頁的每個範例都在建置網站時由 Scissor LaTeX 排版。 最後更新:2026-11-07