$\LaTeX$笔记

基本结构

ctexart文档类型表示中文短文档,сtexbook表示中文长文档,不常用的还有book、article和report等。

1
2
3
4
5
6
7
8
9
10
11
12
13
\documentclass{ctexart}
\title{书名}
\author{作者}
\date{\today}
\begin{document}
\maketitle
\newpage
\tableofcontents %目录
\chapter{...} %只ctexbook类型
\section{...}
\subsection{...}
...
\end{document}

对于诗歌、引言等可用quote环境:

1
2
3
4
5
6
\begin{quote} %中间对齐 行首无缩进 四周间距比正文大
引用小段文章
\end{quote}
%此外还有quotation环境 首行缩进 四周间距比正文大 引用大段文章
%verse环境 四周间距比正文大 折行时悬挂缩进 诗歌
%abstract环境 需要文档类型为article或report 文章摘要 用\abstractname设置摘要标题 默认“摘要”

列表环境如下,可嵌套:

1
2
3
4
\begin{enumerate} %有序列表
\item ...
\end{enumerate}
%还有itemize无序列表 description字母列表

数学公式:

1
2
3
4
\usepackage{amsmath}
\begin{equation}
...
\end{equation}

图形表格:

1
2
3
4
5
6
7
8
9
10
11
12
\usepackage{diagbox}
\usepackage{array}
\usepackage{xcolor}
\begin{tabular}{|c|c|c|}
\hline
\diagbox[linewidth=1.5pt,linecolor=blue]{姓名}{科目}&语文&数学&英语\\ %表头线宽和颜色
\hline
张三&85&85&85\\
\hline
...
\hline
\end{tabular}

数学函数绘制:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\usepackage{tikz}
\usetikzlibrary{arrows.meta,automata,positioning,decorations.markings}
\begin{tikzpicture}[
domain=0:4,
label/.style={
postaction={
decorate,
decoration={
markings,
mark=at position .75 with \node #1;
}
}
}
]
\draw[very thin,color=gray](-0.1,-1.1)grid(3.9,3.9);
\draw[->](-0.2,0)--(4.2,0)node[right]{$x$};
\draw[->](0,-1.2)--(0,4.2)node[above]{$f(x)$};
\draw[red,label={[above left]{$f(x)$=x}}]plot(\x,\x);
\draw[blue,label={[below left]{$f(x)=\sin x$}}]plot(\x,{sin(\x r)});
\draw[orange,label={[right]{$f(x)=\frac{1}{20}\mathrm e^x$}}]plot(\x,{0.05*exp(\x)});
\end{tikzpicture}

幻灯片实例:

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{beamer}
\usepackage{ctex} %显示中文
\usetheme{Warsaw}
\usecolortheme{wolverine}
\usefonttheme[stillsansserifmath]{serif}
\setbeamercolor{normal text}{bg=gray!20}
\title{标题}
\author{作者}
\date{\today}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}
\frametitle{标题}
\framesubtitle{副标题}
\begin{theorem} %定理框
...
\end{theorem}
\begin{proof} %证明框
...
\end{proof}
\end{frame}
\end{document}

