LaTeX入门笔记
1. LaTeX文档基本结构
以document环境为界,document环境前是导言部分(preamble);环境内部是正文部分;环境之后的部分被忽略。在导言区进行格式设置,正文部分套用格式。
%%% 简单文档
% 导言:格式设置
\documentclass{ctexart}
\usepackage[b5paper]{geometry}
% 正文:填写内容
\begin{document}
使用 \LaTeX
\end{document}
2. 文档部件
- 标题:
\title, \author, \date --- \maketitle
- 摘要/引言:
abstract 环境, \chapter*
- 目录:
\tableofcontents
- 章节:
\chapter, \section
- 附录:
\appendix, \chapter, \section
- 文献:
\bibliography
- 索引:
\printindex
3. 文档划分
-
大型文档
- 标题、前言:
\frontmatter
- 主体:
\mainmatter
- 附录、参考文献:
\backmatter
- 标题、前言:
-
一般文档
- 仅划分为正文和附录:
\appendix
- 仅划分为正文和附录:
章节层次结构
4. 磁盘文件组织
-
文件目录结构
- 主文档,给出文档框架结构
- 按内容章节划分不同的文件
- 使用单独的类文件和格式文件设置格式
- 用小文件隔离复杂的图表
-
相关命令
-
\documentclass
:读入文档类文件(.cls) -
\usepackage
:读入一个格式文件,即宏包(.sty) -
\include
:分页,并读入章节文件(.tex) -
\input
:读入任意文件
-
文档框架示例
5. 填写文档
5.1 LaTeX基础
5.1.1 Hello World
英文版:
\documentclass{article}
\begin{document}
Hello World!
\end{document}
中文版:
\documentclass{ctexart}
\begin{document}
你好呀!
\end{document}
编译选项:
5.1.2 语法结构
-
命令
参数用花括号表示,可选参数用中括号表示
\cmd{arg1}{arg2}\\ \cmd[opt]{arg1}{arg2}
如:LaTeX的分数 1 2 \frac{1}{2} 21表示为:
\frac{1}{2}
-
环境
\begin{env} ... \end{env}
如:LaTeX的矩阵表示为:
\begin{matrix} ... \\ ... \end{matrix}
-
注释
以
%
开头
5.1.3 LaTeX宏
-
命令
命令通常以
\
开头,可以带零到多个参数。命令也可以是直接输出某种结果;也可以改变一个状态,此时LaTeX用{}
分组或环境作为状态改变的作用域。如:强调某些文字:
\em abc
或\emph{abc}
-
环境
格式:
\begin{env} 环境的内容 \end{env}
如:右对齐
\begin{flushright} 文字 \end{flushright}
5.2 正文文本
-
文本
用空格分开单词,分段是空一行
-
符号
-
一些符号被LaTeX宏语言所占用,需要以命令形式输入
\# \$ \% \& \{ \} \textbackslash
-
键盘上没有的符号用命令输入
\S \dag \ddag \P \copyright \textbullet \textregistered \texttrademark \pounds
-
5.3 公式
5.3.1 数学公式
-
数学模式
数学模式下,字体、符号、间距都与正文不同,一切数学公式(包括单个符号 n n n)都要在数学模式下输入。
-
行内(inline)公式
使用一对
$ $
来标示,如:$a+b=c$
-
显示(display)公式
- 简单的不编号公式用
\[ \]
标示 - 基本的编号公式用equation环境
- 更复杂的结构,使用amsmath宏包提供的专门的数学环境
- 简单的不编号公式用
-
-
数学结构
- 上标与下标:用
^
和_
表示 - 上下划线与花括号:
\overline, \underline, \overbrace, \underbrace
- 分式:
\frac{分子}{分母}
- 根式:
\sqrt[次数]{根号下}
- 矩阵:使用amsmath宏包提供的专门的矩阵环境matrix, pmatrix, bmatrix等。特别复杂的矩阵(如带线条)使用array环境作为表格画出
- 上标与下标:用
-
数学符号
- 数学字母
a
,
b
,
α
,
Δ
a, b, \alpha, \Delta
a,b,α,Δ,数学字体
B
\mathbb{B}
B:
\mathbb
, B \mathcal{B} B:\mathcal
等 - 普通符号:
∞
\infty
∞:
\infty
, ∠ \angle ∠:\angle
- 二元运算符: a + b , a − b , a ⨁ b a + b, a - b, a \bigoplus b a+b,a−b,a⨁b
- 二元关系符: a = b , a ≤ b a = b, a \leq b a=b,a≤b
- 括号:
⟨
a
,
b
⟩
\langle a,b \rangle
⟨a,b⟩,使用
\left, \right
放大 - 标点:逗号,分号:
\colon
- 数学字母
a
,
b
,
α
,
Δ
a, b, \alpha, \Delta
a,b,α,Δ,数学字体
B
\mathbb{B}
B:
-
数学工具包
常用:amsmath 与 mathtools
例:
\documentclass{article} \usepackage{amsmath} \usepackage{mathtools} \begin{document} \begin{align*} 2^5 &= (1+1)^5 \\ &= \begin{multlined}[t] \binom50\cdot 1^5 + \binom51\cdot 1^4 \cdot 1 + \binom52\cdot 1^3 \cdot 1^2 \\ + \binom53\cdot 1^2 \cdot 1^3 + \binom54\cdot 1 \cdot 1^4 + \binom55\cdot 1^5 \end{multlined} \\ &= \binom50 + \binom51 + \binom52 + \binom53 + \binom54 + \binom55 \end{align*} \end{document}
5.3.2 科技功能
-
单位
使用宏包 siunitx
\num{-1.235e96} \SI{299792458}{m/s} \SI{2x7x3.5}{m} % ???
\begin{tabular}{|S|}\hline -234532\\ 13.55 \\ .9e37km \\ \hline \end{tabular}
-
化学式
使用宏包chemformula
\documentclass{article} \usepackage{chemformula} \begin{document} \ch{2 H2 + O2 -> 2 H2O} \\ \ch{2 H2O -> 2 H2 ^ + O2 ^} \end{document}
5.4 列表与文本块
5.4.1 列表环境
-
有编号列表(enumerate)
-
不编号列表(itemize)
-
有标题列表(description)
\documentclass{article}
\begin{document}
\begin{enumerate}
\item AAA
\item BBB
\item CCC
\end{enumerate}
\end{document}
5.4.2 定理类环境
定义定理类环境:\newtheorem{thm}{定理}[section]
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{proof}{Proof}[section]
\section{theorem}
\begin{theorem}
This is a theorem.
\end{theorem}
\begin{lemma}
This is a lemma.
\end{lemma}
\section{Proof}
\begin{proof}
This is a proof.
\end{proof}
\end{document}
5.4.3 诗歌与引文
-
verse
-
quote
-
quotation
5.4.4 抄录代码
- \verb命令
- verbatim环境
\documentclass{article}
\begin{document}
\verb|#include <stdio.h>|
\begin{verbatim}
#include <stdio.h>
void main() {
printf("Hello World!");
}
\end{verbatim}
\end{document}
- 高级代码:语法高亮
- 使用listings宏包
- 使用minted宏包
\documentclass{article}
\usepackage{listings}
\usepackage{color, xcolor}
\begin{document}
\begin{lstlisting}[language=C, basicstyle=\ttfamily, stringstyle=\color{blue}, showstringspaces=false]
#include <stdio.h>
void main() {
printf("Hello World!");
}
\end{lstlisting}
\end{document}
5.4.5 算法结构
- 使用clrscode宏包(算法导论)
- 使用algorithm2e宏包
- 使用algorithmicx宏包的algpseudocode格式
\documentclass{article}
\usepackage{clrscode}
\begin{document}
\begin{codebox}
\Procname{$\proc{Merge-Sort}(A,p,r)$}
\li \If $p<r$
\li \Then $q \gets \lfloor(p+r)/2\rfloor$
\li $\proc{Merge-Sort}(A,p,q)$
\li $\proc{Merge-Sort}(A,q+1,r)$
\li $\proc{Merge-Sort}(A,p,q,r)$
\End
\end{codebox}
\end{document}
5.5 图表和浮动环境
5.5.1 画表格
使用tabular环境;此外,还可以使用一些工具生成表格代码。
\documentclass{article}
\begin{document}
\begin{tabular}{|rr|}
\hline
input& output \\ \hline
$-2$ & 4 \\
0 & 0 \\
2 & 4 \\ \hline
\end{tabular}
\end{document}
功能各异的表格宏包
- 单元格处理:multirow,makecell
- 长表格:longtable, xtab
- 定宽表格:xtabular
- 表线控制:booktabs, diagbox, arydshln
- 表列格式:array
- 综合应用:tabu
5.5.2 插图
使用graphicx宏包提供的 \includegraphics
命令
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics[width=2cm]{pkulogo.png}
\end{document}
5.5.3 代码画图
优先使用外部工具画图,特别是可视化工具,例如一般的矢量图用:lnkscape, Illustrator, PowerPoint(保存为PDF格式),数学图形用:MATLAB, matplotlib等。
某些特定类型的图形也可以用LaTeX代码作图,可使用TikZ绘图宏包。
5.5.4 浮动体
- figure环境
- table环境
- 其他环境可以使用float宏包得到
浮动体的标题用\caption
命令得到,会自动编号。
\documentclass{article}
\begin{document}
\begin{table}\centering
\caption{title}
\begin{tabular}{|ccccc|}\hline
A&B&C&D&E\\
A&B&C&D&E\\
A&B&C&D&E\\
A&B&C&D&E\\
A&B&C&D&E\\\hline
\end{tabular}
\end{table}
\end{document}
5.6 自动化工具
5.6.1 目录
LaTeX自动生成章节目录命令:\tableofcontents
工作原理:
▲注意:至少要编译两次
5.6.2 交叉引用
LaTeX交叉引用命令:\label{name}, \ref{name}, \pageref{name}
工作原理:
5.6.3 PDF的链接与书签
使用hyperref宏包,hyperref会在PDF中写入相应的”锚点“代码,在其他地方引用;PDF书签会产生单独的.out文件。
5.6.4 BibTex参考文献
引用方法:
-
借助JabRef工具管理文献数据库*(Google Scholar可直接导出BibTex格式)*
-
引用方式:
\cite{}, \nocite{}
-
先执行BibTex文件,再执行pdfTex文件
设置文献格式
- 选用合适的.bst格式,比如plainnat, gbt7714-plain
- natbib与作者-年格式
- 利用custom-bib产生定制的格式文件
- bibLaTeX+Biber:文献处理的新方式
工作原理:
6. 设计文档格式
6.1 基本原则
1. 格式与内容分离,不要在意细节
标准的LaTeX文档类具有相对固定的排版格式,作者编写文档只需使用 \title, \section, abstract
这样的命令或环境,而不必考虑具体实现。有关格式的细节代码,则被封装在文档类、宏包中,或在导言区分离编写。
2. 使用内容相关的命令与环境
作者在填写内容使要遵循分离原则,基本的方法是只使用与内容相关的命令和环境。
% 推荐
It is \emph{important}.
% 不推荐
It is \textit{important}.
% 推荐
\caption{流程图}
% 不推荐
\textbf{图 1:} 流程图
6.2 使用宏包
-
作用
宏包相当于其他程序语言中的“库”,将可重用的代码提取出来。使用宏包可以用简单的接口实现非常复杂的功能。
例如:画一棵二叉树
\documentclass{article} \usepackage{forest} \begin{document} \begin{forest} [A [B [D] [E]] [C [F] [G]]] \end{forest} \end{document}
-
问题
第三方宏包可能破坏TeX设计的“向前兼容性”;不同宏包之间可能会出现兼容性问题。
如何合理使用宏包:
- 尽量不造轮子
- 尽量排除不需要的宏包
-
查看宏包帮助文档
win+R => 输入
cmd
=> 输入texdoc 宏包名
6.3 格式控制功能
6.3.1 字体字号
-
字体
- 罗马字体:
\rmfamily, \textrm{...}
- 无衬线字体:
\sffamily, \textsf{...}
- 打字机字体:
\ttfamily, \texttt{...}
- 罗马字体:
-
字号
-
英文
\Huge, \LARGE, \Large, \large, \normalsize, \small, \footnotesize, \scriptsize, \tiny
-
中文
\zihao{5}, \zihao{-3}
-
6.3.2 对齐
-
默认:两端对齐
-
居中:
\centering
-
左对齐:
raggedleft
-
右对齐:
\raggedright
6.3.3 空白间距
-
水平:
\hspace{2cm}
-
垂直:
\vspace{3mm}
6.3.4 版面布局
- 设计纸张:geometry宏包
- 设计版面:fancyhdr宏包
6.3.5 分页断行
-
换行
\linebreak, \\
-
分页
\pagebreak, \newpage, \clearpage, \cleardoublepage
6.3.6 盒子
- 水平盒子:
\mbox{内容}
- 垂直盒子:
\parbox{4em}{盒子}, minipage
6.4 格式应用于文档
-
在导言区单独设置格式
- 直接设置相关参数,如:
\parindent, \parskip, \linespread, \pagestyle
- 修改部分命令定义,如:
\thesection, \labelenumi, \descriptionlabel, \figurename
- 利用工具宏包完成设置,如:使用ctex宏包设置中文格式,使用tocloft宏包设置目录格式。
- 直接设置相关参数,如:
-
利用自定义命令和环境
对于LaTeX没有直接提供的格式,可使用自定义的命令和环境实现语义的接口,为未来的修改和扩充提供条件。
▲注意:各种直接修改输出格式的命令,如字体、字号、对齐、间距的命令,都应该放在文档格式设置或自定义的命令、环境中,而避免在正文中直接使用。
\documentclass{ctexart} \newcommand\prg[1]{\textbf{\Huge #1}} \begin{document} 程序 \prg{sort} 很有用。 \end{document}
-
章节标题
- 中文:借助ctex宏包及文档类,使用
\ctexset
进行定制 - 西文:使用titlesec宏包
- 中文:借助ctex宏包及文档类,使用
-
浮动标题
使用caption宏包
-
列表环境
使用enumitem宏包
参考视频:刘海洋 · LaTeX不快速的入门
参考书籍:《LaTeX入门》by 刘海洋