03-LaTex入门

文章主要参考知乎Dylaaan的文章《【LaTeX】新手教程:从入门到日常使用》^[https://www.zhihu.com/tardis/zm/art/456055339?source_id=1005]

打开TeXstudio

01-文档的一些声明

01-01-基本部分

从最简单的部分开始

1
2
3
4
5
6
7
8
\documentclass[UTF8]{ctexart}


\begin{document}

Hello, world!

\end{document}

运行如下

01-01-01-\documentclass

其中第一行在声明文档类型

1
\documentclass[UTF8]{ctexart}

TeX有多种文档类型可选,不同的文件类型,编写的过程中也会有一定的差异,如果直接修改文件类型的话,甚至会报错。

简单来说,基础文档类型包括:

  • article:用于科技论文、短文等
  • report:适用于技术报告、学位论文等
  • book:专为书籍排版设计
  • beamer:用于制作演示文稿
  • letter:用于撰写正式信件
  • slides:用于创建幻灯片
  • ctexart:中文文章
  • ctexrep:中文报告
  • ctexbook:中文书籍

举例说明它们的区别:

  • article不支持chapter命令,而report和book支持
  • report和book默认双面打印,article默认单面
  • book支持frontmatter、mainmatter和backmatter命令
  • report和book有独立的标题页
  • ctex开头的中文文档类型自动处理中文排版中的字体设置、标点处理等问题

也可以在\documentclass处设置基本参数,比如说:

1
\documentclass[12pt, a4paper, oneside, UTF8]{ctexart}

设置默认字体大小为12pt,纸张大小为A4,单面打印,文章编码为UTF8

不同的设置之间用逗号隔开。

01-01-02-正文部分

正文部分处于document环境中,在document环境外的部分不会出现在文件中

1
2
3
4
5
6
7
8
\documentclass[12pt, a4paper, oneside, UTF8]{ctexart}


\begin{document}

Hello, world!

\end{document}

01-02-宏包

在document环境之前加载宏包,加载宏包的代码是\usepackage{}

宏包是LaTeX的功能扩展库,就像手机安装不同 App 来解锁不同功能。比如你想在文档里插入图片,就得装个图片处理 App(对应graphicx宏包);想写复杂数学公式,就需要数学公式编辑器(对应amsmath宏包)。

可以一次性导入全部宏,每个包之间用逗号分隔

1
\usepackage{mathtools,wallpaper}

也可以一行行导入,用到什么导什么

1
2
\usepackage{t1enc}
\usepackage{pagecolor}

加载宏包时还可以设置基本参数,如使用超链接宏包hyperref,可以设置引用的颜色为黑色等,代码如下:

1
\usepackage[bookmarks=true, colorlinks, citecolor=blue, linkcolor=black]{hyperref}

目前代码如下

1
2
3
4
5
6
7
8
9
10
11
\documentclass[12pt, a4paper, oneside, UTF8]{ctexart}

\usepackage{amsmath, amsthm, amssymb, graphicx, mathtools, wallpaper, t1enc, pagecolor}
\usepackage[bookmarks=true, colorlinks, citecolor=blue, linkcolor=black]{hyperref}


\begin{document}

Hello, world!

\end{document}

01-03-相关信息

标题可以用\title{}设置,作者可以用\author设置,日期可以用\date{}设置,这些都需要放在导言区。

为了在文档中显示标题信息,需要使用\maketitle。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
\documentclass[12pt, a4paper, oneside, UTF8]{ctexart}

\usepackage{amsmath, amsthm, amssymb, graphicx, mathtools, wallpaper, t1enc, pagecolor}
\usepackage[bookmarks=true, colorlinks, citecolor=blue, linkcolor=black]{hyperref}

% 导言区

\title{我的\LaTeX 文档}
\author{FangTian}
\date{\today}


\begin{document}

\maketitle

Hello, world!


\end{document}

02-正文

02-01-段落设置

正文直接在document环境中书写,文档默认进行首行缩进,无需加入空格来缩进。相邻的两行在编译时仍然会视为同一段。

使用一行相隔为另起一段。

1
2
我是第一段. 
我还是第一段.
1
2
3
我是第一段. 

我是第二段.

总代码如下:

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
\documentclass[12pt, a4paper, oneside, UTF8]{ctexart}

\usepackage{amsmath, amsthm, amssymb, graphicx, mathtools, wallpaper, t1enc, pagecolor}
\usepackage[bookmarks=true, colorlinks, citecolor=blue, linkcolor=black]{hyperref}

% 导言区

\title{我的\LaTeX 文档}
\author{FangTian}
\date{\today}


\begin{document}

\maketitle

Hello, world!

我是第一段.
我还是第一段.

---------------

我是第一段.

我是第二段.


\end{document}

运行结果如下

02-02-换页

另起一页的方式是:

1
\newpage

代码如下:

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
\documentclass[12pt, a4paper, oneside, UTF8]{ctexart}

\usepackage{amsmath, amsthm, amssymb, graphicx, mathtools, wallpaper, t1enc, pagecolor}
\usepackage[bookmarks=true, colorlinks, citecolor=blue, linkcolor=black]{hyperref}

% 导言区

\title{我的\LaTeX 文档}
\author{FangTian}
\date{\today}


\begin{document}

\maketitle

Hello, world!

%测试分段功能段落

我是第一段.
我还是第一段.

---------------

我是第一段.

我是第二段.
\newpage

%测试分页功能
我在第一页
\newpage
我在第二页

\end{document}

效果如下:

02-03-设置特殊字体

字体 命令
直立 \textup{}
意大利 \textit{}
倾斜 \textsl{}
小型大写 \textsc{}
加宽加粗 \textbf{}

代码如下

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
51
52
\documentclass[12pt, a4paper, oneside, UTF8]{ctexart}

\usepackage{amsmath, amsthm, amssymb, graphicx, mathtools, wallpaper, t1enc, pagecolor}
\usepackage[bookmarks=true, colorlinks, citecolor=blue, linkcolor=black]{hyperref}

% 导言区

\title{我的\LaTeX 文档}
\author{FangTian}
\date{\today}


\begin{document}

\maketitle

Hello, world!

%测试分段功能段落

我是第一段.
我还是第一段.

---------------

我是第一段.

我是第二段.
\newpage

%测试分页功能
我在第一页
\newpage
我在第二页

%测试特殊字体
\newpage
abcdefg 测试 我是默认字体

\textup{abcdefg 测试 我是直立字体}

\textit{abcdefg 测试 我是意大利字体}

\textsl{abcdefg 测试 我是倾斜字体}

\textsc{abcdefg 测试 我是小型大写字体}

ABCDEFG 测试 我是默认大写字体

\textbf{abcdefg 测试 我是加宽加粗字体}

\end{document}

效果如下

02-04-章节

对于ctexart文件类型,章节可以用\section{}\subsection{}命令来标记

1
2
3
4
5
6
7
8
9
10
11
\section{一级标题}

\subsection{二级标题}

\subsubsection{三级标题}

这里是正文.

\subsection{二级标题}

这里是正文.

最多到\subsubsection,没有\subsubsubsection

代码如下:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
\documentclass[12pt, a4paper, oneside, UTF8]{ctexart}

\usepackage{amsmath, amsthm, amssymb, graphicx, mathtools, wallpaper, t1enc, pagecolor}
\usepackage[bookmarks=true, colorlinks, citecolor=blue, linkcolor=black]{hyperref}

% 导言区

\title{我的\LaTeX 文档}
\author{FangTian}
\date{\today}


\begin{document}

\maketitle

Hello, world!

%测试分段功能段落

我是第一段.
我还是第一段.

---------------

我是第一段.

我是第二段.
\newpage

%测试分页功能
我在第一页
\newpage
我在第二页

%测试特殊字体
\newpage
abcdefg 测试 我是默认字体

\textup{abcdefg 测试 我是直立字体}

\textit{abcdefg 测试 我是意大利字体}

\textsl{abcdefg 测试 我是倾斜字体}

\textsc{abcdefg 测试 我是小型大写字体}

ABCDEFG 测试 我是默认大写字体

\textbf{abcdefg 测试 我是加宽加粗字体}

%测试章节
\newpage
\section{一级标题}

\subsection{二级标题}

\subsubsection{三级标题}

这里是正文.

\subsection{二级标题}

这里是正文.


\end{document}

效果如下

02-05-目录

在有了章节的结构之后,使用\tableofcontents命令就可以在指定位置生成目录。通常带有目录的文件需要编译两次,因为需要先在目录中生成.toc文件,再据此生成目录。

代码如下:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
\documentclass[12pt, a4paper, oneside, UTF8]{ctexart}

\usepackage{amsmath, amsthm, amssymb, graphicx, mathtools, wallpaper, t1enc, pagecolor}
\usepackage[bookmarks=true, colorlinks, citecolor=blue, linkcolor=black]{hyperref}

% 导言区

\title{我的\LaTeX 文档}
\author{FangTian}
\date{\today}


\begin{document}

\maketitle


Hello, world!

%测试分段功能段落

我是第一段.
我还是第一段.

---------------

我是第一段.

我是第二段.
\newpage

%测试分页功能
我在第一页
\newpage
我在第二页

%测试特殊字体
\newpage

abcdefg 测试 我是默认字体

\textup{abcdefg 测试 我是直立字体}

\textit{abcdefg 测试 我是意大利字体}

\textsl{abcdefg 测试 我是倾斜字体}

\textsc{abcdefg 测试 我是小型大写字体}

ABCDEFG 测试 我是默认大写字体

\textbf{abcdefg 测试 我是加宽加粗字体}

%测试章节
\newpage
\section{一级标题}

\subsection{二级标题}

\subsubsection{三级标题}

这里是正文.

\subsection{二级标题}

这里是正文.

%添加目录
\newpage
\tableofcontents


\end{document}

效果如下

更多部分可以查看从知乎Dylaaan的文章《【LaTeX】新手教程:从入门到日常使用》^[https://www.zhihu.com/tardis/zm/art/456055339?source_id=1005]


03-LaTex入门
https://pattianfang.github.io/2025/08/07/03-LaTex入门/
作者
Pat Tian Fang
发布于
2025年8月7日
更新于
2025年8月7日
许可协议