LaTeX 速查表
一頁式的密集速查卡,涵蓋文件骨架、文字、數學、浮動體、引用、版面、引擎、TikZ、Beamer 與常見的記錄檔錯誤。
案頭參考初學者cheatsheetreference
LaTeX 速查表
把這份放在編輯器旁邊。每一列都可以直接貼上使用。想看較完整的課程,請到學習開啟對應的學習路徑。
快速跳轉:
骨架 ·
序言區 ·
文字 ·
章節 ·
參照 ·
數學 ·
表格 ·
圖片 ·
引用 ·
版面 ·
引擎 ·
TikZ ·
Beamer ·
定理 ·
領域專用 ·
編譯 ·
錯誤 ·
套件順序
文件骨架
\documentclass[11pt,a4paper]{article} % or report, book, beamer
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{graphicx,booktabs}
\usepackage[colorlinks=true,linkcolor=blue,citecolor=blue,urlcolor=blue]{hyperref}
% \usepackage[backend=biber,style=numeric]{biblatex}
% \addbibresource{refs.bib}
\begin{abstract} ... \end{abstract}
% \bibliographystyle{plain}\bibliography{refs} % BibTeX
% \printbibliography % biblatex
| 類別 | 典型用途 |
|---|
article | 論文、筆記、作業 |
report | 有章節的較長報告 |
book | 書籍/許多學位論文 |
beamer | 簡報 |
學校的 .cls | 學位論文範本 |
| 檔案 | 用途 |
|---|
.tex | 原始碼 |
.bib | 參考文獻資料庫 |
.cls / .sty | 文件類別/套件 |
.aux .log .toc | 自動產生;請勿編輯 |
序言區常用寫法
\usepackage[margin=1in]{geometry}
\usepackage{setspace}\onehalfspacing
\usepackage{siunitx} % units and numbers
\usepackage{cleveref} % after hyperref
\DeclareMathOperator*{\argmax}{arg\,max}
\newcommand{\vect}[1]{\boldsymbol{#1}}
hyperref 要晚點載入。cleveref 要在 hyperref 之後載入。
文字與格式
| 想要 | 輸入 |
|---|
| 粗體/斜體/等寬 | \textbf{} \textit{} \texttt{} |
| 強調(可巢狀) | \emph{} |
| 小型大寫 | \textsc{} |
| 底線(少用) | \underline{} |
| 字級 | {\small } {\large } {\Large } {\footnotesize } |
| 新段落 | 空白行 |
| 換行 | \\ 或 \newline |
| 不換行空格 | ~ |
| 水平空白 | \quad \qquad \hspace{1em} |
| 垂直空白 | \vspace{1em} \bigskip |
| 置中/靠右 | center / flushright 環境 |
| 引文 | quote 或 quotation |
| 註腳 | \footnote{...} |
| 註解 | % rest of line |
特殊字元的跳脫(文字模式)
| 字元 | 寫法 | 字元 | 寫法 |
|---|
% | \% | $ | \$ |
& | \& | # | \# |
_ | \_ | { } | \{ \} |
~ | \textasciitilde | ^ | \textasciicircum |
\ | \textbackslash | | |
清單
自訂標籤:\item[(a)] ...
頁碼與分頁
| 想要 | 輸入 |
|---|
| 樣式 | \pagestyle{plain} / empty / fancy |
| 僅限這一頁 | \thispagestyle{empty} |
| 羅馬數字/阿拉伯數字 | \pagenumbering{roman} 然後 arabic |
| 新的一頁 | \newpage \clearpage |
| 軟性斷頁請求 | \pagebreak[3] |
| 保持在同一頁 | \nopagebreak |
章節與結構
\chapter{...} % report/book
\addcontentsline{toc}{section}{Unnumbered}
| 想要 | 輸入 |
|---|
| 目錄 | \tableofcontents |
| 圖表目錄 | \listoffigures \listoftables |
| 摘要 | \begin{abstract}...\end{abstract} |
| 附錄 | \appendix 然後 \section / \chapter |
| 多檔案 | \include{chapters/intro} 或 \input{...} |
| 一次草擬一章 | \includeonly{chapters/methods} |
交叉參照與連結
\section{Methods}\label{sec:methods}
\begin{equation}\label{eq:main} E=mc^2 \end{equation}
\caption{Pipeline}\label{fig:pipe}
See Section~\ref{sec:methods}, Eq.~\eqref{eq:main}, Figure~\ref{fig:pipe}.
% \cref{fig:pipe} \Cref{sec:methods}
\url{https://oleafly.com}
\href{https://oleafly.com}{Oleafly}
\hypersetup{pdftitle={My Paper}, pdfauthor={You}}
請把 \label 放在 \caption 之後,編號才會正確。
數學模式
| 想要 | 輸入 |
|---|
| 行內公式 | $...$ 或 \( ... \) |
| 獨立顯示,不編號 | \[ ... \] |
| 獨立顯示,有編號 | equation + \label |
| 多行對齊 | align / align* |
| 多行置中 | gather / gather* |
| 多行共用一個編號 | equation + split |
| 拆行的長公式 | multline |
| 取消單一編號 | \notag 或 \nonumber |
| 參照 | \ref{eq:x} 或 \eqref{eq:x} |
a &= b + c \label{eq:a} \\
分數、根號、上下標
\frac{a}{b} \dfrac{a}{b} \tfrac{a}{b}
希臘字母(數學模式)
\alpha \beta \gamma \delta \epsilon \varepsilon \zeta \eta
\theta \vartheta \iota \kappa \lambda \mu \nu \xi
\pi \varpi \rho \varrho \sigma \varsigma \tau \upsilon
\phi \varphi \chi \psi \omega
\Gamma \Delta \Theta \Lambda \Xi \Pi \Sigma \Upsilon \Phi \Psi \Omega
運算子、關係符號、集合、箭頭
\pm \mp \cdot \times \div \ast \star \circ \bullet
\leq \geq \neq \approx \equiv \sim \simeq \propto \ll \gg
\in \notin \subset \subseteq \supset \supseteq \cup \cap \emptyset
\to \mapsto \Rightarrow \Leftrightarrow \uparrow \downarrow
\infty \partial \nabla \ell \forall \exists \neg \land \lor
重音符號與字母集
\hat{x} \bar{x} \tilde{x} \vec{x} \dot{x} \ddot{x}
\widehat{xyz} \overline{ABC}
\mathbb{N Z Q R C} \mathcal{L} \mathfrak{g}
\mathrm{d}x \mathbf{A} \boldsymbol{\beta}
大型運算子與微積分
\sum_{i=1}^n \prod_{i=1}^n \int_a^b \oint \iint
\lim_{x \to 0} \max_x \min_x \inf \sup
\frac{dy}{dx} \frac{\partial f}{\partial x}
\argmax_x % after \DeclareMathOperator*{\argmax}{arg\,max}
矩陣
\begin{pmatrix} a & b \\ c & d \end{pmatrix} % ( )
\begin{bmatrix} a & b \\ c & d \end{bmatrix} % [ ]
\begin{vmatrix} a & b \\ c & d \end{vmatrix} % | |
\begin{array}{r|cc} 1 & 2 & 3 \\ \hline 4 & 5 & 6 \end{array}
分段函數
定界符
\left( \frac{a}{b} \right)
\left\{ x : x > 0 \right\}
\lvert x \rvert \lVert v \rVert
即時預覽:公式工具。
表格
\caption{Results}\label{tab:res}
Baseline & 0.81 & 0.79 \\
| 想要 | 輸入 |
|---|
| 對齊欄位 | l c r p{3cm} |
| 垂直線(避免使用) | ` |
| 跨欄 | \multicolumn{2}{c}{...} |
| 跨列 | \multirow{2}{*}{...}(multirow 套件) |
| 跨頁表格 | longtable |
| 視覺化建立工具 | 表格產生器 |
設計習慣:三條水平線,不用垂直線。
\includegraphics[width=0.8\textwidth]{figures/plot.pdf}
\caption{Overview.}\label{fig:plot}
| 想要 | 輸入 |
|---|
| 寬度 | width=0.5\textwidth 或 width=\linewidth |
| 高度 | height=4cm(除非必要,不要同時指定兩者) |
| PDF 的某一頁 | page=2 選項 |
| 並排 | 兩個 minipage,各為 0.48\textwidth |
| 子圖 | subcaption 套件 |
| 擺放位置 | [htbp](原地、頁面頂端、頁面底端、浮動體頁) |
| 屏障 | \usepackage{placeins} + \FloatBarrier |
引用與參考文獻
BibTeX(傳統)
See \cite{knuth84} and \cite{a,b}.
\bibliographystyle{plain} % abbrv, alpha, ieeetr, apalike, ...
biblatex + biber
\usepackage[backend=biber,style=numeric]{biblatex}
\addbibresource{refs.bib}
\cite{knuth84} \parencite{knuth84} \textcite{knuth84}
natbib
\citet{key} % Author (year)
\citep{key} % (Author, year)
\citep*{key} % Author, year
最簡 .bib 條目
author = {Knuth, Donald E.},
title = {Literate Programming},
journal = {The Computer Journal},
驗證:BibTeX 驗證工具。
| 症狀 | 解法 |
|---|
[?] 或 ? | 引用鍵錯誤、少了一次處理,或是 BibTeX 與 Biber 不一致 |
| 參考文獻是空的 | 路徑或後端有問題,或是沒有 \cite/印出指令用錯 |
版面
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{setspace}\onehalfspacing
\setlength{\parindent}{0pt}
\setlength{\parskip}{0.6em}
| 想要 | 輸入 |
|---|
| 只取消一次縮排 | \noindent |
| 手動指定長度 | \hspace{2em} \vspace{1ex} |
| 單位 | pt em ex cm mm in \textwidth \linewidth |
引擎與套件
| 引擎 | 適合用途 |
|---|
| pdfLaTeX | 最大的期刊相容性 |
| XeLaTeX | 系統字型、多種書寫系統(fontspec) |
| LuaLaTeX | 現代 Unicode 與腳本功能 |
| Tectonic | 內建/依需求取得的套件(Oleafly 預設) |
\setmainfont{Times New Roman}
缺少套件(TeX Live):tlmgr install siunitx
顏色
\textcolor[HTML]{2563EB}{brand}
\definecolor{brand}{HTML}{2563EB}
\colorbox{gray!12}{highlight}
TikZ(最精簡)
\usetikzlibrary{positioning,arrows.meta}
\begin{tikzpicture}[node distance=1.4cm]
\node[draw,rounded corners] (a) {In};
\node[draw,rounded corners,right=of a] (b) {Out};
\draw[-Latex] (a) -- (b);
交換圖:tikz-cd。獨立匯出:\documentclass[tikz]{standalone}。
Beamer(最精簡)
定理
\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
領域專用片段
\usepackage[version=4]{mhchem}
\ce{H2O} \ce{CO2 + C -> 2CO}
\braket{\psi|\phi} \braket{\psi|A|\phi}
\begin{lstlisting}[language=Python]
編譯流程
- 編輯
.tex
- 執行引擎(pdfLaTeX / XeLaTeX / LuaLaTeX / Tectonic)
- 如果有引用:執行 BibTeX 或 Biber
- 再次執行引擎(通常要兩次),直到
?? 與目錄穩定下來
在 Oleafly 中,編譯(或自動編譯)會處理這整個流程;SyncTeX 可在原始碼與 PDF 之間跳轉。
記錄檔錯誤(先解讀第一個)
| 症狀 | 常見原因 |
|---|
| 文字在一行中途消失 | 裸露的 % 開啟了註解 |
Missing $ inserted | 在文字中使用了 _、^ 或數學指令 |
Undefined control sequence | 打錯字或缺少套件 |
File not found | input 或圖片的路徑/副檔名錯誤 |
參照顯示為 ?? | 需要再編譯一次 |
引用顯示為 [?] | 引用鍵錯誤、後端不對,或少了 bib 這一次處理 |
| 參考文獻是空的 | \bibliography 路徑錯誤,或 biblatex 設定不一致 |
Overfull \hbox | 無法斷行的內容(網址、很長的數學式、沒有連字號) |
| Emergency stop | 缺少 \end{document}、錯誤的 include、失控的大括號 |
| Runaway argument | 缺少 } 或 \end{...} |
套件載入順序(經驗法則)
- 先放文件類別的選項
- 編碼/字型
- geometry、setspace、語言
- 數學、圖形、表格
- 特定領域的套件
hyperref 靠近最後
cleveref 在 hyperref 之後
Oleafly 學習中的實用連結
完整地圖:/learn/。
返回 案頭參考