一、Latex文件结构

1.1 源文件基本结构

一个Latex文档分为导言区和正文区。

(1)导言区

导言区主要进行全局设置。

  • 使用\documentclass{}引入文档类,如book, article, report, letter类等,不同的类的格式不同,例如book和letter有封面,letter类没有title等。
  • 使用\title{}命令定义文档的标题
  • 使用\author{}命令定义文档的作者
  • 使用\date{}命令定义文档的创作时间
1
2
3
4
5
6
% 导言区
\documentclass{article} % book, report, letter

\title{My Latex Tutorial}
\author{Haofei Ma}
\date{\today} % \today指今天

(2)正文区

正文区撰写文档的内容。

  • 使用\begin{}\end{}输入一个环境,{}内是环境的名称,一个latex文件只能有一个ducument环境
  • 使用\maketitle输出标题
1
2
3
4
5
6
7
8
9
10
% 正文区  
\begin{document} % document为环境名称
\maketitile

% 内容(可以直接输入,空行代表段落)
Hello World!

Let $F(x)$ be defined by formula $$f(x)=3x^2-x+1$$.

\end{document}

1.2 中文处理方法

保证文档的编码为UTF-8格式,在导言区使用\usepackage{ctex}命令引入ctex宏包。

撰写中文时可以使用\heiti, \kaishu等命令指定字体为黑体、楷书等等。

1
2
3
4
5
6
7
8
9
%导言区  
\documentclass{article}

\title{\heiti Latex基础}%指定黑体字体
\author{\kaishu 马浩飞}%指定楷书字体
\date{\today}

\usepackage{ctex}%显示中文需要添加该指令

1.3 字体字号设置

字体字号的设置都是在正文区进行设置(因为字体设置仅针对某一段文字)。

(1)字体族设置

使用\textrm, \textsf, \textttt设置字体族。

或使用\rmfamily Roman Family声明后续的字体为罗马字体。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
%正文区
\begin{document}
%字体族设置(罗马字体、无衬线字体、打字机字体)

%\textrm等是字体命令,大括号里是作用到的文字
\textrm{Roman Family}
\textsf{Scan Serif Family}
\texttt{Typewriter Family}

%\rmfamily是字体声明,后面紧跟的文字是作用到的文字
{\rmfamily Roman Family}
{\sffamily Scan Serif Family}
{\ttfamily Typewriter Family}

\end{document}

括号可以限定字体声明的限定范围。

  • 如果加括号,代表设置括号内的字体为指定字体;
  • 如果无括号,代表后续所有段落字体为指定字体。

遇到另一个字体声明时,会结束当前字体声明,并启用新的字体声明。

(2)字体系列(粗细、宽度)

1
2
3
4
5
%字体系列设置(粗细、宽度)
\textmd{Medium Series}%\设置字体宽度,大括号里是作用到的文字
\textbf{Boldface Series}% \textbf可以对字体加粗
{\mdseries Medium Series}%\设置字体宽度,大括号里是作用到的文字
{\bfseries Boldface Series}%字体声明

(3)字体形状(直立、斜体、伪斜体、小型大写)

1
2
3
4
5
6
7
%字体形状(直立、斜体、伪斜体、小型大写)
\textup{Upright Shape} \textit{Italic Shape} %字体命令
\textsl{Slanted Shape} \textsc{Small Caps Shape}

{\upshape Upright Shape} {\itshape Italic Shape }%字体声明
{\slshape Slanted Shape}
{\scshape Small Caps Shape}

(4)中文字体设置

1
2
{\songti 宋体} \quad{heiti 黑体}\quad{\fangsong 仿宋}\quad {\kaishu 楷书}%\quad表示空格
中文字体的\textbf{粗体表现为黑体}与\textit{斜体表现为楷体}

(5)字体大小

1
2
3
4
5
6
7
8
9
{\tiny  Hello }\\
{\scriptsize Hello }\\
{\footnotesize Hello }\\
{\small Hello }\\
{\normalsize Hello }\\
{\large Hello }\\
{\Large Hello }\\
{\LARGE Hello }\\
{\huge Hello }\\

1.4 文档基本结构

Latex使用\section{}, \subsection{}, \subsubsection{}等命令构建文章的提纲。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
%正文区
\begin{document}
\maketitle %使得导言区的设置生效

\section{引言} % 1 引言

引言的内容第一段。

使用空行来实现分段,这里是引言内容的第二段。

