A Brief Note of
详细文档可见 一份(不太)简短的 介绍,注意此处仅是 命令速查,而非教程。
基本概念
基本的源代码形如下文,一些熟悉的概念不一一列举。
\documentclass{article}
\begin{document}
``Hello world!'' from \LaTeX.
\end{document}
调整参数
\documentclass[11pt,twoside,a4paper]{article}
默认为 letterpaper
宏包
\usepackage{tabularx, makecell, multirow}
用到的文件
.sty
宏包文件。宏包的名称与文件名一致。
.cls
文档类文件。文档类名称与文件名一致。
.bib
BIBTEX 参考文献数据库文件。
.log
排版引擎生成的日志文件,供排查错误使用。
在源代码里插入文件
\include{⟨filename⟩}
\include
在读入 ⟨filename⟩
之前会另起一页。有的时候我们并不需要这样,而是用 \input
命令,它纯粹是把文件里的内容插入。
\includeonly
命令来组织文件,用于导言区,指定只载入某些文件。
导言区使用了 \includeonly
后,正文中不在其列表范围的 \include
命令不会起效。
最后介绍一个实用的工具宏包 syntonly
。加载这个宏包后,在导言区使用 \syntaxonly
命
令,可令 LATEX 编译后不生成 DVI 或者 PDF 文档,只排查错误,编译速度会快不少:
\usepackage{syntonly}
\syntaxonly
排版文字
排版引擎,是编译源代码并生成文档的程序。当前较为方便的中文排版解决方案基于 .
中文支持
CJK 宏包对中文字体的支持比较麻烦,已经不再推荐直接使用。
ctex 宏包本身用于配合各种文档类排版中文,包括 ctexart、ctexrep、ctexbook 等。
\documentclass{ctexart}
\begin{document}
在\LaTeX{}中排版中文。
汉字和English单词混排,通常不需要在中英文之间添加额外的空格。
当然,为了代码的可读性,加上汉字和 English 之间的空格也无妨。
汉字换行时不会引入多余的空格。
\end{document}
注意源代码须保存为 UTF-8 编码,并使用 或 命令编译。
空格
空格键和 Tab 键输入的空白字符视为“空格”。连续的若干个空白字符视
为一个空格。一行开头的空格忽略不计。
行末的换行符视为一个空格;但连续两个换行符,也就是空行,会将文字分段。多个空行被视为一个空行。也可以在行末使用 \par
命令分段。
一定注意断行和分段的区别。
注释 % 号
特殊字符用反斜线转义,但是 \ 被直接定义成了手动换行的命令,输入反斜线就需要用 \textbackslash
单引号 ‘ 和 ’ 分别用 ` 和 ' 输入;双引号 “ 和 ” 分别用 `` 和 '' 输入。
连字号
连字号 ### 用来组成复合词;短破折号 – 用来连接数字表示范围;长破折号 — 用来连接单词,语义上类似中文的破折号。
省略号
\ldots
命令表示省略号。\dots
与 \ldots
命令等效。
空格
我们可以使用字符 ~ 输入一个不会断行的空格,例如:
Donald~E.~Knuth
手动断行
一是 \ 可以带可选参数 ⟨length⟩
,用于在断行处向下增加垂直间距,而 \newline
不带可选参数;二是 \ 也在表格、公式等地方用于换行,而 \newline
只用于文本段落中。带星号的 \ 表示禁止在断行处分页。
\\[⟨length⟩] \\*[⟨length⟩]
\newline
手动断页
断页的命令有两个:
\newpage
\clearpage
在双栏排版模式中 \newpage
起到另起一栏的作用,\clearpage
则能够另起一页。
\newline
和 \newpage
会在断行/断页位置填充适当的间距,但 \linebreak[<n>]
和 \pagebreak[<n>]
不能,使用这些命令强行断行/断页可能会制造出糟糕的排版效果。
文档元素
章节标题
\chapter{⟨title⟩} \section{⟨title⟩} \subsection{⟨title⟩}
\subsubsection{⟨title⟩} \paragraph{⟨title⟩} \subparagraph{⟨title⟩}
其中 \chapter
只在 report 和 book 文档类有定义。这些命令生成章节标题,并能够自动编号。
\part
命令,用来将整个文档分割为大的分块,但不影响 \chapter
或 \section
等的编号。
生成目录 \tableofcontents
附录
所有标准文档类都提供了一个 \appendix
命令将正文和附录分开,使用后,最高一级章节改为使用拉丁字母编号,从 A 开始。
前言后记
\frontmatter
前言部分,页码使用小写罗马数字;其后的 \chapter
不编号。
\mainmatter
正文部分,页码使用阿拉伯数字,从 1 开始计数;其后的章节编号正常。
\backmatter
后记部分,页码格式不变,继续正常计数;其后的 \chapter
不编号。
标题页
\title{⟨title⟩} \author{⟨author⟩} \date{⟨date⟩}
\maketitle
命令生成一个简单的标题页。
article 文档类的标题默认不单独成页,而 report 和 book 默认单独成页。
在 \title
、\author
等命令内可以使用 \thanks
命令生成标题页的脚注,用 \and
隔开多个人名。
\title{Test title}
\author{ Mary\thanks{E-mail:*****@***.com}
\and Ted\thanks{Corresponding author}
\and Louis}
\date{\today}
示例:
\documentclass{book}
% 导言区,加载宏包和各项设置,包括参考文献、索引等
\usepackage{makeidx} % 调用 makeidx 宏包,用来处理索引
\makeindex % 开启索引的收集
\bibliographystyle{plain} % 指定参考文献样式为 plain
\begin{document}
\frontmatter % 前言部分
\maketitle % 标题页
\include{preface} % 前言章节 preface.tex
\tableofcontents
\mainmatter % 正文部分
\include{chapter1} % 第一章 chapter1.tex
\include{chapter2} % 第二章 chapter2.tex
...
\appendix % 附录
\include{appendixA} % 附录 A appendixA.tex
...
\backmatter % 后记部分
\include{epilogue} % 后记 epilogue.tex
\bibliography{books} % 利用 BibTeX 工具从数据库文件 books.bib 生成参考文献
\printindex % 利用 makeindex 工具生成索引
\end{document}
交叉引用
在能够被交叉引用的地方,如章节、公式、图表、定理等位置使用 \label
命令:
\label{⟨label-name⟩}
之后可以在别处使用 \ref
或 \pageref
命令,分别生成交叉引用的编号和页码:
\ref{⟨label-name⟩} \pageref{⟨label-name⟩}
注意在使用不记编号的命令形式(\section*
等)时不要使用 \label
命令,否则生成的引用编号不正确。
脚注
使用 \footnote
命令可以在页面底部生成一个脚注:
\footnote{⟨footnote⟩}
在表格内先使用 \footnotemark
为脚注计数,再在合适的位置用 \footnotetext
生成脚注:
\begin{tabular}{l}
\hline
“天地玄黄,宇宙洪荒。日月盈昃,辰宿列张。”\footnotemark \\
\hline
\end{tabular}
\footnotetext{表格里的名句出自《千字文》。}
边注
\marginpar[⟨left-margin⟩]{⟨right-margin⟩}
\marginpar{\footnotesize 边注较窄,不要写过多文字,最好设置较小的字号。}
有序列表
\begin{enumerate}
\item …
\end{enumerate}
最多嵌套四层。
无序列表 itemize
均可以自定义符号,如 \item[+] Something
description
关键字环境 的用法与以上两者类似,不同的是 \item
后的可选参数用来写关键字,以粗体显示,一般是必填的:
\begin{description}
\item[Enumerate] Numbered list.
\item[Itemize] Non-numbered list.
\end{description}
对齐
、 和 环境分别用于生成居中、左对齐和右对齐的文本环境。
\begin{center} … \end{center}
\begin{flushleft} … \end{flushleft}
\begin{flushright} … \end{flushright}
以下命令直接改变文字的对齐方式:
\centering \raggedright \raggedleft
区别在于后者不在上下文产生额外间距。
引用
用于引用较短的文字,首行不缩进; 用于引用若干段文字。
Francis Bacon says:
\begin{quote}
Knowledge is power.
\end{quote}
摘要
摘要环境 默认只在标准文档类中的 article 和 report 文档类可用,一般用于紧跟\maketitle
命令之后介绍文档的摘要。
代码
\begin{verbatim}
#include <iostream>
int main()
{
std::cout << "Hello, world!"
<< std::endl;
return 0;
}
\end{verbatim}
\begin{verbatim*}
会额外强调空格(所有空格处显示一个特殊字符)。
简短的代码
\verb⟨delim⟩⟨code⟩⟨delim⟩
\verb|\LaTeX|
⟨delim⟩
习惯用 |。
表格
排版表格最基本的 环境用法为:
\begin{tabular}[⟨align⟩]{⟨column-spec⟩}
⟨item1⟩ & ⟨item2⟩ & … \\
\hline
⟨item1⟩ & ⟨item2⟩ & … \\
\end{tabular}
直接使用 环境的话,会和周围的文字混排。
但是通常情况下 环境很少与文字直接混排,而是会放在 浮动体环境中,并用 \caption
命令加标题。
列格式
l/c/r
单元格内容左对齐/居中/右对齐,不折行
p{⟨width⟩}
单元格宽度固定为 ⟨width⟩
,可自动折行
| 绘制竖线
\begin{tabular}{@{} r@{:}lr @{}}
\hline
1 & 1 & one \\
11 & 3 & eleven \\
\hline
\end{tabular}
如以下两种写法是等效的:
\begin{tabular}{|c|c|c|c|c|p{4em}|p{4em}|}
\begin{tabular}{|*{5}{c|}*{2}{p{4em}|}}
表格横线
\cline{⟨i⟩-⟨j⟩}
用来绘制跨越部分单元格的横线:
\begin{tabular}{|c|c|c|}
\hline
4 & 9 & 2 \\ \cline{2-3}
3 & 5 & 7 \\ \cline{1-1}
8 & 1 & 6 \\ \hline
\end{tabular}
三线表
% \usepackage{booktabs}
\begin{tabular}{cccc}
\toprule
& \multicolumn{3}{c}{Numbers} \\
\cmidrule{2-4}
& 1 & 2 & 3 \\
\midrule
Alphabet & A & B & C \\
Roman & I & II& III \\
\bottomrule
\end{tabular}
横向合并单元格
\begin{tabular}{|c|c|c|}
\hline
1 & 2 & Center \\ \hline
\multicolumn{2}{|c|}{3} &
\multicolumn{1}{r|}{Right} \\ \hline
4 & \multicolumn{2}{c|}{C} \\ \hline
\end{tabular}
没错,还可以顺便用于修改某一个单元格的列格式。
纵向合并单元格
% \usepackage{multirow}
\begin{tabular}{ccc}
\hline
\multirow{2}{*}{Item} &
\multicolumn{2}{c}{Value} \\
\cline{2-3}
& First & Second \\ \hline
A & 1 & 2 \\ \hline
\end{tabular}
这里的 * 表示使用自然宽度,也可以填入具体指定的宽度。
嵌套表格
注意要用 \multicolumn
命令配合 @{} 格式把单元格的额外边距去掉,使得嵌套的表格线能和外层的表格线正确相连:
\begin{tabular}{|c|c|c|}
\hline
a & b & c \\ \hline
a & \multicolumn{1}{@{}c@{}|}
{\begin{tabular}{c|c}
e & f \\ \hline
e & f \\
\end{tabular}}
& c \\ \hline
a & b & c \\ \hline
\end{tabular}
如果不需要为“拆分的单元格”画线,并且只在垂直方向“拆分”的话,makecell 宏包提供的 \makecell
命令是一个简单的解决方案:
% \usepackage{makecell}
\begin{tabular}{|c|c|}
\hline
a & \makecell{d1 \\ d2} \\
\hline
b & c \\
\hline
\end{tabular}
插入图片
在调用了 graphicx 宏包以后,就可以使用 \includegraphics
命令加载图片了:
\includegraphics[⟨options⟩]{⟨filename⟩}
另外 graphicx 宏包还提供了 \graphicspath
命令,用于声明一个或多个图片文件存放的目录,使用这些目录里的图片时可不用写路径:
% 假设主要的图片放在 figures 子目录下,标志放在 logo 子目录下
\graphicspath{{figures/}{logo/}}
width=⟨width⟩
将图片缩放到宽度为 ⟨width⟩
height=⟨height⟩
将图片缩放到高度为 ⟨height⟩
scale=⟨scale⟩
将图片相对于原尺寸缩放 ⟨scale⟩
倍
angle=⟨angle⟩
将图片逆时针旋转 ⟨angle⟩
度
当 graphicx 宏包或文档类指定 选项时,图片将不会被实际插入,取而代之的是一个包含文件名的与原图片等大的方框。
盒子
\fbox
和 \framebox
让我们可以为水平盒子添加边框。使用的语法与 \mbox
和 \makebox
一模一样:
\fbox{Test some words.}\\
\framebox[10em][r]{Test some words.}
垂直盒子和标尺盒子忽略。
浮动体
习惯上 里放图片, 里放表格。
\begin{table}[⟨placement⟩]
…
\end{table}
h 当前位置(代码所处的上下文)
t 顶部
b 底部
p 单独成页
! 在决定位置时忽视限制
如 hbp 允许浮动体排版在当前位置、底部或者单独成页。 和 浮动体的默认设置为 tbp。
H 位置参数,不与 htbp 及 ! 混用。使用 H 位置参数时,会取消浮动机制,将浮动体视为一般的盒子插入当前位置。
浮动体的标题
\caption
的用法非常类似于 \section
等命令,可以用带星号的命令 \caption*
生成不带编号的标题,也可以使用带可选参数的形式 \caption[…]{…}
,使得在目录里使用短标题。\caption
命令之后还可以紧跟 \label
命令标记交叉引用。
和 两种浮动体分别有各自的生成目录的命令:
\listoftables
\listoffigures
它们类似 \tableofcontents
生成单独的章节。
并排和子图表
在一个浮动体里面放置多张图。
\begin{figure}[htbp]
\centering
\includegraphics[width=...]{...}
\qquad
\includegraphics[width=...]{...} \\[...pt]
\includegraphics[width=...]{...}
\caption{...}
\end{figure}
由于标题是横跨一行的,用 \caption
命令为每个图片单独生成标题就需要借助前文提到的\parbox
或者 minipage
环境,将标题限制在盒子内。
\qquad
\begin{minipage}{...}
\centering
\includegraphics[width=...]{...}
\caption{...}
\end{minipage}
当我们需要更进一步,给每个图片定义小标题时,就要用到 subcaption 宏包的功能了。
\begin{figure}[htbp]
\centering
\begin{subfigure}{...}
\centering
\includegraphics[width=...]{...}
\caption{...}
\end{subfigure}
\qquad
\begin{subfigure}{...}
\centering
\includegraphics[width=...]{...}
\caption{...}
\end{subfigure}
\end{figure}
数学公式
如果从中学起就在洛谷和知乎写数学公式,对于这一部分的内容应当相当熟悉,故不再赘述。
排版样式
字体的各种属性相互独立(“正交”),用户可以改变字体的大小,而仍然保留字体原有的粗体或者斜体的特性。
字号
\fontsize
用于设定任意大小的字号,单位为 pt。⟨size⟩
为字号,⟨base line-skip⟩
为基础行距。
\rmfamily \textrm{…} roman 衬线字体(罗马体)
\sffamily \textsf{…} sans serif 无衬线字体
\ttfamily \texttt{…} typewriter 等宽字体
\mdseries \textmd{…} medium 正常粗细(中等)
\bfseries \textbf{…} bold face 粗体
\upshape \textup{…} upright 直立体
\itshape \textit{…} italic 意大利斜体
\slshape \textsl{…} slanted 倾斜体
\scshape \textsc{…} SMALL CAPS 小型大写字母
\em \emph{…} emphasized 强调,默认斜体
\normalfont \textnormal{…} normal font 默认字体
\tiny tiny font
\scriptsize very small font
\footnotesize quite small font
\small small font
\normalsize normal font
\large large font
\Large larger font
\LARGE very large font
\huge huge
\Huge largest
行距
导言区使用 \linespread
命令。缺省的基础行距是 1.2 倍字号大小(参考 \fontsize
命令),因此使用 \linespread{1.5}
意味着最终行距为 1.8 倍的字号大小。
辅助功能
基本的参考文献和引用
提供的参考文献和引用方式比较原始,需要用户自行书写参考文献列表(包括格式),因此较难直接使用。
最基本的 \cite
命令用于在正文中引用参考文献:
\cite{⟨citation⟩}
BIBTEX 数据库
以 作为扩展名,其内容是若干个文献条目,每个条目的格式形如:
@article{Alice13,
title = {Demostration of bibliography items},
author = {Alice Axford and Bob Birkin and Charlie Copper and Danny Dannford},
year = {2013},
month = {Mar},
journal = {Journal of \TeX perts},
volume = {36},
number = {7},
pages = {114-120}}
多数时候,我们无需自己手写 BIBTEX 文献条目。从 Google Scholar 或者期刊/数据库的网站上都能够导出 BIBTEX 文献条目。
article 学术论文,必需字段有 author, title, journal, year; 可选字段包括 volume, number,
pages, doi 等;
book 书籍,必需字段有 author/editor, title, publisher, year; 可选字段包括 volume/number,
series, address 等;
incollection 论文集中的一篇,必需字段有 author, title, booktitle, publisher, year; 可选字段包括 editor, volume/number, chapter, pages, address 等;
inbook 书中的一章,必需字段有 author/editor, title, chapter/pages, publisher, year; 可选字段包括 volume/number, series, address 等。
BIBTEX 样式
BIBTEX 提供了几个预定义的样式,如 等。如果使用期刊模板的话,可能会提供自用的样式。样式文件以 为扩展名。
使用样式文件的方法是在源代码内(一般在导言区)使用 \bibliographystyle
命令:
\bibliographystyle{⟨bst-name⟩}
这里 ⟨bst-name⟩
为 样式文件的名称,不要带 扩展名。
通常只列出用了 \cite
命令引用的那些。如果需要列出未被引用的文献,则需要 \nocite{⟨citation⟩}
命令;而 \nocite{*}
则让所有未被引用的文献都列出。
\documentclass{article}
\bibliographystyle{plain}
\begin{document}
\section{Some words}
Some excellent books, for example, \cite{citation1}
and \cite{citation2} \ldots
\bibliography{books}
\end{document}
编译过程
xelatex demo
bibtex demo
xelatex demo
xelatex demo
biblatex 宏包
biblatex 宏包也因其对 UTF-8 和中文参考文献的良好支持,被国内较多模板采用。
形如:
% File: egbibdata.bib
@book{caimin2006,
title = {UML 基础和 Rose 建模教程},
address = {北京},
author = {蔡敏 and 徐慧慧 and 黄柄强},
publisher = {人民邮电出版社},
year = {2006},
month = {1}
}
% File: demo.tex
\documentclass{ctexart}
% 使用符合 GB/T 7714-2015 规范的参考文献样式
\usepackage[style=gb7714-2015]{biblatex}
% 注意加 .bib 扩展名
\addbibresource{egbibdata.bib}
\begin{document}
见文献\cite{caimin2006}。
\printbibliography
\end{document}
编译顺序:
xelatex demo
biber demo
xelatex demo
xelatex demo
索引和 makeindex 工具
\usepackage{makeidx}
\makeindex
颜色
调用 color 或 xcolor 宏包后,我们就可以用如下命令切换颜色:
\color[⟨color-mode⟩]{⟨code⟩}
\color{⟨color-name⟩}
方法一:
\large\sffamily
{\color[gray]{0.6}
60\% 灰色} \\
{\color[rgb]{0,1,1}
青色}
方法二(前提是已经定义了颜色名称):
\large\sffamily
{\color{red} 红色} \\
{\color{blue} 蓝色}
基本的 8 种颜色名称
black red green blue
white cyan magenta yellow
xcolor 额外可用的颜色名称:
darkgray gray lightgray
brown olive orange lime
purple teal violet pink
颜色混合和互补:
\large\sffamily
{\color{red!40} 40\% 红色}\\
{\color{blue}蓝色
\color{blue!50!black}蓝黑
\color{black}黑色}\\
{\color{-red}红色的互补色}
使用超链接
为减少可能的冲突,习惯上将 hyperref 宏包放在其它宏包之后调用。
\url{https://wikipedia.org} \\
\nolinkurl{https://wikipedia.org} \\
\href{https://wikipedia.org}{Wiki}
默认的超链接在文字外边加上一个带颜色的边框(在打印 PDF 时边框不会打印),可指定 colorlinks 参数修改为将文字本身加上颜色,或修改 pdfborder 参数调整边框宽度以“去掉”边框;hidelinks 参数则令超链接既不变色也不加边框。
\hypersetup{hidelinks}
% or:
\hypersetup{pdfborder={0 0 0}}
一些 PDF 阅读器会为 URL 文本自动加上超链接。
绘图功能
不用学tikz,不用写代码,所见即所得:Mathcha 你值得拥有。