LaTeX 完全教程

从入门到精通,掌握专业文档排版的艺术

1. LaTeX 简介与安装

什么是 LaTeX?

LaTeX 是一种基于 TeX 的排版系统,由 Leslie Lamport 开发。它特别适合生成高印刷质量的科技和数学文档。LaTeX 让你专注于内容创作,而不是排版细节。

安装 LaTeX

1

各种系统

注册并使用 Overleaf 或者 Tex Page在线编译(Recommended!

2

Windows 系统

下载并安装 TeX Live 或 MiKTeX

3

macOS 系统

安装 MacTeX

4

Linux 系统

使用包管理器安装 texlive-full

第一个 LaTeX 文档

代码示例

\documentclass{article}
\usepackage{ctex} % 中文支持

\title{我的第一个 LaTeX 文档}
\author{LaTeX 学习者}
\date{\today}

\begin{document}

\maketitle

\section{引言}
这是我的第一个 LaTeX 文档。LaTeX 是一个强大的排版系统,特别适合制作科技文档。

\section{正文}
LaTeX 具有以下优点:
\begin{itemize}
    \item 优秀的数学公式排版
    \item 自动生成目录和参考文献
    \item 专业的文档格式
    \item 跨平台兼容性
\end{itemize}

\section{结论}
LaTeX 是学术写作的首选工具。

\end{document}

编译步骤

  1. 将代码保存为 .tex 文件
  2. 使用 XeLaTeX 或 pdfLaTeX 编译
  3. 查看生成的 PDF 文件

效果预览

我的第一个 LaTeX 文档

LaTeX 学习者

2025年10月5日

1. 引言

这是我的第一个 LaTeX 文档。LaTeX 是一个强大的排版系统,特别适合制作科技文档。

2. 正文

LaTeX 具有以下优点:

  • 优秀的数学公式排版
  • 自动生成目录和参考文献
  • 专业的文档格式
  • 跨平台兼容性

3. 结论

LaTeX 是学术写作的首选工具。

2. 文档结构

导言区

  • 文档类声明
  • 宏包导入
  • 标题信息设置
  • 自定义命令

正文区

  • 章节标题
  • 段落文本
  • 列表环境
  • 公式和图表

常用文档类

article

用于学术论文、报告、文章等短文档

\documentclass{article}

report

用于较长的报告,支持章节分页

\documentclass{report}

book

用于书籍,支持双面排版和索引

\documentclass{book}

beamer

用于制作演示文稿

\documentclass{beamer}

章节结构示例

代码示例

\documentclass{report}
\usepackage{ctex}

\begin{document}

\title{文档结构示例}
\author{作者姓名}
\date{}
\maketitle

\tableofcontents % 生成目录

\chapter{第一章}
\section{第一节}
这是第一节的内容。

\subsection{第一小节}
这是第一小节的内容。

\subsubsection{第一小小节}
这是第一小小节的内容。

\section{第二节}
这是第二节的内容。

\chapter{第二章}
这是第二章的内容。

\end{document}

效果预览

文档结构示例

作者姓名

目录

第一章 ......................................................... 3

1.1 第一节 ..................................................... 3

1.1.1 第一小节 ................................................. 3

1.1.1.1 第一小小节 ............................................. 3

1.2 第二节 ..................................................... 3

第二章 ......................................................... 4

第一章

1.1 第一节

这是第一节的内容。

1.1.1 第一小节

这是第一小节的内容。

1.1.1.1 第一小小节

这是第一小小节的内容。

1.2 第二节

这是第二节的内容。

第二章

这是第二章的内容。

3. 文本格式

基础文本格式

代码示例

% 文本格式示例
\textbf{粗体文本}

\textit{斜体文本}

\textsl{倾斜文本}

\textsc{小型大写字母}

\textsf{无衬线字体}

\texttt{等宽字体}

\underline{下划线文本}

\sout{删除线文本}

\textcolor{red}{红色文本}

{\Huge 超大号字体}
{\huge 大号字体}
{\Large 大字体}
{\large 稍大字体}
{正常字体}
{\small 小字体}
{\footnotesize 脚注字体}
{\scriptsize 非常小字体}
{\tiny 极小字体}

效果预览

粗体文本

斜体文本

倾斜文本

小型大写字母

无衬线字体

等宽字体

下划线文本

删除线文本

红色文本

超大号字体

大号字体

大字体

稍大字体

正常字体

小字体

脚注字体

非常小字体

极小字体

文本对齐方式

代码示例

% 文本对齐方式
\begin{center}
居中对齐的文本\\
这是第二行
\end{center}

\begin{flushleft}
左对齐的文本\\
这是第二行
\end{flushleft}

\begin{flushright}
右对齐的文本\\
这是第二行
\end{flushright}

% 段落对齐
\noindent 无缩进段落:这是一个没有首行缩进的段落。

有缩进段落:这是一个有首行缩进的正常段落。

\parbox[t]{8cm}{
    这是一个固定宽度的文本框,内容会自动换行。
    可以设置宽度和垂直对齐方式。
}

效果预览

居中对齐的文本

这是第二行

左对齐的文本

这是第二行

右对齐的文本

这是第二行

无缩进段落:这是一个没有首行缩进的段落。

有缩进段落:这是一个有首行缩进的正常段落。

这是一个固定宽度的文本框,内容会自动换行。可以设置宽度和垂直对齐方式。

列表环境

代码示例

% 无序列表
\begin{itemize}
    \item 第一个项目
    \item 第二个项目
    \begin{itemize}
        \item 子项目1
        \item 子项目2
    \end{itemize}
    \item 第三个项目
\end{itemize}

% 有序列表
\begin{enumerate}
    \item 第一步
    \item 第二步
    \begin{enumerate}
        \item 子步骤a
        \item 子步骤b
    \end{enumerate}
    \item 第三步
\end{enumerate}

% 描述列表
\begin{description}
    \item[LaTeX] 专业的排版系统
    \item[TeX] LaTeX 的基础
    \item[BibTeX] 参考文献管理工具
\end{description}

% 自定义列表
\begin{list}{\checkmark}{}
    \item 已完成项目1
    \item 已完成项目2
    \item 待完成项目3
\end{list}

效果预览

无序列表
  • 第一个项目
  • 第二个项目
    • 子项目1
    • 子项目2
  • 第三个项目
有序列表
  1. 第一步
  2. 第二步
    1. 子步骤a
    2. 子步骤b
  3. 第三步
描述列表
LaTeX
专业的排版系统
TeX
LaTeX 的基础
BibTeX
参考文献管理工具
自定义列表
  • ✓ 已完成项目1
  • ✓ 已完成项目2
  • ✓ 待完成项目3

4. 表格制作

在LaTeX表格中,`|c|c|c|`用于定义列格式:`c`表示列内容居中对齐,`|`表示添加垂直线;整体意为创建3列居中对齐且两侧及列间都有竖线的表格。其他用法包括:`l`左对齐、`r`右对齐;`p{宽度}`设置固定列宽(内容自动换行);`m{宽度}`垂直居中固定列宽;`|`可灵活控制竖线位置(如`c|c`表示列间有竖线)。


基础表格

代码示例

% 基础表格
\begin{tabular}{|c|l|r|}
    \hline
    居中对齐 & 左对齐 & 右对齐 \\
    \hline
    数据1 & 数据2 & 数据3 \\
    \hline
    数据4 & 数据5 & 数据6 \\
    \hline
\end{tabular}

% 合并单元格
\begin{tabular}{|c|c|c|}
    \hline
    \multicolumn{3}{|c|}{合并三列} \\
    \hline
    \multirow{2}{*}{合并两行} & 数据1 & 数据2 \\
    \cline{2-3}
    & 数据3 & 数据4 \\
    \hline
\end{tabular}

% 三线表
\begin{tabular}{lccr}
    \toprule
    项目 & 数值1 & 数值2 & 总计 \\
    \midrule
    A & 10 & 20 & 30 \\
    B & 15 & 25 & 40 \\
    C & 5 & 35 & 40 \\
    \bottomrule
\end{tabular}

效果预览

居中对齐 左对齐 右对齐
数据1 数据2 数据3
数据4 数据5 数据6
合并三列
合并两行 数据1 数据2
数据3 数据4
项目 数值1 数值2 总计
A 10 20 30
B 15 25 40
C 5 35 40

长表格 (longtable)

代码示例

\usepackage{longtable}

\begin{longtable}{|l|c|r|}
    \caption{学生成绩表}
    \label{tab:scores} \\
    \hline
    \textbf{姓名} & \textbf{学号} & \textbf{成绩} \\
    \hline
    \endhead % 表头
    \hline
    \multicolumn{3}{r}{续下页...} \\
    \hline
    \endfoot % 表脚
    \hline
    \endlastfoot % 最后一页的表脚
    
    张三 & 2023001 & 85 \\
    \hline
    李四 & 2023002 & 92 \\
    \hline
    王五 & 2023003 & 78 \\
    \hline
    赵六 & 2023004 & 88 \\
    \hline
    钱七 & 2023005 & 95 \\
    \hline
    孙八 & 2023006 & 81 \\
    \hline
    周九 & 2023007 & 90 \\
    \hline
    吴十 & 2023008 & 87 \\
    \hline
    % 更多数据行...
    
\end{longtable}

特点说明

  • 自动跨页显示
  • 每页都显示表头
  • 可设置表脚和最后一页的表脚
  • 支持表格标题和标签

复杂表格示例

代码示例

\usepackage{multirow}
\usepackage{booktabs}

\begin{tabular}{cccc}
    \toprule
    \multirow{2}{*}{\textbf{类别}} & \multicolumn{3}{c}{\textbf{性能指标}} \\
    \cmidrule(lr){2-4}
    & 精度 & 召回率 & F1分数 \\
    \midrule
    方法A & 85.2\% & 82.1\% & 83.6\% \\
    方法B & 88.9\% & 86.7\% & 87.8\% \\
    方法C & 91.5\% & 89.3\% & 90.4\% \\
    \midrule
    \textbf{平均值} & \textbf{88.5\%} & \textbf{86.0\%} & \textbf{87.3\%} \\
    \bottomrule
\end{tabular}

% 带颜色的表格
\usepackage{xcolor}
\begin{tabular}{|c|c|c|}
    \hline
    \rowcolor{gray!20}
    项目 & 数值 & 状态 \\
    \hline
    A & 100 & \cellcolor{green!20}正常 \\
    \hline
    B & 50 & \cellcolor{yellow!20}警告 \\
    \hline
    C & 10 & \cellcolor{red!20}异常 \\
    \hline
\end{tabular}

效果预览

类别 性能指标
精度 召回率 F1分数
方法A 85.2% 82.1% 83.6%
方法B 88.9% 86.7% 87.8%
方法C 91.5% 89.3% 90.4%
平均值 88.5% 86.0% 87.3%
项目 数值 状态
A 100 正常
B 50 警告
C 10 异常

5. 数学公式

行内公式

代码示例

% 行内公式使用 $...$
勾股定理:$a^2 + b^2 = c^2$

欧拉恒等式:$e^{i\pi} + 1 = 0$

分数:$\frac{a}{b} + \frac{c}{d} = \frac{ad + bc}{bd}$

根号:$\sqrt{x^2 + y^2}$

求和:$\sum_{i=1}^{n} i = \frac{n(n+1)}{2}$

积分:$\int_{a}^{b} f(x) dx$

极限:$\lim_{x \to \infty} \frac{1}{x} = 0$

矩阵:$\begin{pmatrix} a & b \\ c & d \end{pmatrix}$

效果预览

勾股定理:a² + b² = c²

欧拉恒等式:e^(iπ) + 1 = 0

分数:a/b + c/d = (ad + bc)/bd

根号:√(x² + y²)

求和:∑(i=1 to n) i = n(n+1)/2

积分:∫(a to b) f(x) dx

极限:lim(x→∞) 1/x = 0

矩阵:(a b; c d)

行间公式

代码示例

% 不编号的行间公式
\[
\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}
\]

