环境
LaTeX 环境是文档结构的重要组成部分。本章介绍各种常用环境的使用方法。
列表环境
无序列表
\begin{itemize}
\item 第一项
\item 第二项
\item 第三项
\end{itemize}
有序列表
\begin{enumerate}
\item 第一项
\item 第二项
\item 第三项
\end{enumerate}
描述列表
\begin{description}
\item[术语1] 术语1的解释
\item[术语2] 术语2的解释
\end{description}
自定义列表标记
\usepackage{enumitem}
\begin{itemize}[label=$\star$]
\item 第一项
\item 第二项
\end{itemize}
\begin{enumerate}[label=\Roman*.]
\item 第一项
\item 第二项
\end{enumerate}
引用环境
quote 环境
用于短引用:
\begin{quote}
这是一段短引用。
\end{quote}
quotation 环境
用于长引用,支持多段落:
\begin{quotation}
这是一段长引用。
可以包含多个段落。
这是第二个段落。
\end{quotation}
verse 环境
用于诗歌:
\begin{verse}
第一行诗句\\
第二行诗句\\
第三行诗句
\end{verse}
代码环境
verbatim 环境
原样输出,不处理命令:
\begin{verbatim}
\documentclass{article}
\begin{document}
Hello, LaTeX!
\end{document}
\end{verbatim}
verbatim* 环境
显示空格:
\begin{verbatim*}
Hello World
\end{verbatim*}
listings 宏包
代码高亮:
\usepackage{listings}
\lstset{
basicstyle=\ttfamily,
keywordstyle=\color{blue},
commentstyle=\color{green!60!black},
stringstyle=\color{red},
numbers=left,
numberstyle=\tiny,
frame=single,
breaklines=true
}
\begin{lstlisting}[language=Python]
def hello():
print("Hello, World!")
\end{lstlisting}
minted 宏包
更强大的代码高亮(需要 Python Pygments):
\usepackage{minted}
\begin{minted}{python}
def hello():
print("Hello, World!")
\end{minted}
数学环境
行内数学
$E = mc^2$
\(E = mc^2\)
行间数学
\[E = mc^2\]
\begin{equation}
E = mc^2
\end{equation}
多行公式
\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
\end{multline}
分段函数
\[
f(x) = \begin{cases}
x^2, & x \geq 0 \\
-x^2, & x < 0
\end{cases}
\]
表格环境
tabular 环境
\begin{tabular}{|c|c|c|}
\hline
列1 & 列2 & 列3 \\
\hline
数据1 & 数据2 & 数据3 \\
\hline
\end{tabular}
table 环境
浮动表格:
\begin{table}[htbp]
\centering
\caption{表格标题}
\label{tab:example}
\begin{tabular}{|c|c|}
\hline
列1 & 列2 \\
\hline
\end{tabular}
\end{table}
tabularx 环境
自动调整列宽:
\usepackage{tabularx}
\begin{tabularx}{\textwidth}{|X|c|c|}
\hline
自动宽度 & 固定宽度 & 固定宽度 \\
\hline
\end{tabularx}
longtable 环境
跨页表格:
\usepackage{longtable}
\begin{longtable}{|c|c|}
\hline
列1 & 列2 \\
\hline
\endfirsthead
\hline
列1 & 列2 \\
\hline
\endhead
\hline
\endfoot
数据1 & 数据2 \\
% 更多数据...
\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}
对齐环境
center 环境
\begin{center}
居中的内容
\end{center}
flushleft 环境
\begin{flushleft}
左对齐的内容
\end{flushleft}
flushright 环境
\begin{flushright}
右对齐的内容
\end{flushright}
摘要环境
\begin{abstract}
这是摘要内容。
摘要通常在标题之后,正文之前。
\end{abstract}
定理环境
定义定理环境
\newtheorem{theorem}{定理}[section]
\newtheorem{lemma}[theorem]{引理}
\newtheorem{corollary}[theorem]{推论}
\newtheorem{definition}{定义}[section]
\newtheorem{example}{例}[section]
\newtheorem{remark}{注}
使用定理环境
\begin{theorem}[勾股定理]
直角三角形的两条直角边的平方和等于斜边的平方。
\end{theorem}
\begin{proof}
证明内容。
\end{proof}
\begin{definition}
定义内容。
\end{definition}
盒子环境
mbox 和 fbox
\mbox{不换行的盒子}
\fbox{带边框的盒子}
parbox 环境
\parbox{5cm}{这是一个段落盒子,可以包含多行文字。}
minipage 环境
\begin{minipage}{0.5\textwidth}
这是一个小页面环境。
可以包含段落、列表等。
\end{minipage}
浮动体环境
figure 和 table
\begin{figure}[htbp]
% 图片内容
\end{figure}
\begin{table}[htbp]
% 表格内容
\end{table}
位置选项
| 选项 | 含义 |
|---|---|
| h | 当前位置 |
| t | 页面顶部 |
| b | 页面底部 |
| p | 单独一页 |
| ! | 强制忽略限制 |
| H | 精确位置(需要 float 宏包) |
自定义环境
定义新环境
\newenvironment{myenv}
{\begin{quote}\itshape} % 开始代码
{\end{quote}} % 结束代码
\begin{myenv}
这是自定义环境的内容。
\end{myenv}
带参数的环境
\newenvironment{important}[1]
{\begin{quote}\textbf{#1}\par}
{\end{quote}}
\begin{important}{重要提示}
这是重要内容。
\end{important}
小结
本章介绍了 LaTeX 环境:
- 列表环境:itemize、enumerate、description
- 引用环境:quote、quotation、verse
- 代码环境:verbatim、listings、minted
- 数学环境:equation、align、gather
- 表格环境:tabular、table、longtable
- 图片环境:figure、minipage
- 对齐环境:center、flushleft、flushright
- 定理环境:theorem、lemma、definition
- 自定义环境:newenvironment
掌握各种环境的使用是创建结构化文档的基础。下一章将介绍高级特性。