0%

Latex入门

​ Latex是基于Tex的排版系统,可以方便的生成复杂公式,常常应用于论文的编写。本文介绍在Mac环境下利用Texpad编写Latex。

在Mac环境下编写Latex

MacTeX

​ 在Mac环境下必需下载相应的 MacTeX

​ 下载完成后会得到下面四个app。

​ 其中TeXShop是Latex的编辑器,但功能较为简单,建议使用Sublime+Skim 或 TeXpad

Texpad

​ 一款功能强大的Latex编辑器 Texpad for macOS

Latex

基本结构

​ 每个tex文件都包含导言区和正文区,导言区用于设置全局设置,正文区用于编写显示的内容。

导言区

文件类型

1
\documentclass{article} %文件类型,会根据不同的文件类型有不同的排版,例如article, book, letter等等

标题区

​ 包括title、author、date等等,在正文中调用

1
\maketitle

​ 此时,会显示标题区内容。

正文区

正文

​ 正文部分直接输入,但是要注意:

  • 使用空行分段。单个换行不会引起分段,必须中间有空行,多个空行和一个空行效果一致。
  • 段前不用空格。Latex会自动段前缩进,即使段前打了空格也会被省略。
  • 汉字后的空格会被省略,但英文字母之间不会,多个空格和一个空格效果一致

公式

​ 在 $ $ 中插入公式——行内公式,公式处于行内;

​ 在 中插入公式——行间公式,公式前后自动换行,公式居中。

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
% exemple.tex

%--------------------导言区-------------------
% 用于设置性质(全局设置),自定义一些命令
\documentclass{article} %文件类型,会根据不同的文件类型有不同的排版,例如article, book, letter等等

%----------标题区------------
\title{Tex Introduction}
\author{GuoYi}
\date{\today} %\today会自动同步到今天

%--------------------正文区-------------------
% 一个tex有且只有一个document部分
\begin{document}
\maketitle %实际输出标题区

Hello world!
% 正文中空行表换行

% $行内公式$ $$行间公式$$
Let $f(x)$ be defined by the formula $$f(x)=2sinx$$ which is a polynomial of degree 2.

\end{document}

Latex使用中文

CTEX文档(推荐)

​ 在UTF-8编码情况下,使用CTEX文档。

1
2
3
4
5
6
\documentclass[UTF8]{ctexart}
\title{标题}
\begin{document}
\maketitle
中文输入。{\kaishu 这里是楷体显示},{\songti 这里是宋体显示},{\heiti 这里是黑体显示},{\fangsong 这里是仿宋显示}。
\end{document}

字体

字体族

​ 字体族共有3种:罗马字体族(Roman Family)、无衬线字体族(Sans Serif Family)、打字机字体族(Typewriter Family)。

​ 正文一般默认使用罗马字体族,可用下面两种字体族命令规定字体族。

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

\begin{document}
%字体族设置(罗马字体族、无衬线字体族、打字机字体族)
\textrm{Roman Family} \textsf{Sans Serif Family} \texttt{Typewriter Family}

\rmfamily Roman Family \sffamily Sans Serif Family \ttfamily Typewriter Family

{\rmfamily Roman Family} {\sffamily Sans Serif Family} {\ttfamily Typewriter Family}
\end{document}

截屏2020-02-26上午1.47.37.png

字体系列

​ 字体系列共有2种:中等(medium)、加宽加粗(bold extended)。

​ 正文默认使用中等字体系列。

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

\begin{document}
%字体系列设置(粗细、宽度)
\textmd{Medium Series} \textbf{Boldface Series}

{\mdseries Medium Series} {\bfseries Boldface Series}
\end{document}

字体形状

​ 字体形状共有4种:直立(Upright Shape)、意大利(Italic Shape)、倾斜(Slanted Shape)、小型大写(Small Caps Shape)。

​ 正文默认使用直立字体形状。

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

\begin{document}
%字体形状(直立、意大利、倾斜、小型大写)
\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}
\end{document}

中文字体

​ 中文字体共有4种:宋体、黑体、仿宋、楷书。

​ ctexart文体,正文中的中文默认为宋体。

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

\begin{document}
%中文字体(宋体、黑体、仿宋、楷书)
{\songti 宋体} \quad {\heiti 黑体} \quad {\fangsong 仿宋} \quad {\kaishu 楷书}
\end{document} %\quad为tap

