mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4mobile wallpaper 5
1203 字
3 分钟
LaTeX语法完全指南:从入门到精通
2026-06-05

LaTeX语法完全指南:从入门到精通#

LaTeX是一种高质量的排版系统,特别适合科技文档和数学公式的排版。本文将全面介绍LaTeX的语法和使用方法。


📚 目录#


一、LaTeX简介#

1.1 什么是LaTeX?#

LaTeX(读作”拉泰赫”或”莱特克斯”)是一种基于TeX的排版系统,由Leslie Lamport在1984年开发。它的设计目标是让作者能够专注于内容,而将排版工作交给系统自动完成。

1.2 为什么使用LaTeX?#

优势说明
专业排版自动生成高质量的文档,特别适合学术论文
数学公式数学公式排版能力无与伦比
交叉引用自动生成章节、公式、图表的编号和引用
稳定性文档格式稳定,不受软件版本影响
免费开源完全免费,跨平台支持

1.3 LaTeX vs Word#

对比项LaTeXWord
学习曲线陡峭平缓
数学公式极佳一般
长文档优秀较差
版本控制支持(纯文本)困难(二进制)
协作需要Git等工具实时协作

二、文档结构#

2.1 基本文档结构#

\documentclass[选项]{文档类}
% 导言区:加载宏包、定义命令
\usepackage{宏包名}
% 正文开始
\begin{document}
% 文档内容
这里是正文内容
\end{document}

2.2 文档类#

文档类说明适用场景
article文章短文、报告、论文
report报告长报告、学位论文
book书籍书籍、教材
letter信件书信
beamer演示文稿幻灯片

2.3 文档类选项#

% 字体大小选项
\documentclass[12pt]{article} % 12号字体
% 纸张大小选项
\documentclass[a4paper]{article} % A4纸
% 双面打印
\documentclass[twoside]{article} % 双面
% 组合选项
\documentclass[12pt,a4paper,twoside]{article}

2.4 页面设置#

\usepackage{geometry}
\geometry{
a4paper,
left=2.5cm,
right=2.5cm,
top=2.5cm,
bottom=2.5cm
}

三、文本格式化#

3.1 字体样式#

% 直立字体
\textup{直立字体}
% 意大利体(斜体)
\textit{意大利体}
% 粗体
\textbf{粗体}
% 等宽字体
\texttt{等宽字体}
% 小型大写字母
\textsc{小型大写}
% 下划线
\underline{下划线}
% 删除线(需要ulem宏包)
\sout{删除线}

3.2 字体大小#

% 从小到大
{\tiny 极小}
{\scriptsize 脚本大小}
{\footnotesize 脚注大小}
{\small 小}
{\normalsize 正常}
{\large 大}
{\Large 更大}
{\LARGE 再大}
{\huge 巨大}
{\Huge 最大}

3.3 文本对齐#

% 居中环境
\begin{center}
居中文本
\end{center}
% 右对齐环境
\begin{flushright}
右对齐文本
\end{flushright}
% 左对齐环境
\begin{flushleft}
左对齐文本
\end{flushleft}

3.4 特殊字符#

\# % 井号
\$ % 美元符号
\% % 百分号
\^ % 脱字符
\& % 和号
\_ % 下划线
\{ % 左花括号
\} % 右花括号
\~ % 波浪号
\\ % 反斜杠

3.5 段落控制#

% 换行(不新起段落)
第一行内容\\
第二行内容
% 新段落
第一段内容
第二段内容
% 强制换页
\newpage
% 无缩进
\noindent 这段没有缩进
% 设置段落缩进
\setlength{\parindent}{2em}

四、数学公式#

LaTeX最强大的功能之一就是数学公式排版。

4.1 行内公式#

% 使用 $...$ 或 \(...\)
爱因斯坦的质能方程 $E=mc^2$ 是物理学的基础。
爱因斯坦的质能方程 \(E=mc^2\) 是物理学的基础。

4.2 行间公式#

% 使用 \[...\] 或 equation 环境
\[
E = mc^2
\]
% 带编号的公式
\begin{equation}
E = mc^2
\end{equation}
% 不带编号
\begin{equation*}
E = mc^2
\end{equation*}

4.3 基本数学符号#

% 运算符
+ - * / % 基本运算
\times % 乘号 ×
\div % 除号 ÷
\pm % 正负 ±
\mp % 负正 ∓
\cdot % 点乘 ·
% 关系符
= % 等号
\neq % 不等号 ≠
< > % 小于大于
\leq % 小于等于 ≤
\geq % 大于等于 ≥
\approx % 约等于 ≈
\equiv % 恒等于 ≡
\sim % 相似 ~

4.4 上下标#

