用BasicTeX构建轻量LaTeX写作环境

  • Title(EN): Building A Lightweight LaTeX Environment with BasicTeX
  • Author: dog2

  • 环境:Max OS 10.15
  • 需求 => 解决方案
    • 轻量:TeX Live太重 => 用精简命令行版BasicTeX替代
    • 简单:仅编写公式及特殊情况下的简单写作
      • Visual Studio Code的插件LaTeX Workshop,它可以实时编辑/编译TeX (推荐)
      • 极简TeX编辑器TeXworks

环境安装

安装BasicTeX

BasicTeXTeX Live的Mac OS版本MacTeX的精简版,前者80MB,后者好几GB。BasicTex只有核心的TeX语法编译的命令行工具,不包含GUI程序。

1
brew cask install basictex

安装完成后就可以使用TeX Live的包管理命令tlmgr来管理TeX的包(宏集)了。

使刚刚安装的tlmgr命令在当前shell生效

1
eval "$(/usr/libexec/path_helper)"

将Tex目录永久添加到PATH(默认安装不会添加)

1
sudo tlmgr path add

更新内置所有TeX包

1
sudo tlmgr update --self --all

安装编辑/编译环境

准备工作:安装CTeX 宏集以支持中文

1
sudo tlmgr install ctex

可选编辑器1: Visual Studio Code

安装插件

请安装以下插件:

  • LaTeX Workshop(必须)
  • LaTeX language support(可选)
  • Useful Snippets(可选)

配置LaTeX Workshop插件

  • Code > Preferences > Settings
  • 搜索@ext:james-yu.latex-workshop Recipes
  • 点击Edit in settings.json
  • 修改"latex-workshop.latex.tools""latex-workshop.latex.recipes"为如下配置
  • 重启VSCode窗口
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
"latex-workshop.latex.tools": [
{
"name": "xelatex",
"command": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"%DOC%"
]
},
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"%DOC%"
]
},
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
]
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
]
}
],
"latex-workshop.latex.recipes": [
{
"name": "xelatex",
"tools": [
"xelatex"
]
},
{
"name": "pdflatex -> bibtex -> pdflatex*2",
"tools": [
"pdflatex",
"bibtex",
"pdflatex",
"pdflatex"
]
},
{
"name": "xe->bib->xe->xe",
"tools": [
"xelatex",
"bibtex",
"xelatex",
"xelatex"
]
}
]

使用

  1. 用VSCode创建并编写.tex文件
  2. 点击VSCode左侧Bar中的TEX
  3. 在左侧菜单中,选择 Build LaTeX project
  4. 若为出现错误,选择 View LaTeX PDF,右侧标签页会弹出pdf预览
  5. 至此,修改左侧.tex的文件并保存后,插件会自动帮助我编译.tex文件生成pdf文件,并在右侧更新渲染。

可选编辑器2: TeXworks

TeXworks是一款极简的TeX图形编辑器。

1
brew cask install texworks

你好,world

打开 TeXworks,选择XeLaTeX作为排版工具,编译执行

1
2
3
4
\documentclass[UTF8]{ctexart}
\begin{document}
你好,world!
\end{document}

编写公式

编写测试数学公式,代码来源于一份其实很短的 LaTeX 入门文档

公式测试代码

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
\documentclass[UTF8]{ctexart}
\usepackage{amsmath} % 为了使用 AMS-LaTeX 提供的数学功能,我们需要在导言区加载 amsmath 宏包
\ctexset{section/format=\Large\bfseries}
\begin{document}
\section{上下标}
Einstein 's $E=mc^2$.
\[ E=mc^2. \]

\begin{equation}
E=mc^2.
\end{equation}

\section{根式与分式}

$\sqrt{x}$, $\frac{1}{2}$.

\[ \sqrt{x}, \]

\[ \frac{1}{2}. \]

\[ z = r\cdot e^{2\pi i}. \]