截屏2020-02-26上午2.13.50.png

​ 中文字体同样包含粗体和斜体。

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

\begin{document}
%中文字体的粗体和斜体
中文字体的\textbf{粗体}\textit{斜体}
\end{document}

截屏2020-02-26上午2.20.15.png

字体大小

英文字体大小

​ 以\normalsize的大小为基础,来设置字体大小。

​ 正文字体默认为\normalsize,\normalsize字体大小默认为10pt。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\documentclass[12pt]{ctexart} %[]中可设置normalsize字体大小

\begin{document}
% 字体大小
{\tiny tiny}\\ % \\为换行
{\scriptsize scriptsize}\\
{\footnotesize footnotesize}\\
{\small small}\\
{\normalsize normalsize}\\
{\large large}\\
{\Large Large}\\
{\LARGE LARGE}\\
{\huge huge}\\
{\Huge Huge}\\

\end{document}

中文字体大小

​ 以五号字体的大小为基础,来设置字体大小。

​ 正文字体默认为 五号字体。

截屏2020-02-26上午2.46.40.png

1
2
3
4
5
6
7
8
9
10
11
12
\documentclass{ctexart}

\begin{document}
% 中文字体大小
\zihao{2} 二号中文字体

\zihao{-2} 小二号中文字体

\zihao{5} 五号中文字体

\zihao{-5} 小五号中文字体
\end{document}

截屏2020-02-26上午2.44.21.png

篇章结构

提纲

​ \section可以在正文中构建小节,相当于一级标题,在section前加sub套娃形成二级、三级、四级等等标题。

1
2
3
4
5
6
7
8
\documentclass{article}

\begin{document}
\section{section title} %一级标题
\subsection{subsection title} %二级标题
\subsubsection{subsubsection title} %三级标题
\section{section title} %三级标题
\end{document}

截屏2020-02-26下午7.11.30.png

目录

​ \tableofcontents 可生成目录。

1
2
3
4
5
6
7
8
9
\documentclass{article}

\begin{document}
\tableofcontents %生成目录

\section{section title} %一级标题
\subsection{subsection title} %二级标题
\section{section title} %三级标题
\end{document}

正文

  • 使用空行分段。单个换行不会引起分段,必须中间有空行,多个空行和一个空行效果一致。
  • 段前不用空格。Latex会自动段前缩进,即使段前打了空格也会被省略。
  • 汉字后的空格会被省略,但英文字母之间不会,多个空格和一个空格效果一致
  • \ \ 用来换行。换行不会新开一个段落。
  • \par 分段。一般不使用。
1
2
3
4
5
6
7
8
\documentclass{article}

\begin{document}
This is an article. This is an article. This is an article.
%空行——新开一个段落

This is an article. This is an article. This is an article.\\Change the line. Change the line. Change the line. % \\——换行
\end{document}

特殊字符

空白符号

  • 英文字符之间多个空格当一个空格处理,中文字符之间的空格无效;
  • 中文之间插入英文,在英文片段前后自动添加空格;
  • 其他空格命令
命令 功能
\quad 1em的空白(推荐)
\qquad 2em的空白
\, 1/6em的空白
\thinspace 1/6em的空白
\enspace 0.5em的空白
\ 空格(推荐)
硬空格
\kern 指定宽度 产生指定宽度的空白(宽度可为副值)
\hspace{指定宽度} 产生指定宽度的空白(宽度可为副值)
\hphantom{} 占位宽度的空格(空格长度等于{}内的字符应有的长度)
\hfill 产生一行的空格(前后分别在行首和行尾)
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{article}

\begin{document}
% 1em的空白
a\textbackslash quad b --------------- a\quad b

% 2em的空白
a\textbackslash qquad b -------------- a\qquad b

% 1/6em的空白
a\textbackslash,b --------------------- a\,b

% 1/6em的空白
a\textbackslash thinspace b --------- a\thinspace b

% 0.5em的空白
a\textbackslash enspace b ------------ a\enspace b

% 空格
a\textbackslash b ----------------------- a\ b

% 硬空格
a\~b ------------------------ a~b

% 产生指定宽度的空白(宽度可为副值)
a\textbackslash kern 1em b ----------- a\kern 1em b

% 产生指定宽度的空白(宽度可为副值)
a\textbackslash hspace\{1em\}b ------ a\hspace{1em} b

