网址链接 : 刘海洋 · LaTeX 不快速的入门 - 跟着大神学习最纯正的 LaTeX 知识
一、组织文档结构 1. 文档基本结构- 以document环境为界
- document环境前是导言部分
- 环境内部是正文部分
- 环境之后的部分被忽略
- 在导言区进行格式设置,正文部分套用格式
%%% 简单文档
% 导言:格式设置
documentclass{ctexart}
usepackage[b5paper]{geometry}
% 正文:填写内容
begin{document}
使用 Latex
end{document}
2. 文档部件
- 标题:title, author, date — maketitle
- 摘要/前言:abstract 环境/ chapter*
- 目录:tableofcontents
- 章节:chapter, section, …
- 附录:appendix + chapter 或 section…
- 文献:bibliography
- 索引:printindex
- 大型文档:frontmatter、mainmatter、backmatter
- 一般文档:appendix
| 层次 | 名称 | 命令 | 说明 |
|---|---|---|---|
| -1 | part | part | 可选的最高层 |
| 0 | chapter | chapter | report, book类最高层 |
| 1 | section | section | article类最高层 |
| 2 | subsection | subsection | |
| 3 | subsubsection | subsubsection | report, book 类 默认不编号、不编目录 |
| 4 | paragraph | paragraph | 默认不编号、不编目录 |
| 5 | subparagraph | subparagraph | 默认不编号、不编目录 |
- 文件目录结构
- 主文档,给出文档框架结构
- 按内容章节划分不同的文件
- 使用单独的类文件和格式文件设置格式
- 用小文件隔离复杂的图表
- 相关命令
- documentclass:读入文档类文件(.cls)
- usepackage:读入一个格式文件–宏包(.sty)
- include:分页,并读入章节文件(.tex)
- input:读入任意的文件
% language-main.tex
documentclass{book}
usepackage{makeidx}
makeindex
title{Languages} author{someone}
begin{document}
frontmatter
maketitle
tableofcontents
mainmatter
include{intro}
include{class}
backmatter
include{appendix}
bibliography{foo}
printindex
end{document}
% intro.tex
part{Introduction}
chapter{Background}
% class.tex
part{Classification}
chapter{Natural Language}
chapter{Computer Languages}
section{Machine Languages}
section{High Level Languages}
subsection{Compiled Language}
subsection{Interpretative Language}
subsubsection{Lisp}
paragraph{Common Lisp}
paragraph{Scheme}
subsubsection{Perl}
% appendix.tex
chapter{Appendix}
二、填写文档内容
-
英文 ---- 用pdflatex编译
documentclass{article} begin{document} Hello world. end{document} -
中文 ---- 用xelatex编译
documentclass{ctexart} begin{document} 你好,世界! end{document}
- 命令:参数总在后面花括号表示,用中括号表示可选参数
cmd{arg1}{arg2}\
cmd[opt]{arg1}{arg2}
分数 1/2 frac{1}{2}
- 环境
begin{env}
....
end{env}
矩阵 begin{matirx} ... \ ... end{matrix}
- 注释:以符号%开头,该行在%后面的部分全为注释
% 多行注释 iffalse 这里面是多行注释 这里面是多行注释 fi2. 正文 1. 正文文本
- 用空格分开单词
- 一个换行符等同于一个空格
- 多个空格的效果与与一个相同
- 自然段分段是空一行
-
占用符号
# ==> # $ ==> $ % ==> % & ==> & { == > { } == > } textbackslash ==> -
非键盘符号(具体见symbols文档)
S dag copyright ...
- 行内公式:使用一对符号$$来表示
- 显示公式
- 简单的不编号公式用命令 中括号 表示(不要使用双美元符号)
- 基本的编号的公式用equation环境
- 更复杂的结构,使用amsmath宏包提供的专门的数学环境。(不要使用eqnarray环境)
documentclass{article}
begin{document}
begin{equation}
a^2+b^2 = c^2
end{equation}
end{document}
2. 数学结构
- 上标与下标:用 ^ 和 _ 表示
- 上下画线与花括号:overline, underline, overbrace, underbrace
- 分式:frac{分子}{分母}
- 根式:sqrt[次数]{根号下内容}
- 矩阵:使用amsmath宏包提供的专门的矩阵环境matrix,pmatrix,bmatrix等。
- 特别复杂的矩阵使用array环境作为表格画出
- 数学字母
- 数学字体 mathbb、mathcal等
- 普通符号:如infty(∞),angle(∠)
- 二元运算符
- 二元关系符
- 括号 可使用left, right放大
- 标号:逗号,分号(colon)
- amsmath是基本的数学工具包
- mathtools是对amsmath做了一些补充和增强
documentclass{article}
usepackage{amsmath}
usepackage{mathtools}
begin{document}
begin{align*}
2^5 &= (1+1)^5 \
&= begin{multlined}[t]
binom{5}{0}cdot 1^5 + binom{5}{1} cdot 1^4 cdot 1
+ binom{5}{2} cdot 1^3 cdot 1^2 \
+ binom{5}{3} cdot 1^2 cdot 1^3
+ binom{5}{4} cdot 1 cdot 1^4 + + binom{5}{5} cdot 1^5
end{multlined} \
&= binom{5}{0} + binom{5}{1} + binom{5}{2} + binom{5}{3} + binom{5}{4} + binom{5}{5}
end{align*}
end{document}
4. 科技公式
- siunitx 宏包 : 数字单位的解决方案
documentclass{article}
usepackage{siunitx}
begin{document}
num{-1.235e96} \
SI{299792458}{m/s} \
begin{tabular}{|S|}hline
-234532 \ 13.55 \ .9e37km \
hline
end{tabular}
end{document}
- chemformula 宏包 :编写化学式
-
enumerate 编号
documentclass{article} begin{document} begin{enumerate} item aaa item bbb item ccc end{enumerate} end{document}
-
itemize 不编号
documentclass{article} begin{document} begin{itemize} item aaa item bbb item ccc end{itemize} end{document}
- description 有标题
- newtheorem 定义定理类环境
newtheorem{thm}{定理}[section]
documentclass{ctexart}
newtheorem{thm}{定理}
begin{document}
begin{thm}
一个定理
end{thm}
end{document}
小节内定理
documentclass{ctexart}
begin{document}
section{节}
newtheorem{thm}{定理}[section]
begin{thm}
一个定理
end{thm}
end{document}
7. 诗歌与引文
- verse
- quote
- quotation
- verb 命令
verb |#include|
- verbatim
documentclass{ctexart}
begin{document}
begin{verbatim}
#include
int main(){
puts("hello world.");
}
end{verbatim}
end{document}
- 使用listings宏包
documentclass{ctexart}
usepackage{listings}
usepackage{color}
begin{document}
lstset{
basicstyle=ttfamily,
stringstyle=color{blue}
}
begin{lstlisting}[language=C]
#include
int main(){
puts("hello world.");
}
end{lstlisting}
end{document}
- minted 宏包
- clrscode 宏包(算法导论)
documentclass{ctexart}
usepackage{clrscode}
begin{document}
begin{codebox}
Procname{$proc{Merge-Sort}(A,p,r)$}
li If $p
- algorithm2e 宏包
- algorithmicx宏包的algpseudocode格式
10. 表格
-
画表格 – 使用tabular环境
自动生成表格代码工具:https://www.tablesgenerator.com/latex_tables
documentclass{ctexart}
begin{document}
begin{table}[]
begin{tabular}{rr}
输入 & 输出 \
-2 & 4 \
0 & 0 \
2 & 4
end{tabular}
end{table}
end{document}
-
表格宏包
- 单元格处理: multirow、makecell
- 长表格:longtable、xtab
- 定宽表格:xtabular
- 表线控制:booktabs、diagbox、arydshln
- 表列格式:array
- 综合应用:tabu
11. 插图
- graphicx宏包 ---- includegraphics命令
includegraphics[width=2cm]{1.jpg}
- 代码画图 ---- TikZ宏包
12. 浮动体
为了让图像或表格等可以单独出来,而不是跟在文字后面
- figure 环境
- table 环境
- 其他环境可以使用float宏包得到
- 浮动体的标题用caption命令得到,自动标号(写在浮动体end下方
13. 自动化工具
-
目录 .toc 目录文件
tableofcontents 自动生成目录
-
交叉引用 .aux 辅助文件
为了在下文引用公式或其他引用体时,引用的标号会随着公式位置的更改而变化,从而省去了重新改写编号的麻烦
-
hyperref 宏包:PDF的链接与书签
-
参考文献
-
设置文献引用格式 bibliographystyle{plain}
- .bst格式,如plainnat, gbt7714-plain(中文)
- natbib,支持作者-年格式方式编号
- 利用custom-bib产生定制的格式文件
- biblatex+Biber:文献处理的新方式
-
中间引用 cite{key},通过key来找到.bib文件中对应的文献
-
输出参考文献 bibliography{math} (math为论文数据库文件,如math.bib,名字可自行取)
.bib文件中每一个参考文献的条目都可以直接使用网站所给出的BibTex格式
-
示例(ps:引用文献是我随便找的,别在意,hhhh)
% main.tex
documentclass{article}
bibliographystyle{plain}
begin{document}
hello world! cite{rawsthorn2014hello}
bibliography{hello}
end{document}
% hello.bib
@book{rawsthorn2014hello,
title={Hello world: where design meets life},
author={Rawsthorn, Alice},
year={2014},
publisher={Abrams}
}
三、设计文档格式
1. 基本原则
格式与内容分离,不用在意细节
只使用与内容相关的命令和环境!
2. 使用宏包
-
作用
- 实现代码重用
- 实现复杂代码
-
问题
- 兼容性差
-
使用原则:
-
尽量不造轮子
-
尽量排除不需要的宏包
3. 格式控制功能
1. 字体
- rmfamily, textrm{…} 罗马字体,有衬线
- sffamilly, textsf{…} 无衬线字体
- ttfamily, texttt{…} 打印机字体
2. 字号
- 英文字号
- Huge
- LARGE
- Large
- large
- normalsize
- small
- footnotesize
- scriptsize
- tiny
- 中文字号
- zihao{5}
- zihao{-3} 小三号
3. 对齐
- centering
- raggedleft
- raggedright
4. 空白间距
- hspace{2cm} 水平
- vspace{3mm} 垂直
5. 版面布局
- geometry 宏包
- fancyhdr 宏包
6. 分页断行
- linebreak 当自动断行效果不好时手动断行
- \ 分行,一般正文中不使用,是在表格中使用
- pagebreak 手动分页
- newpage 另起一页
- clearpage 另起一页,保证前面浮动的图表也画出来
- cleardoublepage 设计书籍格式,另起一章
7. 盒子
用来做图表,或需要组合的东西
- mbox{内容} 一行盒子
- parbox{4em}{内容} 多行盒子,当内容超过第一个参数时换行
- minipage 环境,与parbox类似
4. 格式应用于文档
1. 在导言区单独设置格式
- 直接设置相关参数
- parindent 段首的缩进量
- parskip 两段之间的垂直间距
- linespread 几倍行距,默认为一倍行距
- pagestyle 设置页眉页脚格式
- 修改部分命令定义
- thesection 修改章节中节的编号的输出格式,如阿拉伯数字,汉字,ABC等
- labelenumi 修改有序列表中编号格式
- descriptionlabel
- figurename
- 利用工具宏包完成设置
- 使用ctex宏包设置中文格式
- 使用tocloft宏包设置目录格式
2. 利用自定义命令和环境
提供了更具意义的名字,同时为未来的修改和扩充提供条件
示例
documentclass{ctexart}
newcommandprg[1]{textsf{#1}}
begin{document}
程序 prg{sort} 很有用
end{document}
documentclass{ctexart}
newcommandprg[1]{textbf{Huge #1}}
begin{document}
程序 prg{sort} 很有用
end{document}
注意:
- 直接修改输出格式的命令应该放在导言区,而避免放在正文部分
3. 章节标题
ctex宏包及文档类
- 中文用ctexset
- 西文用titlesec
4. 浮动标题
- caption 宏包
5. 列表环境
- enumitem宏包
- 示例
- 原始列表
documentclass{ctexart}
begin{document}
文字文字文字文字文字文字文字文字
文字文字文字文字文字文字文字文字
文字文字文字文字文字文字文字文字
文字文字文字文字文字文字文字文字
begin{enumerate}
item 文字文字文字
item 文字文字文字
item 文字文字文字
item 文字文字文字
end{enumerate}
end{document}
- 改变间距后的列表
documentclass{ctexart}
usepackage{enumitem}
setlist{nosep}
begin{document}
文字文字文字文字文字文字文字文字
文字文字文字文字文字文字文字文字
文字文字文字文字文字文字文字文字
文字文字文字文字文字文字文字文字
begin{enumerate}
item 文字文字文字
item 文字文字文字
item 文字文字文字
item 文字文字文字
end{enumerate}
end{document}



