Latex笔记

$\LaTeX$是一种是一种基于$\TeX$的排版系统,使用$\LaTeX$,我们可以方便地排版出漂亮的文章,甚至书籍,本文记录了本人在学习和使用$\LaTeX$过程中涉及到的一些问题及其解决方案。

下载与安装

安装 texlive

我使用的环境是 Ubuntu, Linux 下基本可以用软件包管理器安装。

1
sudo apt-get install texlive-full

对于 Windows, 可以在官网下载相应的安装包进行安装,不再赘述。

选择一个合适的 $\LaTeX$ 编辑器

你当然可以使用诸如 TexStudio、Texmaker 等专门的工具来编写 $\LaTeX$, 这里我使用的是 VSCode + $\LaTeX$ 插件进行编辑。具体配置过程如下:

  • 首先,你要确保已经正确安装了 texlive.
  • 打开 VSCode, 在插件中搜索 $\LaTeX$ Workshop 进行安装。
  • 在 Setting 中添加配置代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    "latex-workshop.latex.tools": [
    {
    // 编译工具和命令
    "name": "xelatex",
    "command": "xelatex",
    "args": [
    "-synctex=1",
    "-interaction=nonstopmode",
    "-file-line-error",
    "-pdf",
    "%DOCFILE%"
    ]
    },
    {
    "name": "pdflatex",
    "command": "pdflatex",
    "args": [
    "-synctex=1",
    "-interaction=nonstopmode",
    "-file-line-error",
    "%DOCFILE%"
    ]
    },
    {
    "name": "bibtex",
    "command": "bibtex",
    "args": [
    "%DOCFILE%"
    ]
    }
    ],

    "latex-workshop.latex.recipes": [
    {
    "name": "xelatex",
    "tools": [
    "xelatex"
    ]
    },
    {
    "name": "xe->bib->xe->xe",
    "tools": [
    "xelatex",
    "bibtex",
    "xelatex",
    "xelatex"
    ]
    }
    ],
    "latex-workshop.latex.autoBuild.run": "never", // 关闭保存时自动编译

Hello World

下面将通过一个简单的例子来展现 $\LaTeX$ 源文件的构成。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\documentclass{article}       % 文档类

% ===========
% == 导言区 ==
% ===========

% 正文
\begin{document}
\section{Hello World} % 命令 section
Hello World

% 环境 数学公式
\begin{equation}
a = \sqrt{b^2}
\end{equation}
\end{document}

编译后的内容为

HelloWorld

命令

$\LaTeX$ 中命令以反斜线 \ 开头,为以下两种形式之一:

  • 反斜线和后面的一串字母,如上边例子中的 \section
  • 反斜线和后面的单个非字母符号,如 \$.

环境

$\LaTeX$ 中还包括环境,用以令一些效果在局部生效,或是生成特殊的文档元素。$\LaTeX$ 环境的用法为一对命令 \begin\end.
上述例子中的 documentequation 都是环境。

文档类

文档类规定了 $\LaTeX$ 源代码所要生成的文档的性质——普通文章、书籍、演示文稿、个人简历等等。$\LaTeX$ 源代码的开头须用 \documentclass 指定文档类:

1
\documentclass[⟨options⟩]{⟨class-name⟩}

其中 ⟨class-name⟩ 为文档类的名称,如 $\LaTeX$ 提供的 article, book, report,在其基础上派生的一些文档类如支持中文排版的 ctexart / ctexbook / ctexrep.
<options>是可传入的参数,如基本字号、纸张大小、编码方式等。

导言

\documentclass\begin{document} 之间的位置称为导言区。在导言区中一般会使用 \usepackage 调用宏包,以及会进行对文档的全局设置。

正文

document 环境当中的内容是文档正文。这部分内容是最终会呈现出来的内容。

如何获取帮助

没有哪一篇技术博客可以解决你在使用过程中遇到的所有问题,当你发现一个问题在百度上找不出你想要的答案时该怎么办呢,这时候就要求助于相应宏包的官方文档。
打开命令行,输入以下命令,便可以查看宏包文档。

1
texdoc <package-name>

<package-name>是宏包的名称,在下面的介绍中,我会把每个部分使用到的一些宏包贴出来供大家参考。

中文字体

文档类和编码

首先要使用支持中文的文档类,并指定编码为 UTF8.

1
\documentclass[UTF8]{ctexbook}

支持中文的文档类 ctexart / ctexbook / ctexrep 分别与英文中的 article, book, report 对应,可根据需求自行选择。

设置字体和字号

设置字体和字号下面的命令:

1
2
3
4
5
% 字体
\setmainfont{Times New Roman}
\setCJKmainfont{宋体}
% 字号
\zihao{-4}