% 上标
x^2 % x的平方
x^{2n} % x的2n次方(多字符需要用花括号)
% 下标
a_1 % a下标1
a_{n+1} % a下标n+1
% 组合
x_i^2 % x下标i,上标2
x_{i}^{2} % 同上,更清晰

4.5 分数与根号#

% 分数
\frac{分子}{分母}
\frac{1}{2} % 二分之一
\frac{a+b}{c+d} % 分数
% 根号
\sqrt{x} % 平方根
\sqrt[3]{x} % 立方根
\sqrt[n]{x} % n次根

4.6 求和与积分#

% 求和
\sum_{i=1}^{n} x_i % 求和符号
\sum_{i=1}^{\infty} x_i % 无穷求和
% 积分
\int_{a}^{b} f(x) dx % 定积分
\int f(x) dx % 不定积分
\iint f(x,y) dxdy % 二重积分
\iiint f(x,y,z) dxdydz % 三重积分
% 极限
\lim_{x \to \infty} f(x) % 极限
\lim_{x \to 0} \frac{\sin x}{x} = 1

4.7 矩阵#

% 基本矩阵
\begin{matrix}
a & b \\
c & d
\end{matrix}
% 带括号的矩阵
\begin{pmatrix}
a & b \\
c & d
\end{pmatrix}
% 方括号矩阵
\begin{bmatrix}
a & b \\
c & d
\end{bmatrix}
% 花括号矩阵
\begin{Bmatrix}
a & b \\
c & d
\end{Bmatrix}
% 行列式
\begin{vmatrix}
a & b \\
c & d
\end{vmatrix}

4.8 多行公式#

% align 环境(对齐等号)
\begin{align}
f(x) &= (x+1)^2 \\
&= x^2 + 2x + 1
\end{align}
% 不带编号
\begin{align*}
f(x) &= (x+1)^2 \\
&= x^2 + 2x + 1
\end{align*}
% cases 环境(分段函数)
f(x) = \begin{cases}
x^2 & \text{if } x \geq 0 \\
-x & \text{if } x < 0
\end{cases}

4.9 希腊字母#

% 小写希腊字母
\alpha \beta \gamma \delta \epsilon \zeta \eta \theta
\iota \kappa \lambda \mu \nu \xi \pi \rho \sigma \tau
\upsilon \phi \chi \psi \omega
% 大写希腊字母
\Gamma \Delta \Theta \Lambda \Xi \Pi \Sigma \Phi \Psi \Omega

4.10 数学字体#

% 粗体(向量、矩阵)
\mathbf{ABC} % 粗体
\boldsymbol{ABC} % 粗斜体
% 花体
\mathcal{ABC} % 花体字母
% 黑板粗体
\mathbb{R} % 实数集 ℝ
\mathbb{N} % 自然数集 ℕ
\mathbb{Z} % 整数集 ℤ
\mathbb{Q} % 有理数集 ℚ
\mathbb{C} % 复数集 ℂ
% 罗马字体
\mathrm{ABC} % 直立字体
% 斜体
\mathit{ABC} % 斜体
% 等宽
\mathtt{ABC} % 等宽字体

五、列表#

5.1 无序列表#

\begin{itemize}
\item 第一项
\item 第二项
\item 第三项
\end{itemize}

5.2 有序列表#

\begin{enumerate}
\item 第一项
\item 第二项
\item 第三项
\end{enumerate}

5.3 描述列表#

\begin{description}
\item[术语1] 解释1
\item[术语2] 解释2
\item[术语3] 解释3
\end{description}

5.4 嵌套列表#

\begin{enumerate}
\item 第一层
\begin{enumerate}
\item 第二层
\item 第二层
\end{enumerate}
\item 第一层
\end{enumerate}

六、表格#

6.1 基本表格#

\begin{table}[h]
\centering
\caption{表格标题}
\begin{tabular}{|l|c|r|}
\hline
左对齐 & 居中 & 右对齐 \\
\hline
数据1 & 数据2 & 数据3 \\
数据4 & 数据5 & 数据6 \\
\hline
\end{tabular}
\end{table}

6.2 列格式说明#

