环境
环境是 LaTeX 中组织内容的重要方式。每个环境定义了特定的排版规则和行为,用于处理列表、引用、表格、数学公式等各种内容。理解环境的工作原理和使用方法,是掌握 LaTeX 的关键。
理解环境的概念
什么是环境?
环境是 LaTeX 中一种结构化的内容组织方式,使用 \begin{环境名} 和 \end{环境名} 界定范围。在环境内部,LaTeX 会应用特定的排版规则。
\begin{环境名}[可选参数]
内容
\end{环境名}
环境的特性
- 分组作用:每个环境自动创建一个分组,环境内的设置不影响环境外
- 嵌套能力:环境可以嵌套,但必须正确配对
- 可定制性:用户可以定义自己的环境
环境与命令的区别
命令通常用于局部格式控制(如 \textbf{}),而环境用于处理较大的内容块,可能涉及复杂的排版逻辑。
列表环境
无序列表(itemize)
itemize 环境创建无编号列表,每个项目前有标记符号:
\begin{itemize}
\item 第一项
\item 第二项
\item 第三项
\end{itemize}
工作原理:\item 命令开始一个新项目,自动添加标记并处理缩进。LaTeX 默认提供 4 级嵌套,每级使用不同的标记。
自定义标记:
% 单独设置
\begin{itemize}
\item[$\star$] 星号标记
\item[--] 短横线标记
\item 正常标记
\end{itemize}
% 全局设置
\renewcommand{\labelitemi}{$\bullet$}
\renewcommand{\labelitemii}{$\circ$}
有序列表(enumerate)
enumerate 环境创建编号列表:
\begin{enumerate}
\item 第一项
\item 第二项
\item 第三项
\end{enumerate}
编号格式:默认使用阿拉伯数字,嵌套时使用不同格式(数字、字母、罗马数字)。
自定义编号格式:
\renewcommand{\labelenumi}{\Roman{enumi}.} % I. II. III.
\renewcommand{\labelenumii}{\alph{enumii})} % a) b) c)
% 或使用 enumitem 宏包
\usepackage{enumitem}
\begin{enumerate}[label=\Roman*.]
\item 罗马数字编号
\end{enumerate}
指定起始编号:
\begin{enumerate}
\setcounter{enumi}{4} % 从 5 开始
\item 第五项
\item 第六项
\end{enumerate}
描述列表(description)
description 环境适合术语定义:
\begin{description}
\item[LaTeX] 一种文档排版系统
\item[TeX] 由 Donald Knuth 开发的排版引擎
\item[PDF] 便携式文档格式
\end{description}
自定义样式:
\usepackage{enumitem}
\begin{description}[
leftmargin=1cm,
style=nextline % 描述换行显示
]
\item[长术语名称] 描述内容会在下一行显示。
\end{description}
列表嵌套规则
列表可以嵌套,但必须正确配对:
\begin{enumerate}
\item 第一项
\begin{itemize}
\item 子项 A
\item 子项 B
\end{itemize}
\item 第二项
\end{enumerate}
高级列表定制
使用 enumitem 宏包实现精细控制:
\usepackage{enumitem}
% 定义自定义列表
\newlist{steps}{enumerate}{1}
\setlist[steps]{
label=Step \arabic*:,
leftmargin=*,
itemsep=0.5em
}
\begin{steps}
\item 准备环境
\item 编写代码
\item 运行测试
\end{steps}
引用环境
quote 环境
quote 环境用于短引用,特点是无首行缩进、左右缩进较小:
正如 Knuth 所说:
\begin{quote}
程序是为了被人读而写的,顺便让机器执行。
\end{quote}
适用场景:短小精悍的引用语、名人名言。
quotation 环境
quotation 环境用于长引用,支持多段落,首行有缩进:
\begin{quotation}
这是一段较长的引用内容。由于篇幅较长,
需要多个段落来表达完整的观点。
第二个段落会自动首行缩进,保持良好的排版效果。
这对于引用学术论文或书籍段落特别有用。
\end{quotation}
与 quote 的区别:
| 特性 | quote | quotation |
|---|---|---|
| 首行缩进 | 无 | 有 |
| 适用 | 短引用 | 长引用、多段落 |
| 段落间距 | 紧凑 | 较大 |
verse 环境
verse 环境用于诗歌或韵文,使用 \\ 手动换行:
\begin{verse}
白日依山尽,\\
黄河入海流。\\
欲穷千里目,\\
更上一层楼。
\end{verse}
排版特点:左右缩进,适合分行显示的诗句。
代码环境
verbatim 环境
verbatim 环境原样输出内容,不处理任何命令:
\begin{verbatim}
\documentclass{article}
\begin{document}
这里的 \textbf{命令} 不会被解释
\end{document}
\end{verbatim}
使用场景:展示 LaTeX 源代码、需要保留空格和换行的文本。
verbatim 变体*:显示空格为特殊符号
\begin{verbatim*}
这里 的 空格 可见
\end{verbatim*}
行内代码
使用 \verb 命令在行内显示代码:
使用 \verb|\textbf{粗体}| 命令创建粗体文本。
% 可以使用任意配对符号作为分隔符
\verb+不需要转义的特殊字符:$ & %+
\verb!同样有效!
listings 宏包
listings 宏包提供语法高亮功能:
\usepackage{listings}
\usepackage{xcolor}
\lstset{
basicstyle=\ttfamily\small,
keywordstyle=\color{blue}\bfseries,
commentstyle=\color{green!60!black}\itshape,
stringstyle=\color{red},
numbers=left,
numberstyle=\tiny\color{gray},
numbersep=5pt,
frame=single,
breaklines=true,
showstringspaces=false,
tabsize=4
}
\begin{lstlisting}[language=Python]
def fibonacci(n):
"""计算斐波那契数列"""
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
\end{lstlisting}
minted 宏包
minted 宏包基于 Python Pygments,提供更强大的语法高亮:
\usepackage{minted}
\begin{minted}[linenos, bgcolor=lightgray]{python}
def hello():
print("Hello, World!")
\end{minted}
注意:使用 minted 需要:
- 安装 Python 和 Pygments(
pip install Pygments) - 编译时添加
-shell-escape选项
数学环境
行内数学模式
公式 $E = mc^2$ 由爱因斯坦提出。
行间数学环境
基本行间公式:
\[ E = mc^2 \]
% 或使用 equation 环境(带编号)
\begin{equation}
E = mc^2
\label{eq:einstein}
\end{equation}
多行公式对齐:
\begin{align}
a &= b + c \label{eq:first}\\
&= d + e \label{eq:second}
\end{align}
% 不编号版本
\begin{align*}
a &= b + c \\
&= d + e
\end{align*}
无需对齐的多行公式:
\begin{gather}
a = b + c \\
d = e + f
\end{gather}
超长公式:
\begin{multline}
a + b + c + d + e + f + g + h \\
+ i + j + k + l + m + n
\end{multline}
分段函数
\[
f(x) = \begin{cases}
x^2, & x \geq 0 \\
-x^2, & x < 0
\end{cases}
\]
表格环境
tabular 环境
tabular 环境创建表格:
\begin{tabular}{|c|c|c|}
\hline
列1 & 列2 & 列3 \\
\hline
A & B & C \\
D & E & F \\
\hline
\end{tabular}
列格式说明:
| 格式 | 说明 |
|---|---|
l | 左对齐 |
c | 居中 |
r | 右对齐 |
p{宽度} | 指定宽度,自动换行 |
m{宽度} | 垂直居中的段落列 |
b{宽度} | 底部对齐的段落列 |
| ` | ` |
@{内容} | 自定义列间内容 |
booktabs 专业表格
使用 booktabs 宏包创建专业的三线表:
\usepackage{booktabs}
\begin{tabular}{lcc}
\toprule
方法 & 准确率 & 时间 \\
\midrule
方法 A & 0.95 & 1.2s \\
方法 B & 0.92 & 0.8s \\
方法 C & 0.98 & 2.1s \\
\bottomrule
\end{tabular}
booktabs 线条命令:
\toprule:顶部粗线\midrule:中间细线\bottomrule:底部粗线\cmidrule:部分横线
\begin{tabular}{lcccc}
\toprule
& \multicolumn{2}{c}{实验1} & \multicolumn{2}{c}{实验2} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
方法 & 精度 & 召回 & 精度 & 召回 \\
\midrule
A & 0.9 & 0.8 & 0.85 & 0.75 \\
\bottomrule
\end{tabular}
跨行跨列
\usepackage{multirow}
\begin{tabular}{|c|c|c|}
\hline
\multirow{2}{*}{合并两行} & 数据1 & 数据2 \\
& 数据3 & 数据4 \\
\hline
\multicolumn{2}{|c|}{合并两列} & 数据5 \\
\hline
\end{tabular}
浮动表格(table 环境)
table 环境是表格的浮动体容器:
\begin{table}[htbp]
\centering
\caption{表格标题}
\label{tab:example}
\begin{tabular}{lcc}
\toprule
项目 & 数值1 & 数值2 \\
\midrule
A & 100 & 200 \\
\bottomrule
\end{tabular}
\end{table}
位置选项:
| 选项 | 含义 |
|---|---|
h | 当前位置(here) |
t | 页面顶部(top) |
b | 页面底部(bottom) |
p | 单独一页 |
! | 忽略限制 |
长表格(longtable)
跨页表格使用 longtable:
\usepackage{longtable}
\begin{longtable}{|c|c|c|}
\hline
\multicolumn{3}{|c|}{表格标题} \\
\hline
列1 & 列2 & 列3 \\
\hline
\endfirsthead % 首页表头
\multicolumn{3}{c}{续表} \\
\hline
列1 & 列2 & 列3 \\
\hline
\endhead % 续页表头
\hline
\endfoot % 页脚
数据1 & 数据2 & 数据3 \\
% 更多数据...
\end{longtable}
图片环境
figure 浮动体
\begin{figure}[htbp]
\centering
\includegraphics[width=0.8\textwidth]{image.png}
\caption{图片标题}
\label{fig:example}
\end{figure}
并排图片
使用 minipage 实现并排:
\begin{figure}[htbp]
\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}
子图(subcaption)
\usepackage{subcaption}
\begin{figure}[htbp]
\centering
\begin{subfigure}[b]{0.45\textwidth}
\centering
\includegraphics[width=\textwidth]{image1.png}
\caption{子图A}
\label{fig:sub1}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.45\textwidth}
\centering
\includegraphics[width=\textwidth]{image2.png}
\caption{子图B}
\label{fig:sub2}
\end{subfigure}
\caption{总标题}
\label{fig:total}
\end{figure}
对齐环境
center 环境
\begin{center}
居中的内容
\end{center}
% 等价声明形式
{\centering 居中的内容\par}
flushleft 环境
\begin{flushleft}
左对齐的内容
\end{flushleft}
% 等价声明形式
{\raggedright 左对齐的内容\par}
flushright 环境
\begin{flushright}
右对齐的内容
\end{flushright}
% 等价声明形式
{\raggedleft 右对齐的内容\par}
其他常用环境
abstract 环境
\begin{abstract}
这是摘要内容。摘要通常位于标题之后、正文之前,
简要概括文章的主要内容和结论。
\end{abstract}
定理环境
% 定义定理环境
\usepackage{amsthm}
\newtheorem{theorem}{定理}[section]
\newtheorem{lemma}[theorem]{引理}
\newtheorem{corollary}[theorem]{推论}
\theoremstyle{definition}
\newtheorem{definition}{定义}[section]
\theoremstyle{remark}
\newtheorem*{remark}{注}
% 使用
\begin{theorem}[勾股定理]
直角三角形的两条直角边的平方和等于斜边的平方。
\end{theorem}
\begin{proof}
证明内容。
\end{proof}
盒子环境
minipage 环境:
\begin{minipage}[t]{0.5\textwidth}
左侧内容。minipage 像一个小页面,
可以包含段落、列表等复杂内容。
\end{minipage}%
\begin{minipage}[t]{0.5\textwidth}
右侧内容。位置选项:t(顶部)、c(居中)、b(底部)。
\end{minipage}
自定义环境
基本定义
\newenvironment{important}
{\begin{quote}\bfseries\itshape} % 开始代码
{\end{quote}} % 结束代码
\begin{important}
这是重要内容,会以粗斜体显示在引用环境中。
\end{important}
带参数的环境
\newenvironment{boxed}[1]
{\begin{center}\fbox{\begin{minipage}{#1}}
{\end{minipage}\end{center}}
\begin{boxed}{5cm}
这是一个带边框的盒子,宽度为 5cm。
\end{boxed}
带可选参数的环境
\newenvironment{highlight}[1][yellow]
{\begin{center}\colorbox{#1}}
{\end{center}}
\begin{highlight}
默认黄色背景
\end{highlight}
\begin{highlight}[red]
红色背景
\end{highlight}
重定义现有环境
% 重定义 quote 环境
\renewenvironment{quote}
{\begin{itshape}\begin{quote}}
{\end{quote}\end{itshape}}
环境嵌套规则
正确的嵌套
\begin{enumerate}
\item 外层项目
\begin{itemize}
\item 内层项目
\end{itemize}
\end{enumerate}
错误的嵌套
% 错误:交叉嵌套
\begin{enumerate}
\begin{itemize}
\end{enumerate}
\end{itemize}
% 错误:未闭合
\begin{enumerate}
\item 项目
% 缺少 \end{enumerate}
环境调试技巧
查找环境配对问题
当出现环境相关的错误时:
% 错误信息示例
! LaTeX Error: \begin{itemize} on input line 10 ended by \end{enumerate}.
% 解决方法:检查 \begin 和 \end 是否配对
使用注释定位问题
\begin{enumerate}
\item 项目
% \begin{itemize} % 注释掉怀疑有问题的部分
% \item 子项目
% \end{itemize}
\end{enumerate}
小结
本章介绍了 LaTeX 环境的核心知识:
- 环境概念:理解
\begin/\end的作用和分组特性 - 列表环境:itemize、enumerate、description 及其定制
- 引用环境:quote、quotation、verse 的使用场景
- 代码环境:verbatim、listings、minted 的语法高亮
- 数学环境:行内公式、行间公式、多行公式
- 表格环境:tabular、booktabs、longtable 的使用
- 图片环境:figure、minipage、subcaption
- 对齐环境:center、flushleft、flushright
- 自定义环境:newenvironment 的使用方法
掌握环境的使用是创建结构化文档的基础。下一章将介绍高级特性。