其中 \setmainfont 设置的是英文的默认字体,\setCJKmainfont设置的是中文的默认字体。
\zihao 可以设置字号,后面的数字 4 对应四号字,-4 对应小四,以此类推。

中文粗体和斜体

在中文字体中,本身是不存在粗体和斜体的概念的,比如宋体,我们只能在系统中找到宋体字,却找不到宋体-粗体这一种字体。而 $\LaTeX$ 在使用粗体时,会在系统字体中寻找粗体字体,如果找不到最终会使用宋体的原始字体。这就使本应是粗体显示的部分(如标题)无法正常显示。
对于这一问题,有两种解决方案。

  • 使用有粗体和斜体的中文字体,这种字体很难找。。。
  • 用其他字体代替粗体和斜体,形如:
    1
    \setCJKmainfont[BoldFont={黑体}, ItalicFont={楷体}]{宋体}

各种距离设置

行距

1
\linespread{⟨factor⟩}

其中 ⟨factor⟩ 作用于基础行距而不是字号。缺省的基础行距是 1.2 倍字号大小,因此使用 \linespread{1.5} 意味着最终行距为 1.8 倍的字号大小。

首行缩进

以下长度分别为段落的左缩进、右缩进和首行缩进:

1
2
3
\setlength{\leftskip}{⟨length⟩}
\setlength{\rightskip}{⟨length⟩}
\setlength{\parindent}{⟨length⟩}

它们和设置行距的命令一样,在分段时生效。

1
2
\indent
\noindent

$\LaTeX$ 默认在段落开始时缩进,长度为用上述命令设置的 \parindent。如果你在某一段不想使用缩进,可使用某一段开头使用 \noindent 命令。相反地,\indent 命令强制开启一段首行缩进的段落。多个 \indent 命令可以累加缩进量。
$\LaTeX$ 还默认在 \chapter\section 等章节标题命令之后的第一段不缩进。如果不习惯这种设定,可以调用 indentfirst 宏包,令第一段的首行缩进照常。

页面边距

设置页边距要用到宏包 geometry

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

交叉引用

交叉引用由 \label\ref 两个命令构成,使用方式如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
\documentclass[UTF8]{ctexart}

\begin{document}

\section{你好世界}

\begin{equation}
a = \sqrt{b^2}
\label{math_1}
\end{equation}

这里引用公式\ref{math_1}

\end{document}

结果为

label_ref

这样做的好处是,当公式(或图片、定理等可以使用交叉引用的环境)编号改变时,你无需关心其引用需要修改。

注意,使用交叉引用时,需要进行两次编译。

改变引用文字字体(以链接形式显示)

1
\usepackage[colorlinks,linkcolor=red,anchorcolor=blue,citecolor=green]{hyperref}

列表

三种不同的列表

  • 有序列表 enumerate
  • 无序列表 itemize
  • 关键字列表 description

三种列表环境有不同的使用场景,并且支持相互嵌套,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
\documentclass[UTF8]{ctexart}

\begin{document}

\section{你好世界}

\begin{enumerate}
\item 有序列表
\begin{enumerate}
\item 第一项
\item 第二项
\item 第三项
\end{enumerate}
\item 无序列表
\begin{itemize}
\item 第一项
\item 第二项
\item 第三项
\end{itemize}
\item 关键字列表
\begin{description}
\item{一} 第一项
\item{二} 第二项
\item{三} 第三项
\end{description}
\end{enumerate}

\end{document}

enumerate

列表间距

$\LaTeX$ 默认的列表间距过大,可使用 enumitem 宏包进行调整

1
2
3
4
\usepackage{enumitem}
\setenumerate[1]{itemsep=0pt,partopsep=0pt,parsep=\parskip,topsep=5pt}
\setitemize[1]{itemsep=0pt,partopsep=0pt,parsep=\parskip,topsep=5pt}
\setdescription{itemsep=0pt,partopsep=0pt,parsep=\parskip,topsep=5pt}

数学公式

AMS宏集

AMS 宏集合是美国数学学会 (American Mathematical Society) 提供的对 $\LaTeX$ 原生的数学公式排版的扩展,其核心是 amsmath 宏包,对多行公式的排版提供了有力的支持。此外,amsfonts 宏包以及基于它的 amssymb 宏包提供了丰富的数学符号;amsthm 宏包扩展了 $\LaTeX$ 定理证明格式。

行内公式与行间公式

数学公式有两种排版方式:其一是与文字混排,称为行内公式;其二是单独列为一行排版,称为行间公式

  • 行内公式使用一对 $ 符号包裹
  • 行间公式由 equation 环境包裹

公式编号

  • equation 环境会为公式自动生成一个编号;
  • 这个编号可以用 \label\ref 生成交叉引用;
  • amsmath\eqref 命令甚至为引用自动加上圆括号;
  • \tag 命令可以手动修改公式的编号,\notag取消为公式编号;
  • 如果要直接生成不带编号的行间公式,可用 \[\]包裹,或者使用 equation* 环境。