格式说明
l左对齐
c居中
r右对齐
``
`

6.3 复杂表格#

\begin{table}[h]
\centering
\caption{学生成绩表}
\begin{tabular}{|l|c|c|c|}
\hline
\textbf{姓名} & \textbf{语文} & \textbf{数学} & \textbf{英语} \\
\hline
张三 & 85 & 92 & 78 \\
李四 & 90 & 88 & 95 \\
王五 & 78 & 95 & 82 \\
\hline
\end{tabular}
\end{table}

6.4 合并单元格#

% 合并列
\begin{tabular}{|l|c|c|}
\hline
\multicolumn{2}{|c|}{合并两列} & 单独列 \\
\hline
A & B & C \\
\hline
\end{tabular}
% 合并行(需要multirow宏包)
\begin{tabular}{|l|c|}
\hline
\multirow{2}{*}{合并两行} & 第一行 \\
& 第二行 \\
\hline
\end{tabular}

6.5 跨页长表格#

\usepackage{longtable}
\begin{longtable}{|l|c|r|}
\caption{长表格标题} \\
\hline
\textbf{列1} & \textbf{列2} & \textbf{列3} \\
\hline
\endfirsthead
\hline
\textbf{列1} & \textbf{列2} & \textbf{列3} \\
\hline
\endhead
\hline
\endfoot
数据1 & 数据2 & 数据3 \\
数据4 & 数据5 & 数据6 \\
...更多数据...
\end{longtable}

七、图片插入#

7.1 基本图片插入#

\usepackage{graphicx}
\begin{figure}[h]
\centering
\includegraphics[width=0.8\textwidth]{image.png}
\caption{图片标题}
\label{fig:myimage}
\end{figure}

7.2 图片选项#

选项说明
width宽度
height高度
scale缩放比例
angle旋转角度
% 设置宽度
\includegraphics[width=5cm]{image.png}
% 设置高度
\includegraphics[height=3cm]{image.png}
% 缩放
\includegraphics[scale=0.5]{image.png}
% 旋转
\includegraphics[angle=45]{image.png}
% 组合
\includegraphics[width=0.8\textwidth, angle=0]{image.png}

7.3 浮动体位置#

选项说明
hhere,当前位置
ttop,页面顶部
bbottom,页面底部
ppage,单独一页
!忽略内部参数
\begin{figure}[htbp]
% 尝试放在当前位置,不行就放顶部、底部、单独页
\end{figure}

7.4 并排图片#

\begin{figure}[h]
\centering
\begin{minipage}{0.45\textwidth}
\centering
\includegraphics[width=\textwidth]{image1.png}
\caption{图片1}
\end{minipage}
\hfill
\begin{minipage}{0.45\textwidth}
\centering
\includegraphics[width=\textwidth]{image2.png}
\caption{图片2}
\end{minipage}
\end{figure}

八、引用与参考文献#

8.1 交叉引用#

% 标签
\label{标签名}
% 引用
\ref{标签名} % 引用编号
\pageref{标签名} % 引用页码
% 示例
如图\ref{fig:myimage}所示...
如公式\ref{eq:einstein}所示...
见第\pageref{sec:intro}页...

8.2 脚注#

这是正文内容\footnote{这是脚注内容}。

8.3 参考文献(BibTeX)#

% 在导言区
\bibliographystyle{plain}
\bibliography{references}
% 在正文中引用
\cite{引用键}
% references.bib 文件
@article{einstein1905,
author = {Einstein, Albert},
title = {On the Electrodynamics of Moving Bodies},
journal = {Annalen der Physik},
year = {1905},
volume = {322},
pages = {891--921}
}

8.4 natbib宏包#

\usepackage{natbib}
% 引用格式
\citet{einstein1905} % Einstein (1905)
\citep{einstein1905} % (Einstein, 1905)
\citep[see][]{einstein1905} % (see Einstein, 1905)
\citep[p.~10]{einstein1905} % (Einstein, 1905, p. 10)

九、代码排版#

9.1 行内代码#

使用\texttt{print()}函数输出。

9.2 代码块(listings宏包)#

\usepackage{listings}
\usepackage{xcolor}
\lstset{
language=Python,
basicstyle=\ttfamily\small,
keywordstyle=\color{blue}\bfseries,
commentstyle=\color{green!60!black},
stringstyle=\color{red},
numbers=left,
numberstyle=\tiny\color{gray},
frame=single,
breaklines=true,
captionpos=b
}
\begin{lstlisting}[caption={Python示例}]
def hello():
print("Hello, LaTeX!")
hello()
\end{lstlisting}

9.3 minted宏包(代码高亮)#

\usepackage{minted}
\begin{minted}{python}
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
\end{minted}

十、高级功能#

10.1 自定义命令#

% 简单命令
\newcommand{\R}{\mathbb{R}} % 实数集
\newcommand{\N}{\mathbb{N}} % 自然数集
\newcommand{\norm}[1]{\|#1\|} % 范数
% 带默认参数
\newcommand{\person}[2]{#1 #2} % 姓名
\person{张}{三} % 输出:张三

10.2 自定义环境#

% 定义定理环境
\newtheorem{theorem}{定理}
\newtheorem{lemma}[theorem]{引理}
\newtheorem{corollary}[theorem]{推论}
% 使用
\begin{theorem}
这是定理内容。
\end{theorem}

10.3 条件编译#

\newif\ifdraft
\drafttrue % 启用草稿模式
% \draftfalse % 关闭草稿模式
\ifdraft
草稿模式:显示草稿标记
\else
正式模式:隐藏草稿标记
\fi

10.4 循环#

\usepackage{pgffor}
\foreach \x in {1,2,...,10} {
数字:\x
}

十一、常用宏包#

11.1 文档类相关#

宏包用途
geometry页面设置
fancyhdr页眉页脚
titlesec章节标题格式
tocloft目录格式

11.2 数学相关#

宏包用途
amsmath数学公式增强
amssymb数学符号
mathtools数学工具
bm粗体数学符号

11.3 图表相关#

宏包用途
graphicx图片插入
booktabs专业表格
multirow表格合并行
longtable长表格
subcaption子图

11.4 代码相关#

宏包用途
listings代码排版
minted代码高亮
algorithm2e算法排版

11.5 中文支持#

宏包用途
ctex中文支持(推荐)
xeCJKXeLaTeX中文
CJK传统中文支持

十二、实战技巧#

12.1 学术论文模板#

\documentclass[12pt,a4paper]{article}
\usepackage[UTF8]{ctex}
\usepackage{geometry}
\usepackage{amsmath,amssymb}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{hyperref}
\geometry{left=2.5cm, right=2.5cm, top=2.5cm, bottom=2.5cm}
\title{论文标题}
\author{作者姓名}
\date{\today}
\begin{document}
\maketitle
\begin{abstract}
这是摘要内容。
\end{abstract}
\tableofcontents
\newpage
\section{引言}
正文内容...
\section{方法}
\subsection{数据收集}
具体内容...
\section{结论}
结论内容...
\bibliographystyle{plain}
\bibliography{references}
\end{document}

12.2 数学笔记模板#

\documentclass[12pt]{article}
\usepackage[UTF8]{ctex}
\usepackage{amsmath,amssymb,mathtools}
\usepackage{geometry}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{数学笔记}
\fancyhead[R]{\thepage}
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\norm}[1]{\|#1\|}
\begin{document}
\section{线性代数}
\subsection{向量空间}
\begin{definition}
向量空间是一个集合 $V$,配备加法和标量乘法运算,满足以下公理...
\end{definition}
\begin{theorem}
$V$ 是有限维向量空间,则 $V$ 的任意两个基具有相同的基数。
\end{theorem}
\begin{proof}
$B_1$$B_2$$V$ 的两个基。由于 $B_1$ 线性无关且 $B_2$ 生成 $V$,故 $|B_1| \leq |B_2|$。同理 $|B_2| \leq |B_1|$,因此 $|B_1| = |B_2|$
\end{proof}
\end{document}

12.3 Beamer演示文稿#

\documentclass{beamer}
\usetheme{Madrid}
\usepackage[UTF8]{ctex}
\title{演示文稿标题}
\author{演讲者}
\date{\today}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}{大纲}
\tableofcontents
\end{frame}
\section{第一部分}
\begin{frame}{第一页}
\begin{itemize}
\item 要点一
\item 要点二
\item 要点三
\end{itemize}
\end{frame}
\begin{frame}{公式示例}
\begin{equation}
E = mc^2
\end{equation}
\end{frame}
\end{document}

12.4 常见问题解决#

问题1:中文显示问题#

% 使用ctex宏包
\usepackage[UTF8]{ctex}
% 或者使用xeCJK
\usepackage{xeCJK}
\setCJKmainfont{SimSun} % 设置中文字体

问题2:图片找不到#

% 设置图片路径
\graphicspath{{images/}{figures/}}

问题3:公式太长#

% 使用multiline环境
\begin{multline}
f(x) = a + b + c + d + e + f + g + h + i + j \\
+ k + l + m + n + o + p + q + r + s + t
\end{multline}

问题4:表格太宽#

% 使用resizebox
\resizebox{\textwidth}{!}{
\begin{tabular}{...}
...
\end{tabular}
}

📝 总结#

核心要点#

内容要点
文档结构documentclass → 导言区 → document环境
数学公式行内行内、[行间]、equation环境
表格tabular环境、booktabs宏包
图片graphicx宏包、figure环境
引用label和ref配合使用
中文支持ctex宏包

学习建议#

  1. 从模板开始:先用模板,再逐步修改
  2. 多写多练:LaTeX需要实践才能熟练
  3. 善用搜索:遇到问题先搜索解决方案
  4. 参考文档:查阅LaTeX官方文档和宏包文档

推荐资源#


LaTeX让排版变得简单而专业!

分享

如果这篇文章对你有帮助,欢迎分享给更多人!

LaTeX语法完全指南:从入门到精通
https://emilia520.icu/posts/latex语法完全指南/
作者
火花花
发布于
2026-06-05
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

目录