% 编号的行间公式
\begin{equation}
\frac{d}{dx} f(x) = f'(x)
\end{equation}

% 多行公式
\begin{equation}
\begin{split}
\sin(A + B) &= \sin A \cos B + \cos A \sin B \\
\cos(A + B) &= \cos A \cos B - \sin A \sin B
\end{split}
\end{equation}

% 方程组
\begin{equation}
\begin{cases}
a_1x + b_1y = c_1 \\
a_2x + b_2y = c_2
\end{cases}
\end{equation}

效果预览

∫(-∞ to ∞) e^(-x²) dx = √π

df(x)/dx = f'(x)

(1)

sin(A + B) = sin A cos B + cos A sin B

cos(A + B) = cos A cos B - sin A sin B

(2)

{ a₁x + b₁y = c₁

a₂x + b₂y = c₂

(3)

复杂公式示例

代码示例

% 微分方程
\begin{equation}
\frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2} = f(x,y)
\end{equation}

% 矩阵方程
\begin{equation}
\begin{pmatrix}
a_{11} & a_{12} & \cdots & a_{1n} \\
a_{21} & a_{22} & \cdots & a_{2n} \\
\vdots & \vdots & \ddots & \vdots \\
a_{n1} & a_{n2} & \cdots & a_{nn}
\end{pmatrix}
\begin{pmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{pmatrix}
=
\begin{pmatrix} b_1 \\ b_2 \\ \vdots \\ b_n \end{pmatrix}
\end{equation}

% 概率公式
\begin{equation}
P(A|B) = \frac{P(B|A)P(A)}{P(B)}
\end{equation}

% 泰勒展开
\begin{equation}
f(x) = f(a) + f'(a)(x-a) + \frac{f''(a)}{2!}(x-a)^2 + \cdots + \frac{f^{(n)}(a)}{n!}(x-a)^n + R_n
\end{equation}

常用数学符号

\alpha → α
\beta → β
\gamma → γ
\delta → δ
\epsilon → ε
\lambda → λ
\mu → μ
\pi → π
\sigma → σ
\tau → τ
\phi → φ
\theta → θ

6. 参考文献

BibTeX 基础

.bib 文件示例

@article{einstein1905,
    author = {Einstein, Albert},
    title = {Zur Elektrodynamik bewegter K{\"o}rper},
    journal = {Annalen der Physik},
    volume = {322},
    number = {10},
    pages = {891--921},
    year = {1905},
    publisher = {Wiley-VCH}
}

@book{latexcompanion,
    author = {Goossens, Michel and Mittelbach, Frank and Samarin, Alexander},
    title = {The \LaTeX\ Companion},
    edition = {2},
    publisher = {Addison-Wesley},
    year = {2004}
}

@inproceedings{lecun1998gradient,
    author = {LeCun, Yann and Bottou, L{\'e}on and Bengio, Yoshua and Haffner, Patrick},
    title = {Gradient-based learning applied to document recognition},
    booktitle = {Proceedings of the IEEE},
    volume = {86},
    number = {11},
    pages = {2278--2324},
    year = {1998},
    organization = {IEEE}
}

文献类型说明

@article

期刊文章

必填字段:author, title, journal, year

@book

书籍

必填字段:author/editor, title, publisher, year

@inproceedings

会议论文

必填字段:author, title, booktitle, year

在文档中引用

代码示例

\documentclass{article}
\usepackage{ctex}
\usepackage{cite} % 引用宏包

\begin{document}

\section{引言}
爱因斯坦在相对论中提出了著名的质能方程 \cite{einstein1905}。
LaTeX 是一个强大的排版系统 \cite{latexcompanion}。
深度学习在图像识别方面取得了显著进展 \cite{lecun1998gradient}。

\section{相关工作}
多项研究表明 \cite{einstein1905, latexcompanion},
传统方法在某些场景下仍然有效。

% 生成参考文献列表
\bibliographystyle{plain} % 参考文献样式
\bibliography{references} % .bib 文件名

\end{document}

常用引用命令

\cite{key}

基本引用

\citet{key}

文本引用(作者,年份)

\citep{key}

括号引用(作者,年份)

\cite{key1,key2}

多重引用

参考文献样式

常用样式

\bibliographystyle{plain}

按字母顺序排列,数字编号

\bibliographystyle{unsrt}

按引用顺序排列,数字编号

\bibliographystyle{alpha}

作者-年份字母标签

\bibliographystyle{abbrv}

缩写格式,数字编号

效果示例

[1] Einstein, A. Zur Elektrodynamik bewegter K{\"o}rper. Annalen der Physik, 322(10):891–921, 1905.

[2] Goossens, M., Mittelbach, F., and Samarin, A. The LaTeX Companion. Addison-Wesley, 2nd edition, 2004.

[3] LeCun, Y., Bottou, L., Bengio, Y., and Haffner, P. Gradient-based learning applied to document recognition. Proceedings of the IEEE, 86(11):2278–2324, 1998.

7. TikZ 绘图

基础图形

代码示例

\usepackage{tikz}

% 基本图形
\begin{tikzpicture}
    % 直线
    \draw (0,0) -- (2,0);
    
    % 带箭头的直线
    \draw[->] (0,1) -- (2,1);
    
    % 矩形
    \draw (0,2) rectangle (2,3);
    
    % 圆
    \draw (1,4) circle (0.5);
    
    % 椭圆
    \draw (1,5.5) ellipse (0.8 and 0.3);
    
    % 填充图形
    \fill[red!30] (0,6.5) rectangle (2,7.5);
    \fill[blue!30] (1,8.5) circle (0.5);
\end{tikzpicture}

% 颜色和样式
\begin{tikzpicture}
    \draw[black, thick] (0,0) -- (3,0);
    \draw[red, dashed] (0,0.5) -- (3,0.5);
    \draw[blue, dotted] (0,1) -- (3,1);
    \draw[green, dashdotted] (0,1.5) -- (3,1.5);
    \draw[orange, thick, ->] (0,2) -- (3,2);
\end{tikzpicture}

效果预览

直线
带箭头直线
矩形
椭圆

流程图

代码示例

\begin{tikzpicture}[
    block/.style = {rectangle, draw, fill=blue!20, 
                   text width=5em, text centered, rounded corners, minimum height=2em},
    decision/.style = {diamond, draw, fill=red!20, 
                     text width=4em, text centered, rounded corners, minimum height=2em},
    startstop/.style = {ellipse, draw, fill=green!20, 
                      text width=5em, text centered, rounded corners, minimum height=2em},
    arrow/.style = {thick,->,>=stealth}
]

    % 节点定义
    \node [startstop] (start) {开始};
    \node [block, below of=start] (input) {输入数据};
    \node [decision, below of=input] (decision) {数据有效?};
    \node [block, below of=decision, yshift=-0.5cm] (process) {处理数据};
    \node [block, right of=decision, xshift=4cm] (error) {显示错误};
    \node [startstop, below of=process] (end) {结束};

    % 箭头连接
    \draw [arrow] (start) -- (input);
    \draw [arrow] (input) -- (decision);
    \draw [arrow] (decision) -- node[anchor=east] {是} (process);
    \draw [arrow] (decision) -- node[anchor=south] {否} (error);
    \draw [arrow] (process) -- (end);
    \draw [arrow] (error) |- (input);

\end{tikzpicture}

流程图元素

开始/结束

ellipse 形状

处理步骤

rectangle 形状

决策

diamond 形状

数学图形

函数图像

\begin{tikzpicture}[scale=1.2]
    % 坐标轴
    \draw[->] (-3,0) -- (3,0) node[right] {$x$};
    \draw[->] (0,-1.5) -- (0,1.5) node[above] {$y$};
    
    % 网格
    \draw[dashed,gray!30] (-3,-1.5) grid (3,1.5);
    
    % 函数图像
    \draw[blue, thick] plot[domain=-3:3, samples=100] (\x, {sin(\x r)});
    \draw[red, thick] plot[domain=-3:3, samples=100] (\x, {cos(\x r)});
    
    % 图例
    \node[blue] at (2, 0.8) {$\sin(x)$};
    \node[red] at (2, 0.3) {$\cos(x)$};
\end{tikzpicture}

% 几何图形
\begin{tikzpicture}
    % 三角形
    \draw[thick] (0,0) -- (3,0) -- (1.5,2.6) -- cycle;
    
    % 高线
    \draw[dashed] (1.5,2.6) -- (1.5,0);
    
    % 标签
    \node at (-0.3,-0.3) {$A$};
    \node at (3.3,-0.3) {$B$};
    \node at (1.8,2.8) {$C$};
    \node at (1.8,1) {$h$};
\end{tikzpicture}

效果说明

三角函数图像

使用 plot 命令绘制 sin(x) 和 cos(x) 函数

  • • domain 设置定义域
  • • samples 设置采样点数
  • • r 表示弧度制
几何图形

绘制三角形并添加高线和标签

  • • cycle 闭合图形
  • • dashed 设置虚线样式
  • • node 添加文本标签

8. 自定义命令

基本自定义命令

代码示例

% 无参数命令
\newcommand{\latex}{\LaTeX}
\newcommand{\myclass}{计算机科学与技术}

% 带一个参数的命令
\newcommand{\emphasis}[1]{\textbf{\textit{#1}}}
\newcommand{\highlight}[1]{\textcolor{red}{#1}}

% 带两个参数的命令
\newcommand{\authorinfo}[2]{作者:#1,单位:#2}
\newcommand{\equationref}[2]{公式 (\ref{#1}) 中的 #2}

% 带可选参数的命令
\newcommand{\figurewithcaption}[3][htbp]{
    \begin{figure}[#1]
        \centering
        \includegraphics{#2}
        \caption{#3}
    \end{figure}
}

% 使用示例
\authorinfo{张三}{清华大学}
\emphasis{重要内容}
\highlight{重点强调}
\figurewithcaption[width=0.8\textwidth]{image.png}{示例图片}

使用说明

命令格式

\newcommand{\命令名}[参数个数]{定义}

参数用 #1, #2, ..., #9 表示

可选参数

\newcommand{\命令名}[参数个数][默认值]{定义}

使用时用方括号指定可选参数

重定义命令

\renewcommand{\已有命令}{新定义}

用于修改已存在的命令

高级自定义命令

代码示例

% 条件命令
\newcommand{\grade}[1]{
    \ifnum#1 >= 90
        \textcolor{green}{优秀}
    \else\ifnum#1 >= 80
        \textcolor{blue}{良好}
    \else\ifnum#1 >= 70
        \textcolor{orange}{中等}
    \else\ifnum#1 >= 60
        \textcolor{purple}{及格}
    \else
        \textcolor{red}{不及格}
    \fi\fi\fi\fi
}

% 循环命令
\newcounter{loopcounter}
\newcommand{\repeattext}[3]{
    \setcounter{loopcounter}{0}
    \loop
        #1
        \stepcounter{loopcounter}
        \ifnum\value{loopcounter} < #2
    \repeat
    #3
}

% 复杂环境定义
\newenvironment{mybox}[2][gray!20]{
    \begin{tcolorbox}[
        colback=#1,
        colframe=black,
        title=#2
    ]
}{
    \end{tcolorbox}
}

% 数学命令
\newcommand{\derivative}[2]{\frac{d#1}{d#2}}
\newcommand{\partialderivative}[2]{\frac{\partial#1}{\partial#2}}
\newcommand{\integral}[4][]{
    \int_{#2}^{#3} #4 \, d#1
}

% 使用示例
\grade{85} % 输出:良好
\repeattext{步骤}{3}{完成} % 输出:步骤步骤步骤完成
\begin{mybox}[blue!10]{重要提示}
    这是一个重要提示框
\end{mybox}
\derivative{y}{x} % 输出:dy/dx
\integral{x}{a}{b}{f(x)} % 输出:∫(a to b) f(x) dx

常用自定义命令示例

\newcommand{\email}[1]{\href{mailto:#1}{#1}}

创建邮件链接

\newcommand{\url}[1]{\href{#1}{#1}}

创建网页链接

\newcommand{\figref}[1]{图 \ref{#1}}

引用图片

\newcommand{\tabref}[1]{表 \ref{#1}}

引用表格

自定义环境

代码示例

% 基本环境定义
\newenvironment{myenvironment}{
    % 环境开始部分
    \begin{center}
    \begin{tabular}{|p{0.8\textwidth}|}
    \hline
    \textbf{自定义环境:}
}{
    % 环境结束部分
    \\\hline
    \end{tabular}
    \end{center}
}

% 带参数的环境
\newenvironment{quotationwithauthor}[2][gray!10]{
    \begin{quote}[#1]
        \itshape #2
}{
    \end{quote}
}

% 定理环境
\newtheorem{theorem}{定理}[section]
\newtheorem{lemma}[theorem]{引理}
\newtheorem{corollary}[theorem]{推论}

% 证明环境
\newenvironment{proof}{
    \begin{trivlist}
    \item[\hskip \labelsep {\bfseries 证明:}]
}{
    \qed
    \end{trivlist}
}

% 使用示例
\begin{myenvironment}
    这是自定义环境的内容
\end{myenvironment}

\begin{quotationwithauthor}[Einstein]{
    想象力比知识更重要,因为知识是有限的,而想象力概括着世界上的一切。
}

\begin{theorem}
    对于任意实数 $a$ 和 $b$,有 $a^2 + b^2 \geq 2ab$。
\end{theorem}

\begin{proof}
    因为 $(a - b)^2 \geq 0$,展开得 $a^2 - 2ab + b^2 \geq 0$,
    所以 $a^2 + b^2 \geq 2ab$。
\end{proof}

环境定义说明

环境格式

\newenvironment{环境名}{开始代码}{结束代码}

开始代码在环境内容前执行,结束代码在环境内容后执行

带参数环境

\newenvironment{环境名}[参数个数]{开始代码}{结束代码}

参数用 #1, #2 等表示

定理环境

使用 newtheorem 创建自动编号的定理环境

[theorem] 表示共享编号

9. 常用宏包

基础宏包

amsmath

增强的数学公式支持

提供 align、gather 等环境

amssymb

数学符号扩展

提供更多数学符号

graphicx

图片插入

支持多种图片格式

hyperref

超链接支持

创建交叉引用和网页链接

geometry

页面布局设置

灵活设置页边距

fancyhdr

自定义页眉页脚

支持复杂的页眉页脚格式

multirow

表格合并行

在表格中合并多行单元格

booktabs

专业表格线

创建高质量的三线表

中文支持宏包

主要宏包

% CTeX 宏包(推荐)
\usepackage{ctex}

% 文档类方式
\documentclass{ctexart}    % 对应 article
\documentclass{ctexrep}    % 对应 report
\documentclass{ctexbook}   % 对应 book
\documentclass{ctexbeamer} % 对应 beamer

% 配置选项
\usepackage[UTF8]{ctex}
\usepackage[fontset=windows]{ctex}  % Windows 字体
\usepackage[fontset=mac]{ctex}      % macOS 字体
\usepackage[fontset=fandol]{ctex}   % Fandol 字体

% 字体设置
\setmainfont{Times New Roman}        % 英文正文
\setCJKmainfont{宋体}                % 中文正文
\setCJKsansfont{黑体}                % 中文无衬线
\setCJKmonofont{楷体}                % 中文等宽

% 段落设置
\CTEXsetup[indent={2\ccwd}]{paragraph} % 段落缩进
\CTEXsetup{section={name={第,章},format={\Huge\bfseries}}} % 章节格式

使用说明

CTeX 宏包特点
  • • 自动检测编码
  • • 支持多种字体配置
  • • 提供中文段落格式
  • • 支持中文断行和 hyphenation
编译方式

推荐使用 XeLaTeX 或 LuaLaTeX 编译

  • • XeLaTeX: 支持系统字体
  • • LuaLaTeX: 支持更多高级特性
  • • pdfLaTeX: 需要配置 CJK 字体

专业宏包

科学计算

numpy

数值计算

scipy

科学计算

matplotlib

数据可视化

图形绘制

tikz

矢量图形

pgfplots

数据图表

asymptote

高级图形

文档格式

beamer

演示文稿

tikz-cd

交换图表

biblatex

参考文献

10. Beamer 演示文稿

Beamer 基础

基本结构

\documentclass{beamer}
\usepackage{ctex}

% 设置主题
\usetheme{Warsaw}
\usecolortheme{beaver}
\useinnertheme{rounded}
\useoutertheme{infolines}

% 标题信息
\title{Beamer 演示文稿示例}
\subtitle{LaTeX 教程}
\author{演示者姓名}
\institute{机构名称}
\date{\today}
\logo{\includegraphics[width=1cm]{logo.png}}

\begin{document}

% 标题页
\frame{\titlepage}

% 目录页
\begin{frame}
    \frametitle{目录}
    \tableofcontents
\end{frame}

% 正文页
\section{第一部分}
\begin{frame}
    \frametitle{第一页标题}
    \framesubtitle{副标题}
    
    这是第一页的内容。
    
    \begin{itemize}
        \item 项目1
        \item 项目2
        \item 项目3
    \end{itemize}
\end{frame}

\end{document}

主题设置

内置主题
Warsaw
Berlin
Madrid
Frankfurt
Copenhagen
Singapore
颜色主题
beaver
dolphin
seahorse
whale

动画效果

代码示例

% 渐进显示
\begin{frame}
    \frametitle{渐进显示示例}
    
    \begin{itemize}[<+->]
        \item 第一点
        \item 第二点
        \item 第三点
        \item 第四点
    \end{itemize}
\end{frame}

% 手动控制
\begin{frame}
    \frametitle{手动控制动画}
    
    \only<1>{这是第一页内容}
    \only<2>{这是第二页内容}
    \only<3>{这是第三页内容}
    
    \begin{itemize}
        \item<1-> 始终显示的项目
        \item<2-> 从第二页开始显示
        \item<3> 只在第三页显示
    \end{itemize}
\end{frame}

% 覆盖效果
\begin{frame}
    \frametitle{覆盖效果}
    
    \uncover<1->{这是基本内容}
    
    \uncover<2->{
        \begin{equation}
        E = mc^2
        \end{equation}
    }
    
    \uncover<3->{这是额外说明}
\end{frame}

% 透明度控制
\begin{frame}
    \frametitle{透明度动画}
    
    \setbeamercovered{transparent}
    
    \begin{itemize}[<+->]
        \item 重点内容
        \item 次要内容
        \item 补充说明
    \end{itemize}
\end{frame}

动画命令说明

<1->

从第1页开始显示

<-3>

显示到第3页

<2-4>

在第2-4页显示

<+->

渐进显示

复杂页面布局

分栏布局

% 分栏布局
\begin{frame}
    \frametitle{分栏布局示例}
    
    \begin{columns}[t]
        \column{0.5\textwidth}
            \textbf{左侧内容}
            \begin{itemize}
                \item 项目1
                \item 项目2
                \item 项目3
            \end{itemize}
        
        \column{0.5\textwidth}
            \textbf{右侧内容}
            \[
            f(x) = \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}
            \]
    \end{columns}
\end{frame}

% 嵌套分栏
\begin{frame}
    \frametitle{嵌套分栏}
    
    \begin{columns}
        \column{0.6\textwidth}
            主要内容区域
            
            \begin{columns}[t]
                \column{0.5\textwidth}
                    子栏1
                \column{0.5\textwidth}
                    子栏2
            \end{columns}
        
        \column{0.4\textwidth}
            侧边内容区域
    \end{columns}
\end{frame}

% 文字环绕图片
\begin{frame}
    \frametitle{图文混排}
    
    \begin{minipage}{0.6\textwidth}
        这是文字内容区域,可以放置大量文本。图片将显示在右侧,文字会自动环绕。
    \end{minipage}
    \hfill
    \begin{minipage}{0.35\textwidth}
        \includegraphics[width=\textwidth]{image.png}
    \end{minipage}
\end{frame}

特殊页面类型

% 空白页
\begin{frame}[plain]
    \centering
    \Huge 谢谢观看!
    \vspace{1cm}
    \Large 欢迎提问
\end{frame}

% 带注释的页面
\begin{frame}
    \frametitle{带注释的页面}
    
    这是演示内容。
    
    \note{
        这是演讲者的注释,
        只会在演讲者视图中显示。
    }
\end{frame}

% 引用页面
\begin{frame}[allowframebreaks]
    \frametitle{参考文献}
    \bibliographystyle{plain}
    \bibliography{references}
\end{frame}

% 目录导航页面
\AtBeginSection[]
{
    \begin{frame}
        \frametitle{目录}
        \tableofcontents[currentsection]
    \end{frame}
}

11. 实用技巧

调试技巧

常见错误处理

Missing $ inserted

数学模式未正确结束

检查是否遗漏了 $ 或 \)

LaTeX Error: File 'xxx.sty' not found

宏包未安装

安装相应的宏包或检查拼写

Paragraph ended before \xxx was complete

环境未正确结束

检查是否遗漏了 \end{xxx}

调试工具

\tracingall

详细的调试输出

在导言区添加,查看详细的编译过程

\show\command

显示命令定义

查看命令的具体实现

% 注释

逐行注释查找错误

通过注释定位错误位置

优化技巧

性能优化

使用 \include 和 \input

将大文档分割成多个文件

使用 \includeonly

只编译需要修改的部分

预编译图片

将复杂图形单独编译

使用缓存

利用 .aux, .bbl 等缓存文件

代码规范

统一的缩进风格

保持代码结构清晰

有意义的标签名

使用 descriptive labels

适当的注释

解释复杂的代码部分

模块化设计

将功能封装成命令或环境

学习总结

通过本教程,您已经掌握了 LaTeX 的核心功能和高级技巧。 现在您可以创建专业的学术文档、演示文稿和复杂的数学公式了。

基础扎实

掌握了文档结构、文本格式、表格制作等基础知识

公式精通

能够创建复杂的数学公式和科学表达式

高级应用

学会了绘图、自定义命令、演示文稿制作等高级功能