latex 语法备忘

  • latex 语法 备忘

  • 资料来源:

    https://liam.page/2014/09/08/latex-introduction/

  • 更新

    1
    2021.04.25 初始

导语

请注意这不是 latex 语法入门!

只是记录自己一直用,但是一直忘的语法…

注意:

  • 有些公式语法是写 markdown 时用的,可能单纯写 latex 会有区别.
  • 可能是 next 主题的 latex 渲染有小问题.有的公式没有正确渲染,不过暂时没时间修…

基础

可以说 markdown 是另一种简化的 latex.某种程度上非常相似.

开始前

  • latex 文件后缀是 .tex
  • 大小写敏感
  • 编码最好是 UTF-8.
  • vscode 代码格式化可用.但是警告提示很烦人.

helo world!

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

\usepackage{cite}

\begin{document}

中文文档类测试。

{\songti 这里是宋体显示}

\end{document}

格式

\begin{document}\end{document} 之间是正文,之前是导言区.

导言区

  • \documentclass: 文章的开始的控制序列,声明编码/文档类等等.
  • usepackage: 是论文引用的宏包.基本上 latex 实现特定格式都需要某些宏包.

正文

  • 开始于 \begin{document},结束于 \end{document}.
  • \songti 可以使用系统字体.

组织

文章内容的组织.

首页

1
2
3
4
5
6
7
8
\documentclass[UTF8]{ctexart}
\title{你好,world!}
\author{Liam}
\date{\today}
\begin{document}
\maketitle
你好,world!
\end{document}
  • title: 标题
  • author: 作者,有更复杂的格式.
  • date: 日期
  • maketitle: 显示目录

段落

latex 定义了 5 种层级(不同模板可能不同,例如 ei 的模板就没有 subsubsection)

  • \section{·}
  • \subsection{·}
  • \subsubsection{·}
  • \paragraph{·}
  • \subparagraph{·}

条目

1
2
3
4
\begin{itemize}
\item
\item
\end{itemize}
  • 默认是黑点,无编号.
  • 可以定制,item[*] 出来就是 *
1
2
3
4
\begin{enumerate}[1)]
\item a
\item b
\end{enumerate}
  • 带编号的条目.
  • 样式通过 1) 定制,latex 可自动识别.
    • A,a,I,i,1.
    • 可以是方括号 ],但是必须引用 \usepackage{enumerate}.
1
2
3
4
\begin{description}
\item[item a] a
\item[item b] b
\end{description}
  • [] 内词会直接显示出来.
  • 完美定制.

其他

文章内容非常庞大,还可以分文件,最后在主文件引用.

公式

大括号 \begin{cases} \end{cases}

$$
T(n) =
\begin{cases}
& T(0) + T(n-1) + \Theta(n) , x_1 发生 \
& T(1) + T(n-2) + \Theta(n), x_2 发生\
& …\
& T(n-1) + T(1) + \Theta(n),x_n 发生
\end{cases}
$$

等号对齐,在 \begin{aligned} \end{aligned} 中间使用 & \\ 对齐与换行.

$$
\begin{aligned}
E[x_k] & = 0Pr{x_k = 0} + 1Pr{x_k=1} \
&= Pr{x_k=1} \
&= \frac{1}{n}
\end{aligned}
$$

公式序号,这个 obsidian 还不支持..暂时手动标注$

向下取整 \lfloor x \rfloor

$$
\lfloor x \rfloor
$$

向上取整 \lceil x \rceil

$$
\lceil x \rceil
$$

取模 mod

1
2
\bmod
\pmod

图表

参考LaTeX排版札记:part 4—插入图片(并排显示、自定义编号)

导言区

1
2
3
\usepackage{graphicx} %插入图片的宏包
\usepackage{float} %设置图片浮动位置的宏包
\usepackage{subfigure} %插入多图时用子图显示的宏包

图片横排编号,无子图,跨双栏.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
\begin{figure*}
\centering %图片全局居中
%并排几个图,就要写几个minipage
\begin{minipage}[b]{0.32\textwidth} %所有minipage宽度之和要小于1,否则会自动变成竖排
\centering %图片局部居中
\includegraphics[width=0.8\textwidth]{fig5.png} %此时的图片宽度比例是相对于这个minipage的,不是全局
\caption{Link failure detection and handling}
\label{Fig}
\end{minipage}
\begin{minipage}[b]{0.32\textwidth}
\includegraphics[width=0.8\textwidth]{fig6.png}
\caption{Link recovery detection and handling}
\label{Fig}
\end{minipage}
\begin{minipage}[b]{0.32\textwidth}
\includegraphics[width=0.8\textwidth]{fig7.png}
\caption{Link state update handling.}
\label{Fig}
\end{minipage}
\end{figure*}
  • figure*: 跨过双栏.
  • 所有 \begin{minipage}[b]{0.32\textwidth} 的数字之和小于 1(不是小于等于),超过 1 自动变成竖排.

表格过宽

1
2
3
4
5
\resizebox{linewidth}{!}{
\begin{tabular}...
....
....
\end{tabular}
  • \linewidth: 目前环境宽度
  • \hsize: tex 定义的长度,分栏时,为栏宽度.
  • \textwidth: 一行文字宽度,固定值.
  • \columnwidth: 分栏栏宽度
  • {!}: 是自适应宽度

LaTeX中不同宽度(width)的含义

参考文献