# LaTeX 快速参考

*文档结构、数学公式、表格、图片等*

> Source: LaTeX Project (latex-project.org) · MIT

## 文档结构

### 最小文档

```
\documentclass{article}
\begin{document}
Hello, LaTeX!
\end{document}
```

### 文档类

| Command | Description |
|---------|-------------|
| `article` | 短文档、论文、报告 |
| `report` | 含章节的较长文档 |
| `book` | 含部分和章节的完整书籍 |
| `beamer` | 幻灯片演示 |
| `letter` | 正式信函 |

### 导言区

```
\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, graphicx}
\title{My Document}
\author{Alice}
\date{\today}
```

### 章节命令

| Command | Description |
|---------|-------------|
| `\part{}` | 最高级（book/report） |
| `\chapter{}` | 章（仅 report/book） |
| `\section{}` | 节 |
| `\subsection{}` | 小节 |
| `\subsubsection{}` | 子小节 |
| `\paragraph{}` | 带名称的段落 |

## 文本格式

### 字体样式

| Command | Description |
|---------|-------------|
| `\textbf{bold}` | **粗体** |
| `\textit{italic}` | *斜体* |
| `\underline{text}` | 下划线 |
| `\texttt{code}` | 等宽/打字机字体 |
| `\textsc{Small Caps}` | 小型大写字母 |
| `\emph{emphasis}` | 强调（上下文感知斜体） |

### 字体大小

| Command | Description |
|---------|-------------|
| `\tiny` | 最小 |
| `\small` | 小于正常 |
| `\normalsize` | 默认大小 |
| `\large / \Large` | 大 / 更大 |
| `\huge / \Huge` | 巨大 / 最大 |

### 间距与换行

| Command | Description |
|---------|-------------|
| `\\` | 换行 |
| `\newpage` | 换页 |
| `\vspace{1cm}` | 垂直间距 |
| `\hspace{1cm}` | 水平间距 |
| `\noindent` | 取消段落缩进 |
| `~` | 不换行空格 |

## 数学模式

### 行内与独立公式

```
Inline: $E = mc^2$ or \( a^2 + b^2 = c^2 \)
Display: \[ \int_0^\infty e^{-x} dx = 1 \]
Numbered: \begin{equation}
  F = ma \label{eq:newton}
\end{equation}
```

### 常用符号

| Command | Description |
|---------|-------------|
| `^ and _` | 上标 / 下标：`x^2`、`a_i` |
| `\frac{a}{b}` | 分数：a/b |
| `\sqrt{x} / \sqrt[3]{x}` | 平方根 / 立方根 |
| `\sum_{i=1}^{n}` | 求和 |
| `\int_a^b` | 积分 |
| `\lim_{x \to 0}` | 极限 |
| `\infty` | 无穷大符号 |

### 希腊字母

| Command | Description |
|---------|-------------|
| `\alpha \beta \gamma \delta` | 小写希腊字母 |
| `\Gamma \Delta \Theta \Lambda` | 大写希腊字母 |
| `\epsilon \sigma \omega \pi` | 更多小写希腊字母 |
| `\mu \nu \rho \tau \phi` | 统计 / 物理常用 |

### 矩阵

```
\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}
% pmatrix = (), vmatrix = ||, Bmatrix = {}
```

## 环境

### 常用环境

| Command | Description |
|---------|-------------|
| `document` | 主内容区域 |
| `equation` | 带编号的数学方程 |
| `align` | 对齐的多行方程 |
| `figure` | 浮动图形 |
| `table` | 浮动表格 |
| `verbatim` | 原文文本（不格式化） |
| `abstract` | 摘要块（article） |

### 对齐方程

```
\begin{align}
  x &= a + b \\
  y &= c + d
\end{align}
% & marks alignment point, \\ breaks lines
```

## 列表与表格

### 列表

```
\begin{itemize}
  \item Bullet point
  \item Another item
\end{itemize}
\begin{enumerate}
  \item First  \item Second
\end{enumerate}
```

### 表格

```
\begin{tabular}{|l|c|r|}
  \hline
  Name & Age & Score \\ \hline
  Alice & 25 & 88 \\
  Bob & 30 & 92 \\ \hline
\end{tabular}
```