\section{实验方法} % 2 实验方法
\section{实验过程} % 3 实验过程
\subsection{实验过程1} % 3.1 实验过程1
\subsection{实验过程2} % 3.2 实验过程2
\subsubsection{实验数据} % 3.2.1 实验数据
\subsubsection{实验图表} % 3.2.2 实验图表
\subsection{结果分析} % 3.3 结果分析
\section{结论} % 4 结论
\section{致谢} % 5 致谢

\end{document}

二、Latex中的特殊字符

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
%正文区
\begin{document}
\section{空白符号}
% 空行分段,多个空行等同1个
% 自动缩进,绝对不能使用空格代替
% 英文中多个空格处理为1个空格,中文中空格将被忽略
% 汉字与其它字符的间距会自动被处理
% 禁止使用中文全角空格

% 1em(当前字体中M的宽度)
a\quad b

% 2em
a\qquad b

% 1/6em
a\,b a\thinspace b

% 0.5em
a\enspace b

% 空格
a\ b

% 硬空格(不能分割的空格)
a~b

% 指定宽度的空格
a\kern 1em b
a\hspace{35pt}b

% 占位宽度({}中的字符宽度)
a\hphantom{xyz}b

% 弹性长度(撑满整个空间)
a\hfill b

\section{\ LaTex 控制符} % 相当于转义字符,`\`使用\textbackslash产生
\# \$ \{ \} \~{} \_{} \^{} \textbackslash

\section{排版符号}
\S \P \dag \ddag \copyright \pounds

\section{\ Tex 标志符号}
\ TeX{} \ LaTeX{} \ LaTeXe{}

\section{引号}
`' % `表示单引号的左边,'表示单引号的右边
`` '' % ``表示双引号的左边,''表示双引号的右边
``被引号包裹''

\section{连字符}
- -- ---

\section{非英文字符}
\oe \OE \AE \aa \AA \o \O \l \L \ss \SS !` ?`

\section{重音符号}
\`o \'o \^o \''o \~o \=o \.o \u{o} \v{o} \H{o} \r{o} \t{o} \b{o} \c{o} \d{o}

\end{document}

三、Latex中的图表

3.1 图

在导言区使用\usepackage{grahpicx}命令引入graphicx宏包。

语法:\includegraphics[选项]{文件名},选项用于设置缩放比例、旋转等

格式:EPS, PDF, PNG, JPEG, BMP

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
%导言区 
\documentclass{article}
\title{My Latex Tutorial}
\author{Haofei Ma}
\date{\today}
\usepackage{ctex}
\usepackage{graphicx}
\graphicspath{{figures/},{pics/}}%表示图片在当前目录下的figures目录或pics/目录

%正文区
\begin{document}
% 插入图片
\includegraphics{filename}
\includegraphics{filename2}
\includegraphics{filename3}%two是figures文件夹下的文件(图像)

% 设置缩放比例
\includegraphics[scale=0.3]{filename}

% 高度设置
\includegraphics[height=0.1\textheight]{filename} %文档高度0.1倍高度
\includegraphics[height=2cm]{filename}

% 宽度设置
\includegraphics[width=0.1\textwidth]{filename} % 文档宽度0.1倍宽度
\includegraphics[width=2cm]{filename} % 文档宽度0.1倍宽度

% 旋转角度
\includegraphics[angle=-45,width=2cm]{filename}
\end{document}

3.2 表

使用\begin{tabular}环境生成表格,生成环境时指定列数和每一列的对齐方式

语法:\begin{tabular}[垂直对齐方式]{列格式说明}

  • l: 本列左对齐
  • c: 本列居中对齐
  • r: 本列右对齐
  • p{宽度}: 本列宽度固定,内容超过宽度时会自动换行
  • \\: 表示换行
  • &: 表示不同的列
  • |: 是否在列间添加竖线(||双竖线)
  • \hline: 在行间添加横线(\hline \hline双横线)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
%正文区
\begin{document}
\begin{tabular}{|l| c| c| c| r|}%会有5列,指定每列的居中形式,|表示每列中间有竖线分开
\hline%每行之间由横线分开
姓名&语文&数学&外语&政治\\%\\表示换行
\hline
张三&87&120&25&36\\
\hline
张1&87&120&25&36\\
\hline
张2&87&120&25&36\\
\hline
\end{tabular}

\end{document}

四、Latax中的浮动体

五、Latex中的数学公式

使用\equation{}命令产生带编号的行间公式。

1
2
3
\begin{equation} %表示带编号的行间公式  
AB^2=BC^2+AC^2
\end{equation}

六、Latex中的参考文献

七、Latex中的自定义命令和环境