命令自定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
\newcommand{\emphxy}[1]{\textcolor{red}{\textbf{#1}}} %新定义\emphxy 1表示参数个数
\newenvironment{Abstract}{ %定义Abstract环境分别为begdef和enddef
\begin{center}
\normalfont
\bfseries
摘要
\end{center}
\begin{quote}
}{
\end{quote}
\par
}
\begin{Abstract}
...
\end{Abstract}

\usepackage{xspace}
\newcommand\USA{United States of America\xspace} %xspace智能添加空格 避免与后面文字粘连
xxx \USA xxx

核心知识

多国语言文字:

1
2
3
4
5
\usepackage{ctex} %中文
%同时排版多国语言:
\usepackage{fontspec} %可设置字体 不用PdfLaTeX
\setmainfont{CMU Serif}
\usepackage{xeCJK} %可换ctex

符号:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
%重音符号
\'a \-a \v{a} \c{a} \`a \=a \H{a} \d{a} \^a \.a \t{aa} \b{a} \"a \u{a} \r{a}
%特殊符号
\AA \OE \IJ \O \aa \oe \ij \o \AE \SS \L \i \ae \ss \l \j
\# \$ \% \& \{ \} \_ \textbackslash
\usepackage{alltt}
\begin{alltt}
可特殊字符 # $ % ^ & ~ _
\end{alltt}

%标点符号
This is `left single quatationi mark'.
这是‘左单引号’。
%英文单引号:伪代码
\usepackage{listings}
\lstset{upquote}
\begin{lstlisting}
aaa'aaa"aaa
\end{lstlisting}
\verb<char>text<char> %例如&:\verb|&| \verb=&=等
\verb*"a & b" %用类似于下划线的空格

程序段:

1
2
3
4
\usepackage{verbatim}
\begin{verbatim}
int main(){}
\end{verbatim}

打印数学公式:

1
2
3
4
5
\usepackage{verbatim}
\usepackage{fancyvrb}
\begin{Verbatim}
\sum_{i=1}
\end{Verbatim}

字体修饰:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
\textbf{加粗}
\usepackage{CJKfntef}
\CJKunderdot{着重号} %或ctex或xeCJK
\usepackage{ulem}
\uline{aaa} %单下划线
\uwave{aaa} %波浪线
\xout{aaa} %斜删除线
\dotuline{aaa} %下加点
\uuline{aaa} %双下划线
\sout{aaa} %删除线
\dashuline{aaa} %虚线
\usepackage{soul}
\caps{aaa} %转大写
\so{aaa} %拉宽
\ul{aaa} %同uline
\hl{aaa} %背影:浅灰 加载color宏包:黄
\st{aaa} %同sout

空格:

1
2
3
\quad \qquad
\phantom{...} %和内容一样长的空格
\hphantom{...} \vphantom{...} 水平、垂直

常用字体:

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
命令 声明
textrm rmfamily
textsf sffamily
texttt ttfamily
textmd mdseries
textbf bfseries
textup upshape
textit itshape
textsl slshape
textsc scshape
emph em
textnormal normalfont

%例如等价:
\texttt{aaa}
{\ttfamily aaa}
\begin{ttfamily}
aaa
\end{ttfamily}

%中文专用
\songti \fangsong \lishu \yahei \heiti \kaishu \youyuan \pingfang

%数学专用
\mathcal \mathrm \mathbf \mathsf \mathtt \mathnormal \mathit

%字号从大到小 \Huge为一号 其他以此类推
\Huge \huge \LARGE \Large \large \normalsize \small \footnotesize \scriptsize \tiny

%字号调整
\usepackage{relsize}
\relsize{n} %n>0 增大n字号 n<0减小
\smaller{aaa} %字号-1
\larger{aaa} %字号+1
\textsmaller[n]{aaa}
\textlarger[n]{aaa}
%以下适用数学公式 表示调整为f倍 如1.2为120% 可叠加
\relscale{f}{aaa} %等于textscale
\mathsmaller{aaa}
\mathlarger{aaa}
%以下为中文专用 n表示调整为大写“n”号字,-n表示大写小“n”号字 0为初号 -0为小初号
\usepackage{ctex}
\zihao{1}

%大小写切换
\usepackage{textcase}
\MakeTextUppercase{aaa\NoCaseChane{bbb}ccc}
\MakeTextLowercase{aaa}

%空格、间距与长度单位
\, \enspace \nobreakspace \ %nobreakspace等于“~” 最后一个有个空格
\hspace{2cm} \vspace{2cm} %行水平、垂直间距
\fill %行末剩余空间长度
\stretch{n} %n倍\fill长度
\hfill \vfill%相当于\hspace{\fill}
\hrulefill \dotfill %后者用点填充\hfill长度
pt 榜
em 全方 1em为1个\quad
ex 字号
pc 1pc=12pt=四号字
1in=72.27pt
2.54cm=1in
1cc=12dd
1in=72bp
65536sp=1pt
10mm=1cm
1157dd=1238pt

段落:

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
%缩进
\usepackage{ctex}
\indent \noindent %强制缩进/不缩进 默认除subparagraph时标题都不缩进
\ctexset{
part={
format+=\raggedright,
indent=3\ccwd,
},
section={
format=\Large\bfseries,
indent=20pt,
}
}

%首行缩进
\usepackage{indentfirst}
\setlength{\parindent}{2em}
%换行
\par %产生空行
\\ \\[2cm] %强行换行 产生2cm额外行间距
\linebreak \nolinebreak %强行换行 可带参数0~4 0不可以换行 默认4必须换行
\pagebreak %换页

%水平对齐
\begin{flushleft} %center、flushright等
\end{flushleft}
%或
\raggedright{aaa} %相反 相当于左对齐 centering、raggedleft等
%ragged2e宏包
\usepackage{raagged2e}
\begin{FlushLeft} %Center、FlushRight等
\end{FlushLeft}
%或
\RaggedRight %Centering、RaggedLeft等

%行间距
\baselineskip{aaa} %默认行间距 比字号大20%
\linespread{n}{aaa} %修改行间距
\usepackage{setspace}
\singlespacing{aaa} %onehalfspacing、doulespacing等
\begin{singlespace} %onehalfspace、doublespace等
\end{singlespace}
\setstretch{1.2}
\begin{spacing}{1.2}
\end{spacing}

首字下沉:

1
2
3
4
5
6
\usepackage{lettrine}
\lettrine{A}{bbb}... %bbb文本部分英文默认大写
\renewcommand\LettrineFontHook{\sffamily\bfseries} %通过重定义修改字符样式
\renewcommand\LettrineTextFont{\sffamily\scshape} %通过重定义修改文本字体样式
\lettrine[lines=4,loversize=-0.1,lraise=0.1,lhang=.2,slope=0.6em,findent=-1em,nindent=0.6em]{A}{bbb}...
%lines下沉行数 loversize增大下沉的高度 lraise上下移动 lhang下沉宽度 slope如A/V等字母与各行首的距离 findent缩进行于下沉的水平距离 nindent第二行开始水平移动缩进行

图文环绕:

1
2
3
4
5
6
\usepackage{picinpar}
\begin{window}[lines,where,commands,caption] %lines在第几行下方插入图标 where插入位置(l/c/r) command要插入的内容 caption标题可省
aaa...
\end{window}
\fbox{aaa} %边框
%window环境不能产生被环绕图表的编号 figwindow环境可以 用法同上

定理环境:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
%格式:\newtheorem{envname}[counter]{caption}[within]
%envname为自定义环境名称 caption为自定义环境标题 within为自定义环境主计数器 counter为延续编号计数器
%counter和within可选

%格式1:定理1(某定理)
\newtheorem{thm}{定理}
\begin{thm}[某定理(可选)]
...
\end{thm}

%格式2:引理 X.X.X 从section层级开始编号
\newtheorem{lemma}{引理}[section]
\begin{lemma}
...
\end{lemma}