### 列格式符

| Command | Description |
|---------|-------------|
| `l / c / r` | 左对齐 / 居中 / 右对齐 |
| `\|` | 列间竖线 |
| `p{3cm}` | 段落列（固定宽度） |
| `\hline` | 水平线 |
| `\cline{2-3}` | 部分水平线（第 2-3 列） |

## 图形

### 插入图片

```
\usepackage{graphicx}  % in preamble
\begin{figure}[htbp]
  \centering
  \includegraphics[width=0.8\textwidth]{img.png}
  \caption{A figure caption}
  \label{fig:example}
\end{figure}
```

### 位置说明符

| Command | Description |
|---------|-------------|
| `h` | 此处（近似） |
| `t` | 页面顶部 |
| `b` | 页面底部 |
| `p` | 专用浮动页 |
| `!` | 覆盖内部限制 |
| `H` | 精确此处（需 `float` 包） |

### 交叉引用

```
See Figure~\ref{fig:example} on
page~\pageref{fig:example}.
% Requires two compilations to resolve
```

## 参考文献

### BibTeX 工作流

```
% In .bib file:
@article{smith2025,
  author = {Smith, John},
  title = {A Great Paper},
  journal = {Nature}, year = {2025}
}
```

### 文档中引用

```
According to~\cite{smith2025} ...
\bibliographystyle{plain}
\bibliography{refs}  % refs.bib
```

### 参考文献样式

| Command | Description |
|---------|-------------|
| `plain` | 编号，按字母排序 |
| `unsrt` | 编号，按引用顺序 |
| `abbrv` | 类似 plain，缩写姓名 |
| `apalike` | 作者-年份格式（类 APA） |

## 宏包

### 常用宏包

| Command | Description |
|---------|-------------|
| `amsmath` | 高级数学环境 |
| `graphicx` | 图片插入 |
| `hyperref` | 可点击链接与引用 |
| `geometry` | 页边距：`\usepackage[margin=1in]{geometry}` |
| `booktabs` | 专业表格（`\toprule`、`\midrule`） |
| `xcolor` | 文本和背景颜色 |
| `listings` | 源代码排版 |
| `tikz` | 程序化图形与图表 |
| `babel` | 多语言支持 |
| `natbib` | 灵活引用（作者-年份、编号） |

## 自定义命令

### 新命令

```
\newcommand{\R}{\mathbb{R}}    % shortcut
\newcommand{\norm}[1]{\|#1\|}  % 1 argument
Now use: $x \in \R$, $\norm{v}$
```

### 重定义与新环境

```
\renewcommand{\abstractname}{Summary}
\newenvironment{boxed}
  {\begin{center}\begin{tabular}{|p{0.9\textwidth}|}\hline}
  {\hline\end{tabular}\end{center}}
```

### 常用快捷命令

```
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\dd}[2]{\frac{d #1}{d #2}}
% Usage: $\pd{f}{x}$, $\dd{y}{t}$
```

## 常见模式

### 标题页

```
\begin{document}
\maketitle
\tableofcontents
\newpage
```

### 编译

| Command | Description |
|---------|-------------|
| `pdflatex doc.tex` | 编译为 PDF |
| `bibtex doc` | 处理参考文献 |
| `latexmk -pdf doc.tex` | 自动编译（处理多次运行） |
| `xelatex doc.tex` | 支持 Unicode / 自定义字体 |

### 特殊字符

| Command | Description |
|---------|-------------|
| `\% \$ \& \# \_` | 转义保留字符 |
| `\textbackslash` | 文本中的反斜杠 |
| `\{ \}` | 字面大括号 |
| ```text''` | 智能双引号 |
| `---` | 破折号（em dash） |
| `--` | 连接号（en dash） |

### 实用技巧

| Command | Description |
|---------|-------------|
| `\label{} + \ref{}` | 交叉引用任意内容 |
| `\input{file}` | 引入另一个 .tex 文件 |
| `% comment` | 单行注释 |
| `\usepackage{lipsum}` | 占位文本：`\lipsum[1-3]` |