下面是一个这些命令和环境的使用案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
\documentclass[UTF8]{ctexart}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}

\begin{document}

\section{你好世界}

首先,先来一个行内公式:$x^2+y^2 = 0$,它在文字中间

下面是一个行间公式:
\begin{equation}
x^2+y^2 = 0
\end{equation}

下面用 tag 命令修改编号为 aaa
\begin{equation}
x^2+y^2 = 0 \tag{aaa}
\end{equation}

下面的行间公式没有编号,使用了 notag 命令
\begin{equation}
x^2+y^2 = 0 \notag
\end{equation}

下面的行间公式没有编号
\[
x^2+y^2 = 0
\]

最后看一下自动编号的结果,发现 notag 和 直接使用无编号环境都不会计数。
\begin{equation}
x^2+y^2 = 0
\end{equation}

\end{document}

数学公式示例1

多行公式

使用 align 环境可以实现多行公式,它将公式用 & 隔为两部分并对齐,它会为每一行公式独立编号。
若要生成共用编号的多行公式,最方便的是用 equation 嵌套 aligned 环境的方式。
示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
\documentclass[UTF8]{ctexart}

\usepackage{amsmath}
\usepackage{amsfonts}

\begin{document}

\section{你好世界}

\subsection{align}
\begin{align}
a &= b+c+d \\
&= x+y+z
\end{align}

\subsection{equation嵌套aligned}
\begin{equation}
\begin{aligned}
a &= b+c+d \\
&= x+y+z
\end{aligned}
\end{equation}

\end{document}

结果如下

数学公式示例2

定理环境

$\LaTeX$ 原始定理环境

使用 $\LaTeX$ 排版数学和其他科技文档时,会接触到大量的定理、证明等内容。$\LaTeX$ 提供了一个基本的命令 \newtheorem 提供定理环境的定义:

1
2
\newtheorem{⟨theorem environment⟩}{⟨title⟩}[⟨section-level⟩]
\newtheorem{⟨theorem environment⟩}[⟨counter⟩]{⟨title⟩}
  • ⟨theorem environment⟩ 为定理环境的名称。原始的 $\LaTeX$ 里没有现成的定理环境,不加定义而直接使用很可能会出错。
  • ⟨title⟩ 是定理环境的标题(“定理”,“公理”等)。
  • ⟨section-level⟩⟨counter⟩ 用于指定定理的编号方式,二者不可同时使用
    • ⟨section level⟩ 为章节级别,如 chaptersection 等,定理序号成为章节的下一级序号;
    • ⟨counter⟩ 为用 \newcounter 自定义的计数器名称(在此不做详述,大多数情况下使用⟨section level⟩即可满足需求)

自定义定理环境样式

amsthm 提供了 \theoremstyle 命令支持定理格式的切换,在用 \newtheorem 命令定义定理环境之前使用。
amsthm 预定义了三种格式\theoremstyle:

  • plain 和 $\LaTeX$ 原始的格式一致;
  • definition 使用粗体标签、正体内容;
  • remark 使用斜体标签、正体内容。

另外 amsthm 还支持用带星号的 \newtheorem* 定义不带序号的定理环境:
更进一步的,还可以使用 \newtheoremstyle 命令自定义定理格式。

下面给出一个使用定理环境的示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
\documentclass[UTF8]{ctexart}

\usepackage{amsthm}

% 定理环境
\newtheoremstyle{mythesty}% name of the style to be used
{5pt}% measure of space to leave above the theorem. E.g.: 3pt
{5pt}% measure of space to leave below the theorem. E.g.: 3pt
{\it}% name of font to use in the body of the theorem
{}% measure of space to indent
{\bf}% name of head font
{\\}% punctuation between head and body
{ }% space after theorem head; " " = normal interword space
{}
\theoremstyle{mythesty}
\newtheorem{example}{例}[section]
\theoremstyle{plain}
\newtheorem{codelist}{代码清单}[section]

\begin{document}

\section{你好世界}

\begin{example}
这是一个例题
\end{example}

\section{你好世界2}

\begin{codelist}
这是一个代码清单
\begin{verbatim}
int main()
{
printf("Hello World\n");
return 0;
}
\end{verbatim}
\end{codelist}

\end{document}

结果为

定理环境

本文参考书籍 《一份(不太)简短的 $\LaTeX$ $2_\epsilon$介绍》
英文作者: Tobias Oetiker Hubert Partl, Irene Hyna and Elisabeth Schlegl
英文版本: Version 6.2, Februrary 28, 2018
中文翻译: CTEX 开发小组
中文版本: 版本 6.01,二零一九年四月