LaTeX 笔记 - 在 VSCode 编写 LaTeX 文档的一个尝试

本文最后更新于 2025年4月21日 晚上

嗐,好久不见,没想到上次写博文已经是在高考前的事了。

大家都说,「上了大学就轻松了,高考就尽力吧」,现在想想,我 tm 真的是谢谢你

目前上了一个半学期,一路下来个人感觉比高考还忙。最最离谱的是这个学期我游戏打得比考高考那段时间还少(虽然原因很大是我电子阳痿了

不过用俩个字总结就是 – 忙,累

忙,主要是作业太多了。上个学期只能说还行,但这个学期感觉已经要原地爆炸,主要还是因为上了门线性代数。

线代难吗?不难,但是带证明的线代只能说谁上谁爆炸(说实话证明也还行,但这课的作业量真的就。。。俩星期一份作业,每份作业 20 页起步。。。

累,也是因为作业,我高考都没试过自习室从下午 3 点一坐坐到晚上 11 点,但现在几乎是常态。

如果是纯计算的作业也就还好,但要证明的话真的就一天最多俩题(当然,俩者都同样耗费脑力,但证明所花的时间就比计算多多了

虽然上面几句基本上都是我在倒垃圾,不过这也是为啥我开始用 LaTeX 的缘由。

冬季学期我上了门 Introduction to Mathematical Proofs,基本上就是从零开始学数学证明,什么是 Theorem (定理),以及如何证明。

这个学期我就延续这门课,选择了一个带证明的线性代数(万恶的开头,俩星期 10 题证明就是这门课 (#`-_ゝ-)

上学期那会我还在手写证明,因为证明一般都不会很长,俩题证明也就大概 3 - 4 页左右,加上用的是 iPad,小修小改也十分方便。但是这个学期的作业量我是十分的无语,第一份作业我依旧是手写证明,但也是我第一次感受到写数学作业比中,英文作文还难受,写完一题手就是酸的,纯纯梦回高考写作

于是在好友的推荐下,我尝试了使用 LaTeX。我是从 Overleaf 入坑的,一个线上的 LaTeX 编辑平台,和云文档概念几乎一摸一样,编写文件以及生成 pdf 都是在他的服务器上完成。

入坑倒不错,但这就不得不提及我的云文档恐惧症了。当然,云文档的随开随用确实是十分方便,但他的服务完全不在你的控制下,所有文件理论上都不属于你的…(云服务恐惧症发作现象,请放心跳过

总而言之,在我的一番调查后发现,其实编辑 LaTeX 完全可以在本地完成。

LaTeX 相当于一个编译语言,你先编写 .tex 文件,然后通过一个 pdf 生成程序(TeX Live)按照指定的形式生成 pdf

VSCode 就有款保姆级插件,利用 VSCode 本身作为一个强大的文本编辑器,以及其文件预览功能(预览 pdf),实现非常舒服的 LaTeX 写作体验。

这个插件,就是大名鼎鼎的 LaTeX Workshop

对了,你可能会在这里发现我:语无伦次、明显的中文语病、错误用词等。

这是因为我上次用中文写这么长的文章已经是高考那会了,也就语文作文卷写过这么长的,来到加拿大这边就几乎没写过中文,笔画都要忘了(

Anyways,有任何上述问题都请见谅 <(_ _)>

在本地编译 LaTeX

安装 LaTeX Workshop 插件以及 TeX Live

首先,我默认各位对 VSCode 有一定程度的了解。这不是一个手把手教程,只是我的一个个人经历分享。

LaTeX Workshop 本身其实只是一个让用户能在 VSCode 里方便的编写 LaTeX 文件以及生成 pdf 的插件而已,本身并不具备解析 .tex 文件的能力

至于将 .tex 文件合成为 .pdf 文件,就需要专门的 LaTeX 软件,例如 TeX Live.

当然,你也有其他选择,例如体积更小的 TinyTeX 或者 MiKTeX(对比之下,TeX Live 大小为 8GB)

TinyTeX 体积小的原因是他的包更少,如果要用其他包的话得自行下载

MiKTeX 也是同理,不过如果要和 LaTeX Workshop 配合食用需要另外下载 Perl,详情请见 LaTeX Workshop 的 Wiki

安装完成后,请记得将软件添加到系统的环境变量里,也就是 PATH (此处我用 Windows 系统举例,基于 Unix 的系统我就不清楚了)

如果没有添加到环境变量里,LaTeX Workshop 可能无法将 .tex 文件合成为 .pdf 文件。

现在只要在 VSCode 里安装好 LaTeX Workshop 的插件就完成了

编译你的第一个 LaTeX 文件

现在我以 ~/example/ 为例

创造新的 VSCode Project

1
2
cd ~/example
code . -n :: -n 代表新建项目

进入 VSCode 界面后,新建一个空的 .tex 文件,这里我就用 example.tex 为例

打开 example.tex,这其实就是一个普通的纯文本格式

在文件开头写上如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
\documentclass[12pt]{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{amsmath,amsthm,amssymb} % Math packages

\begin{document}

\title{Testing document}
\date{}
\author{}

\maketitle


\end{document}

一堆看不懂的命令?先别急,下面来一句一句分析

首先,\ 在 LaTeX 里表示指令,任何 \ 开头的都不算是纯文本

  • \documentclass[12pt]{article} 是文件头,用来表示这份 LaTeX 文件所用的字体大小(12pt),以及其文件类型(这里是 article 类)

  • \usepackage{xxx} 表示的是用什么渲染包,不同的渲染包就会有不同的功能

    • graphicx:渲染图片

    • ams 包:渲染数学相关内容,例如 Theorem,proof 格式,以及数学符号

  • \begin{document} 表示开始 document 渲染,下面要接一个 \end{document},这里 begin 和 end 的用法和 html 的 <body> <\body> 有类似的逻辑。任何在这个区间内的纯文本都会被渲染

  • \title{}, \date{}, \author{} 分别代表这份文件的标题、日期、以及作者。注意这些只是属于文件信息指令,并不会被渲染

  • \maketitle 渲染以上文件信息

  • \end{document} 表示文件渲染结束,通常表文件末尾。任何在 document 区间外的都不会被渲染

基础配置完成,现在可以使用(win 用户)Ctrl + Alt + B 快捷键 build 一下,LaTeX Workshop 插件就会自动执行一系列的 TeX Live 命令为您渲染 / 构建 .pdf 文件。(这个步骤通常用时大概 2 秒)

(若有任何报错,可以在 VSCode 里看一下报错信息,有可能 上一步的 PATH 环境没配置好,或者你的文件里有 Syntax Error)

渲染完成后,可以使用 (win 用户) Ctrl + Alt + V Visit 一下构建完的 example.pdf 文件

就酱,你的第一份本地 LaTeX 文件编译完成 ╰(°▽°)╯

LaTeX 基本语法

这个部分是我对 LaTeX 语法的一个基本收录,通常只围绕 amsmathamsthm,以及 amssymb 这三个基本数学符号库使用

有可能会随时更新 (~ ̄▽ ̄)~

基础格式

注释

所有以 % 符号之后的内容会被视为注释

1
2
3
4
This is a sample sentence
% this is a comment, this line will not be rendered!

This is another sample sentence % this is comment in a same sentence

This is a sample sentence\text{This is a sample sentence}

This is another sample sentence\text{This is another sample sentence}

数学模式

单纯的文字只会被正常渲染,若想要使用数学符号或者公式就得加上 $ 符号,以进入数学模式

Inline math mode(行内数学模式)

1
Let $A$ be some element in the set of $\mathcal{M}_{\mathbf{n} \times \mathbf{n}}(\mathbb{F})$

 Let A be some element in the set of Mn×n(F) \text{ Let $A$ be some element in the set of $\mathcal{M}_{\mathbf{n} \times \mathbf{n}}(\mathbb{F})$ }

$ 会强制把内容渲染为 Display math mode,也就是说会开启一个区域进行展示

1
Test $$A = B + C$$ test

Test

A=B+CA = B + C

test

若想在数学模式区域写正常文字,空格会被忽略

1
$$The space will be ignored$$

ThespacewillbeignoredThe space will be ignored

在数学区域使用 \text{} 指令即可正常展示文字

1
$$\text{Now text with space will render normally!}$$

Now text with space will render normally in math mode!\text{Now text with space will render normally in math mode!}

字体

1
2
3
4
5
6
7
\mathbf{bold}
% or \textbf{bold}

\mathit{italic}
% or \textit{italic}

\underline{underline}

bold\mathbf{bold} % or \textbf{bold}

italic\mathit{italic} % or \textit{italic}

underline\underline{underline}

1
2
3
4
5
\mathbb{ABCDEFGHIJKLMNOPQRSTUVWXYZ}

\mathcal{ABCDEFGHIJKLMNOPQRSTUVWXYZ}

\mathfrak{ABCDEFGHIJKLMNOPQRSTUVWXYZ}

ABCDEFGHIJKLMNOPQRSTUVWXYZ\mathbb{ABCDEFGHIJKLMNOPQRSTUVWXYZ}

ABCDEFGHIJKLMNOPQRSTUVWXYZ\mathcal{ABCDEFGHIJKLMNOPQRSTUVWXYZ}

ABCDEFGHIJKLMNOPQRSTUVWXYZ\mathfrak{ABCDEFGHIJKLMNOPQRSTUVWXYZ}

列表

无序列表

1
2
3
4
\begin{itemize}
\item This is one entry
\item This is another entry
\end{itemize}
  • This is one entry

  • This is another entry

注意,这里的缩进非必要,和众多编程语言一样(Python 除外),缩进并不会影响 LaTeX 渲染

有序列表

1
2
3
4
\begin{enumerate}
\item This is the first entry
\item This is the second entry
\end{enumerate}
  1. This is the first entry

  2. This is the second entry

有序列表可以使用 \setcounter{enumi}{x} 来控制计数器

基于有序列表可以叠加的机制,\setcounter{enumi} 命令中的 i 的数量是指控制的层数

1
2
3
4
5
6
7
8
9
\begin{enumerate}
\item This is the first item
\begin{enumerate}
\setcounter{enumii}{2}
\item This should show item (c)!
\end{enumerate}
\setcounter{enumi}{5}
\item this is the 6th item
\end{enumerate}
  1. This is the first item

    c. This should show item c!

  2. this is the 6th item

注意,enumerate 会用 1, a, i 以此类推来进行不同层级的计数,而并非全用数字

一些例子

Theorem. If n is an integer and n2 is even, then n is itself even.\text{\textbf{Theorem.} \textit{If $n$ is an integer and $n^2$ is even, then $n$ is itself even.}}

Proof. Contrapositives are for cowards, so assume that nn is an integer and n2n^2 is even. Then n2=2kn^2 = 2k for some integer kk, and thus n22k=0n^2 - 2k = 0. Behold:

n=n+(n22k)=n(n+1)2kn = n + (n^2 - 2k) = n (n + 1) - 2k

Both n(n+1)n(n + 1) and 2k2k are even, so nn is even too.

1
2
3
4
5
6
7
8
9
\begin{proof}
Contrapositives are for cowards, so assume that $n$ is an integer and $n^2$ is even. Then $n^2 = 2k$ for some integer $k$, and thus $n^2 - 2k = 0$. Behold:

$$
n = n + (n^2 - 2k) = n (n + 1) - 2k
$$

Both $n(n + 1)$ and $2k$ are even, so $n$ is even too.
\end{proof}

个人语法收藏

基本数学

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

\ln(x)

\sin(x) % 三角函数

x^n % superscript

x^{123} % 若 superscript 多于一个字符,就需要加上 `{}`

x_a % subscript

x_{abc} % subscript 同理

x^{abc}_{ijk} % 混用

\infty
  • x\sqrt{x}

  • ln(x)\ln(x)

  • sin(x)\sin(x)

  • xnx^n

  • x123x^{123}

  • xax_a

  • xabcx_{abc}

  • xijkabcx^{abc}_{ijk}

  • \infty

分数

1
2
3
\frac{a}{b} % inline

\dfrac{a}{b} % dispaly

This is inline fraction ab\frac{a}{b}
This is also inline but force into display mode ab\dfrac{a}{b}

数学符号

\alpha α\alpha \eta η\eta \nu ν\nu \tau τ\tau
\beta β\beta \theta θ\theta \xi ξ\xi \upsilon υ\upsilon
\gamma γ\gamma \iota ι\iota o oo \phi ϕ\phi
\delta δ\delta \kappa κ\kappa \pi π\pi \chi χ\chi
\epsilon or \varepsilon ϵ\epsilon or ε\varepsilon \lambda λ\lambda \rho ρ\rho \psi ψ\psi
\zeta ζ\zeta \mu μ\mu \sigma σ\sigma \omega ω\omega

大写同理

\Alpha A\Alpha \Eta H\Eta \Nu N\Nu \Tau T\Tau
\Beta B\Beta \Theta Θ\Theta \Xi Ξ\Xi \Upsilon Υ\Upsilon
\Gamma Γ\Gamma \Iota I\Iota O OO \Phi Φ\Phi
\Delta Δ\Delta \Kappa K\Kappa \Pi Π\Pi \Chi X\Chi
\Epsilon E\Epsilon \Lambda Λ\Lambda \Rho P\Rho \Psi Ψ\Psi
\Zeta Z\Zeta \Mu M\Mu \Sigma Σ\Sigma \Omega Ω\Omega

微积分

极限

1
\lim_{n \to \infty} x_n

limnxn\lim_{n \to \infty} x_n

导数

1
f'(x)

f(x)f'(x)

1
\dfrac{dy}{dx}

dydx\dfrac{dy}{dx}

例子:导数定义

1
f'(x) = \lim_{h \to \infty} \dfrac{f(x + h) - f(x)}{h}

f(x)=limhf(x+h)f(x)hf'(x) = \lim_{h \to \infty} \dfrac{f(x + h) - f(x)}{h}

1
f'(a) = \lim_{x \to a} \dfrac{f(x) - f(a)}{x - a}

f(a)=limxaf(x)f(a)xaf'(a) = \lim_{x \to a} \dfrac{f(x) - f(a)}{x - a}

求和符号

1
\sum_{k = 1}^{n} x_k

k=1nxk\sum_{k = 1}^{n} x_k

积分

1
\int_a^b f(x) dx

abf(x)dx\int_a^b f(x) dx

例子:黎曼和以及黎曼积分

1
\sum_{i = 1}^{n} f(x^*_i) \Delta x

i=1nf(xi)Δx\sum_{i = 1}^{n} f(x^*_i) \Delta x

1
\int_a^b f(x) dx = \lim_{n \to \infty} \sum_{i = 1}^{n} f(x^*_i) \Delta x

abf(x)dx=limni=1nf(xi)Δx\int_a^b f(x) dx = \lim_{n \to \infty} \sum_{i = 1}^{n} f(x^*_i) \Delta x

线性代数

Field (域)

1
\mathbb{F} % field

F\mathbb{F}

Field Symbol LaTeX
Natural Number(自然数) N\mathbb{N} \mathbb{N}
Integers(整数) Z\mathbb{Z} \mathbb{Z}
Rational(有理数) Q\mathbb{Q} \mathbb{Q}
Real(实数) R\mathbb{R} \mathbb{R}
Complex(复数) C\mathbb{C} \mathbb{C}

Matrix(矩阵)

1
2
3
4
\left(\begin{array}{cc}
A & B \\
C & D
\end{array}\right)

(ABCD)\left(\begin{array}{cc} A & B \\ C & D \end{array}\right)

其中,\left\right 指将符号自适应大小,后面可以接 (), [] {}

\begin{array}{cc} 指开始 array,可视为一个列表,当中 c 的数量表示列的数量

c 中可以插入 | 来加入竖线分割,见下面例子

array 里,用 & 做分割,\\ 进行换行

一些变种:

1
2
3
4
5
6
\left(\begin{array}{cccc}
a_{11} & a_{12} & \cdots & a_{1n} \\
a_{21} & a_{22} & \cdots & a_{2n} \\
\vdots & \vdots & \ddots & \vdots \\
a_{m1} & a_{m2} & \cdots & a_{mn} \\
\end{array}\right)

(a11a12a1na21a22a2nam1am2amn)\left(\begin{array}{cccc} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \\ \end{array}\right)

Augmented Matrix(增广矩阵)

1
2
3
4
5
6
\left[\begin{array}{cccc|c}
a_{11} & a_{12} & \cdots & a_{1n} & c_1 \\
a_{21} & a_{22} & \cdots & a_{2n} & c_2 \\
\vdots & \vdots & \ddots & \vdots & \vdots \\
a_{m1} & a_{m2} & \cdots & a_{mn} & c_m \\
\end{array}\right]

[a11a12a1nc1a21a22a2nc2am1am2amncm]\left[\begin{array}{cccc|c} a_{11} & a_{12} & \cdots & a_{1n} & c_1 \\ a_{21} & a_{22} & \cdots & a_{2n} & c_2 \\ \vdots & \vdots & \ddots & \vdots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} & c_m \\ \end{array}\right]

Block Matrix(似乎没有中文译名)

1
2
3
4
5
6
7
\left(\begin{array}{cc|cc}
a_{11} & a_{12} & a_{13} & a_{14} \\
a_{21} & a_{22} & a_{23} & a_{24} \\
\hline
a_{31} & a_{32} & a_{33} & a_{34} \\
a_{41} & a_{42} & a_{43} & a_{44} \\
\end{array}\right)

(a11a12a13a14a21a22a23a24a31a32a33a34a41a42a43a44)\left(\begin{array}{cc|cc} a_{11} & a_{12} & a_{13} & a_{14} \\ a_{21} & a_{22} & a_{23} & a_{24} \\ \hline a_{31} & a_{32} & a_{33} & a_{34} \\ a_{41} & a_{42} & a_{43} & a_{44} \\ \end{array}\right)

在换完行后加上 \hline 即可

注意 \hline 后没有换行符(\\


就先水到这里,等收集到更多再更新吧 <(_ _)>


LaTeX 笔记 - 在 VSCode 编写 LaTeX 文档的一个尝试
https://blissfulalloy79.github.io/14-latex/
作者
BlissfulAlloy79
发布于
2025年3月18日
许可协议