\section{运算符}
\[ \pm\; \times \; \div\; \cdot\; \cap\; \cup\;
\geq\; \leq\; \neq\; \approx \; \equiv \]

$ \sum_{i=1}^n i\quad \prod_{i=1}^n $
$ \sum\limits _{i=1}^n i\quad \prod\limits _{i=1}^n $
\[ \lim_{x\to0}x^2 \quad \int_a^b x^2 dx \]
\[ \lim\nolimits _{x\to0}x^2\quad \int\nolimits_a^b x^2 dx \]

\[ \iint\quad \iiint\quad \iiiint\quad \idotsint \]


\section{定界符(括号等)}
\[ \Biggl(\biggl(\Bigl(\bigl((x)\bigr)\Bigr)\biggr)\Biggr) \]
\[ \Biggl[\biggl[\Bigl[\bigl[[x]\bigr]\Bigr]\biggr]\Biggr] \]
\[ \Biggl \{\biggl \{\Bigl \{\bigl \{\{x\}\bigr \}\Bigr \}\biggr \}\Biggr\} \]
\[ \Biggl\langle\biggl\langle\Bigl\langle\bigl\langle\langle x
\rangle\bigr\rangle\Bigr\rangle\biggr\rangle\Biggr\rangle \]
\[ \Biggl\lvert\biggl\lvert\Bigl\lvert\bigl\lvert\lvert x
\rvert\bigr\rvert\Bigr\rvert\biggr\rvert\Biggr\rvert \]
\[ \Biggl\lVert\biggl\lVert\Bigl\lVert\bigl\lVert\lVert x
\rVert\bigr\rVert\Bigr\rVert\biggr\rVert\Biggr\rVert \]
(x)
\[ \bigl((x)\bigr) \]

\section{省略号}
\[ x_1,x_2,\dots ,x_n\quad 1,2,\cdots ,n\quad
\vdots\quad \ddots \]


\section{矩阵}
\[ \begin{pmatrix} a&b\\c&d \end{pmatrix} \quad
\begin{bmatrix} a&b\\c&d \end{bmatrix} \quad
\begin{Bmatrix} a&b\\c&d \end{Bmatrix} \quad
\begin{vmatrix} a&b\\c&d \end{vmatrix} \quad
\begin{Vmatrix} a&b\\c&d \end{Vmatrix} \]

Marry has a little matrix $ ( \begin{smallmatrix} a&b\\c&d \end{smallmatrix} ) $.

\section(多行公式)

\subsection{长公式}
\subsubsection{不对齐}
\begin{multline}
x = a+b+c+{} \\
d+e+f+g
\end{multline}

\subsubsection{对齐}
\begin{multline*}
x = a+b+c+{} \\
d+e+f+g
\end{multline*}

\subsection{公式组}
\begin{gather}
a = b+c+d \\
x = y+z
\end{gather}
\begin{align}
a &= b+c+d \\
x &= y+z
\end{align}

\subsection{分段函数}
\[ y= \begin{cases}
-x,\quad x\leq 0 \\
x,\quad x>0
\end{cases} \]

\end{document}

渲染的pdf:

LaTeX数学公式编写辅助工具

对LaTeX的数学公式语法还不熟悉,可以使用下列工具辅助:

  • LaTeX公式编辑器
    • 强烈推荐,妈咪说出品,详见介绍视频
    • 帮助文档:文档很全面,有详细的 数学符号<=>LaTeX标记 对照表,可以当作cheat sheet查阅
  • Mathpix:截图 => OCR => LaTex语法的数学公式
  • Detexify:手写数学符号 => 识别 => LaTex语法的数学符号
  • Myscript.Math:手写数学公式 => 识别 => LaTex语法的数学公式/绘图

参考链接

  • 一份其实很短的 LaTeX 入门文档
  • CTEX 宏集手册
  • 初心者がBasicTexをmacにインストールする(2019年)
  • (VSCode) LaTeX 所有文件报 no nobo 错误