% 占位宽度的空格(空格长度等于{}内的字符应有的长度)
a\textbackslash hphantom\{xyz\}b --- a\hphantom{xyz}b

% 产生一行的空格(前后分别在行首和行尾)
a\textbackslash hfill b ------------------ a\hfill b
\end{document}

控制符

​ 对于有特殊含义的符号,要想其正常显示,需在符号前加 \ ,而 \ 本身由于\ \表示换行,所以用 \textbacklash 表示。

控制符 命令
\ \textbackslash
# \#
$ \$
{} \{\}

排版符号

排版符号 命令
§ \S
\P
\dag
\ddag
© \copyright
£ \pounds

标志符号

1
2
3
4
5
\documentclass{article}

\begin{document}
\TeX \quad \LaTeX
\end{document}

引号

符号名称 符号 命令
左单引号 `
右单引号
左双引号 ``
右双引号 ‘’

插图

基本

​ 在LaTeX中,插图是由 graphics 或 graphicx 宏包所使用的 \includegraphics 命令完成的。graphics 和 graphicx 功能差别不大,但 graphicx 使用更方便一些。

​ 插图适用格式:EPS、PDF、PNG、JPEG、BMP

​ 搜索路径默认为tex文件所在文件夹,也可用\graphicspath 对搜索路径进行修改。

1
2
3
4
5
6
7
8
9
10
\documentclass{article}

% 调用graphicx宏包,使用插图功能
\usepackage{graphicx}
\graphicspath{{\figures}} %修改搜索路径

\begin{document}
% 基于搜索路径插入图像
\includegraphics{Sx2ec.png}
\end{document}

命令选项

大小缩放

​ width、height 和 scale 设置图形的宽度、高度或缩放比例。

1
2
3
4
5
6
7
8
9
10
11
\documentclass{article}

% 调用graphicx宏包,使用插图功能
\usepackage{graphicx}

\begin{document}
% 基于搜索路径插入图像
\includegraphics[width=10em]{figures/Sx2ec} %宽度为10em
\includegraphics[height=5cm]{figures/Sx2ec} %高度为10cm
\includegraphics[scale=0.5]{figures/Sx2ec} %缩放0.5倍
\end{document}

旋转

​ angle 选项可以让图形逆时针旋转一定的角度,旋转的中心可以用 origin 选项确定。origin的值可以用字符 l,r,c,t,b,B中的一个或者两个,分别表示左,右,中,上,下和基线,默认值为 lb。

1
2
3
4
5
6
7
8
9
10
\documentclass{article}

% 调用graphicx宏包,使用插图功能
\usepackage{graphicx}

\begin{document}
% 基于搜索路径插入图像
\includegraphics[angle=90]{figures/Sx2ec} %基于左下,逆时针旋转90度
\includegraphics[angle=-45,origin=c]{figures/Sx2ec} %基于中心,顺时针旋转45度
\end{document}

表格

​ LaTeX表格使用环境录入:在文本或数学模式下都可以使用 tabular 环境,在数学模式下还可以使用 array 环境。tabular 和 array 的一般格式基本相同。

基本

​ 每行后用 \\ 表示换行,一行内的不同列之间用 & 分开。

​ 列格式说明,用 l、c、r 分别表示 左对齐、居中、右对齐。

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

\begin{document}

% \begin{tabular}[(垂直对齐)]{(列格式说明)}
% (表项)&(表项)&...&(表项)\\
% ....
% \end{tabular}

\begin{tabular}{lcr} %三列 分别为:左对齐、居中、右对齐
l & c & r \\ %一行
left & center & right \\ %一行
\end{tabular}

\end{document}

表格线

​ 列表格线:在列格式说明中加 |

​ 行表格线:\hline

​ 需要双线是重复 || 或 \hline\hline。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
\documentclass{article}

\begin{document}

% \begin{tabular}[(垂直对齐)]{(列格式说明)}
% (表项)&(表项)&...&(表项)\\
% ....
% \end{tabular}

\begin{tabular}{|l|c|r|} %三列 分别为:左对齐、居中、右对齐
\hline
l & c & r \\ %一行
\hline \hline
left & center & right \\ %一行
\hline
\end{tabular}

\end{document}

垂直对齐

​ 确定表格的基线位置。

  • t 按表格顶部对齐
  • b 按表格底部对齐
  • 默认 垂直居中
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{article}

\begin{document}
---line1---
\begin{tabular}{|rr|} %默认垂直居中
\hline
Input & Output \\
\hline
$-2$ & 4 \\
0 & 0 \\
2 & 4 \\
\hline
\end{tabular}

---line2---
\begin{tabular}[t]{|rr|} %按表格顶部对齐
\hline
Input & Output \\
\hline
$-2$ & 4 \\
0 & 0 \\
2 & 4 \\
\hline
\end{tabular}

---line3---
\begin{tabular}[b]{|rr|} %按表格底部对齐
\hline
Input & Output \\
\hline
$-2$ & 4 \\
0 & 0 \\
2 & 4 \\
\hline
\end{tabular}
\end{document}

截屏2020-02-29上午12.19.31.png

列格式说明

  • l 本列左对齐
  • c 本列居中
  • r 本列右对齐
  • p{(宽)} 本列具有固定宽度,且可以自动换行。
  • | 画一条竖线
  • @{(内容)} 添加任意内容,不占表项计数。
  • *{(计数)}{(列格式说明符)} 将给出的列格式说明符重复多次。
1
2
3
4
5
6
7
8
9
10
11
\documentclass{article}

\begin{document}
\begin{tabular}{|c|*{3}{r@{.}l|}}
\hline
revenue & 1234&6 & 2345&23 & 239&0 \\
\hline
cost & 231&24 & 123&0 & 329&18 \\
\hline
\end{tabular}
\end{document}

表格单元的合并与分割

合并

行合并

\multicolumn{(项数)}{(新列格式)}{(内容)} 可用于将一行中的几个不同的表项合并为一项。

1
2
3
4
5
6
7
8
9
10
11
12
13
\documentclass{article}

\begin{document}
\begin{tabular}{|r|r|}
\hline
\multicolumn{2}{|c|}{score} \\ %居中合并两个单元格,内容为score
\hline
Maths & English \\
\hline
80 & 85 \\
\hline
\end{tabular}
\end{document}

截屏2020-02-29上午2.01.15.png

列合并
\cline(容易实现,但效果不佳)

\cline{(起始列号)-(终止列号)} 和 \hline 一样可以画水平的表格线,但是 \cline 可以规定表格线起始和终止的列号,即可相当于合并列单元。

1
2
3
4
5
6
7
8
9
10
11
\documentclass{article}

\begin{document}
\begin{tabular}{|c|r|r|}
\hline
& Maths & English \\
\cline{2-3} %只画2到3列
score & 80 & 85 \\
\hline
\end{tabular}
\end{document}

\multirow (效果较好)

​ \multirow{(行数)}{(宽度)}{(内容)} 或 \multirow{(行数)}*{(内容)}

​ 第一种形式可自动换行。

​ 使用\multirow得先引入宏包multirow。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
\documentclass{article}

% 使用\multirow得先引入宏包multirow
\usepackage{multirow}

\begin{document}
\begin{tabular}{|c|r|r|}
\hline
\multirow{2}*{score} & Maths & English \\
\cline{2-3}
& 80 & 85 \\
\hline
\end{tabular}
\end{document}

拆分

​ \diagbox 自动判断是把表头分成两部分还是三部分。

​ 使用\diagbox得先引入宏包diagbox。

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

% 使用\diagbox得先引入宏包diagbox
\usepackage{diagbox}

\begin{document}
\begin{tabular}{|c|c|c|}
\hline
\diagbox{Letter}{Number}& 1 & 2 \\
\hline
a & 0.1 & 0.3 \\
\hline
b & 0.3 & 0.8 \\
\hline
\end{tabular}
\end{document}

浮动体

​ 浮动体用于实现灵活分页,给图表添加标题,交叉应用。

​ 图片使用 figure环境,表格使用 table环境。

格式(以figure为例):

1
2
3
\begin{figure}[<允许位置>]
< 任意内容 >
\end{figure}

<允许位置>参数(默认tbp):

  • h —- 此处(here),代码所在的上下文位置
  • t —- 页顶(top),代码所在页面或之后的页面的顶部
  • b —- 页底(bottom),代码所在页面或之后的页面的底部
  • p —- 独立一页(page),浮动页面
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{ctexart}

% 调用graphicx宏包,使用插图功能
\usepackage{graphicx}

\begin{document}
下面\ref{Latex}\LaTeX{}的标志。% \ref为引用标签
\begin{figure}[htbp] %htbp可以让图表随文字运动
\centering %浮动体居中
\includegraphics{figures/Sx2ec}
\caption{\LaTeX 的标志} %标题
\label{Latex} %标签(用于引用)
\end{figure}

引用下面\ref{InAndOut} 中的数据。% \ref为引用标签
\begin{table}[htbp] %htbp可以让图表随文字运动
\centering %浮动体居中
\begin{tabular}{|rr|}
\hline
Input & Output \\
\hline
$-2$ & 4 \\
0 & 0 \\
2 & 4 \\
\hline
\end{tabular}
\caption{输入输出数据} %标题
\label{InAndOut} %标签(用于引用)
\end{table}
\end{document}

数学公式

​ LaTeX 将排版内内容分为文本模式和数学模式。文本模式适用于普通文本排版,数学模式用于数学公式排版。

基本

行内公式

​ 不需换行的公式。下面三种方法均可生成行内公式。

$ 美元符号

​ 格式:

小括号

​ 格式: \(<数学公式>\)

math环境

​ 格式: \begin{math}<数学公式>\end{math}

​ 使用math环境需引入 amsmath宏包。

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

\usepackage{amsmath}

\begin{document}
% $ 美元符号
交换律是 $a+b=b+a$,如 $1+2=2+1=3$

% 小括号
交换律是 \(a+b=b+a\),如 \(1+2=2+1=3\)

%math环境
交换律是 \begin{math} a+b=b+a\end{math},如 \begin{math} 1+2=2+1=3\end{math}
\end{document}

行间公式

​ 行间公式换行不分段。下面三种方法均可生成行间公式。

$ 美元符号

​ 格式: $$$<数学公式>$$$

中括号

​ 格式: \[<数学公式>\]

displaymath环境

​ 格式: \begin{displaymath}<数学公式>\end{displaymath}

​ 使用displaymath环境需引入 amsmath宏包。

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

\usepackage{amsmath}

\begin{document}
% $ 美元符号
交换律是
$$a+b=b+a$$

$$1+2=2+1=3$$

% 中括号
交换律是
\[a+b=b+a\]

\[1+2=2+1=3\]

%displaymath环境
交换律是
\begin{displaymath} a+b=b+a\end{displaymath}

\begin{displaymath} 1+2=2+1=3\end{displaymath}
\end{document}

自动编号公式——equation环境

​ equation环境可以对公式自动编号,此时添加 \label 标签方便引用。

​ 格式:\begin{equation}<数学公式>\end{equation}

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

\begin{document}
调用下面的公式\ref{eq:exchange}
\begin{equation}
a+b=b+a \label{eq:exchange}
\end{equation}
\end{document}

数学结构

上标与下标

上标

​ 用 ^{} 表示上标。

1
2
3
4
5
6
\documentclass{article}

\begin{document}
% 上标^{}
$ f(x)=x^2 + x^{20} + 2x^{4x^2+1} $
\end{document}

下标

​ 用 _{} 表示下标。

1
2
3
4
5
6
\documentclass{article}

\begin{document}
% 下标_{}
$a_0,a_1,a_2,...,a_{100}$
\end{document}

其他上下标
符号 命令
° ^\circ

求和

​ 格式: \sum_{<下限>}^{<上限>} <函数>

​ 或(上面的格式上下限在右边,下面的\limit格式上下限在正上下方 )

\sum\limits_{<下限>}^{<上限>} <函数>

1
2
3
4
5
6
7
\documentclass{ctexart}

\begin{document}
$ \sum_{i=0}^{n} a_i $ %上下限在右边
\quad
$ \sum\limits_{i=0}^{n} a_i $ %上下限在正上下方
\end{document}

积分

​ 格式: \int_{<下限>}^{<上限>} <函数> d\! <变量>

​ 多重积分用 \iint \iiint

1
2
3
4
5
\documentclass{ctexart}

\begin{document}
$ \int_0^1 f(t) d\! t $
\end{document}

分式

​ 格式: \frac <分子><分母>

1
2
3
4
5
\documentclass{article}

\begin{document}
$ \frac 12 + \frac 1a = \frac{2+a}{2a} $
\end{document}

根式

​ 格式: \sqrt[<开方次数>]{<被开方数>}

1
2
3
4
5
\documentclass{article}

\begin{document}
$ \sqrt[n]{\frac{x^2+\sqrt{2}}{x+y}} $
\end{document}

矩阵

​ amsmath宏包提供一下矩阵形式。

​ 编写格式与表格相似。

1
2
3
4
5
6
7
8
9
10
11
12
\documentclass{article}

% 使用矩阵,引入amsmath宏包
\usepackage{amsmath}

\begin{document}
$ A = \begin{pmatrix}
a_{11} & a_{12} & a_{13} \\
0 & a_{22} & a_{23} \\
0 & 0 & a_{33}
\end{pmatrix} $
\end{document}

省略号
  • 行省略号 \dots
  • 列省略号 \vdots
  • 斜省略号 \ddots
1
2
3
4
5
6
7
8
9
10
11
12
\documentclass{ctexart}

% 使用矩阵,引入amsmath宏包
\usepackage{amsmath}

\begin{document}
$A = \begin{bmatrix}
a_{11} & \dots & a_{1n} \\
& \ddots & \vdots \\
0 & & a_{nn}
\end{bmatrix}_{n\times n}$
\end{document}

  • 多列省略号 \hdotsfor{<列数>}
1
2
3
4
5
6
7
8
9
10
11
12
\documentclass{ctexart}

% 使用矩阵,引入amsmath宏包
\usepackage{amsmath}

\begin{document}
$A = \begin{bmatrix}
a_{11} & \dots & a_{1n} \\
\hdotsfor{3} \\ %跨3列的省略号
0 & & a_{nn}
\end{bmatrix}_{n\times n}$
\end{document}

符号与类型

符号 LaTeX
$\geq$ \geq
$\leq$ \leq
$\approx$ \approx
$\pm$ \pm
$\partial$ \partial
符号 LaTeX
$\vec x$ \vec x
$\hat x$ \hat x
$\widehat x$ \widehat x

希腊字母

数学普通符号

运算符

数学算子

多行公式

​ equation环境中,换行命令\\无效,所以多行公式需要使用其他环境,使其可用\\换行。

​ 使用多行公式的环境,均需引入 amsmath宏包。

gather环境

​ gather环境中公式自带编号,如要阻止编号,需使用 \notag 命令。

1
2
3
4
5
6
7
8
9
10
11
\documentclass{article}

% 使用gather环境,引入amsmath宏包
\usepackage{amsmath}

\begin{document}
\begin{gather}
a+b = b+a \\ %带编号
ab = ba \notag %不带编号
\end{gather}
\end{document}

gather*环境

​ 与gather环境不同的是,公式不带编号。

1
2
3
4
5
6
7
8
9
10
11
\documentclass{article}

% 使用gather环境,引入amsmath宏包
\usepackage{amsmath}

\begin{document}
\begin{gather*} %不带编号
a+b = b+a \\
ab = ba
\end{gather*}
\end{document}

align环境

​ align环境允许公式按等号或其他关系符对齐,在关系符前加 & 表示对齐。

1
2
3
4
5
6
7
8
9
10
11
\documentclass{article}

% 使用gather环境,引入amsmath宏包
\usepackage{amsmath}

\begin{document}
\begin{align} %关于&对齐,带编号
x &= t + \cos t + 1 \\
y &= 2\sin t
\end{align}
\end{document}

align*环境

​ 与align环境不同的是,但公式不带编号。

自定义命令

\newcommand 定义新的命令

  • 格式:\newcommand<命令>[<参数个数>][<首参数默认值>]{<具体定义>}
  • 命令只能由字母组成,不能以 \end 开头
  • 参数可以从1到9,用 #1,#2,….,#9 表示
  • 默认值为非必填项,只能设定 #1 的参数
1
2
3
4
5
6
7
8
9
10
11
\documentclass{article}

% 设置新命令
\newcommand{\PRC}{People's Republic of China}
\newcommand{\loves}[2]{#1 love #2}

\begin{document}
\PRC

\loves{I}{apple}
\end{document}

\renewcommand 重新定义命令

  • 格式:\renewcommand<命令>[<参数个数>][<首参数默认值>]{<具体定义>}
  • 只能定义已有命令
1
2
3
4
5
6
7
8
\documentclass{ctexart}

% 重新定义已有命令
\renewcommand \abstractname{简介}

\begin{document}
\abstractname
\end{document}