Use Visual Studio Code to Write LaTeX in Windows

下载vscode和TeX Live

这部分显然不需要说太多,下载vscode应该是一件非常容易的事情,只需要进入vscode的官网,下载.exe(Windows),或者.zip(Mac OS)文件,点击安装即可

Windows下 TeX Live 安装

我本人推荐使用的TeX发行版是TeX Live,所以这里也就只介绍TeX Live的安装。推荐使用清华大学的镜像,点击进入,下载texlive.iso文件即可,这个不带日期的版本默认是最新的。

下载之后打开.iso文件,点击install-tl-windows.bat安装即可。

安装过程大约需要2个小时。耐心等待即可。

系统用户名为中文的解决方案

很多同学习惯于用中文作为用户名,但是这样会导致 TeX Live 安装失败。更改 系统的 TEMP 和 TMP 这两个环境变量可以避免这样的问题。在命令行中运行

1
2
3
mkdir C:\temp
set TEMP=C:\temp
set TMP=C:\temp

然后再点击安装即可。这样修改环境变量是暂时的,等到cmd窗口关闭,环境变量会自动恢复原样。

Mac OS 下 MacTeX安装

MaxTeXTeX Live 的一个为 Mac OS 所定制的发行版,所以对于使用 Mac OS的推荐使用 MacTeX。同样我们也可以到清华镜像站下载。对于 Mac OS 用户,Homebrew 是一个非常方便的包管理程序,如果你安装了 homebrew,可以通过下面的方法安装 MacTeX

1
brew install MacTeX

vscode的配置

在已经完成了上述工作的基础之上,接下来就可以进行vscode的配置,不过和配置C语言环境相比,配置vscode要简单得多,安装插件LaTeX Workshop。然后进入设置,在文本模式中编辑设置,把以下代码复制进去:

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"latex-workshop.latex.tools": [
{
"name": "xelatex",
"command": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
]
},
{
"name": "xelatex-with-shell-escape",
"command": "xelatex",
"args": [
"--shell-escape",
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
]
},
{
"name": "xelatex-latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-xelatex",
"-outdir=%OUTDIR%",
"%DOC%"
]
},
{
"name": "xelatex-latexmk-with-shell-escape",
"command": "latexmk",
"args": [
"--shell-escape",
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-xelatex",
"-outdir=%OUTDIR%",
"%DOC%"
]
},
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
]
},
{
"name": "pdflatex-with-shell-escape",
"command": "pdflatex",
"args": [
"--shell-escape",
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
]
},
{
"name": "pdflatex-latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"-outdir=%OUTDIR%",
"%DOC%"
]
},
{
"name": "pdflatex-latexmk-with-shell-escape",
"command": "latexmk",
"args": [
"--shell-escape",
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"-outdir=%OUTDIR%",
"%DOC%"
]
},
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"%DOC%"
]
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
]
},
{
"name": "lualatex",
"command": "lualatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-shell-escape",//这个命令行在网上的Latex Workshop设置里一般没有,所以直接recipe会报错
"%DOCFILE%"
]
}
],

"latex-workshop.latex.recipes": [
{
"name": "XeLaTeX",
"tools": [
"xelatex"
]
},
{
"name": "LuaLaTeX",
"tools": [
"lualatex"
]
},
{
"name": "XeLaTeX with Shell Escape",
"tools": [
"xelatex-with-shell-escape"
]
},
{
"name": "XeLaTeX Auto",
"tools": [
"xelatex-latexmk"
]
},
{
"name": "XeLaTeX Auto with Shell Escape",
"tools": [
"xelatex-latexmk-with-shell-escape"
]
},
{
"name": "PDFLaTeX",
"tools": [
"pdflatex"
]
},
{
"name": "PDFLaTeX with Shell Escape",
"tools": [
"pdflatex-with-shell-escape"
]
},
{
"name": "PDFLaTeX Auto",
"tools": [
"pdflatex-latexmk"
]
},
{
"name": "PDFLaTeX Auto with Shell Escape",
"tools": [
"pdflatex-latexmk-with-shell-escape"
]
},
{
"name": "PDFLaTeX -> BibTeX -> PDFLaTeX*2",
"tools": [
"pdflatex",
"bibtex",
"pdflatex",
"pdflatex"
]
},
{
"name": "XeLaTeX -> BibTeX -> XeLaTeX*2",
"tools": [
"xelatex",
"bibtex",
"xelatex",
"xelatex"
]
},
{
"name": "latexmk",
"tools": [
"latexmk"
]
},
{
"name": "BibTeX",
"tools": [
"bibtex"
]
},
{
"name":"MakeIndex",
"tools":[
"makeindex"
]
},

],

//tex文件浏览器,可选项为"none" "browser" "tab" "external"
"latex-workshop.view.pdf.viewer": "tab",
//自动编译tex文件
"latex-workshop.latex.autoBuild.run": "onSave",
//显示内容菜单:(1)编译文件;(2)定位游标
"latex-workshop.showContextMenu": true,
//显示错误
"latex-workshop.message.error.show": false,
//显示警告
"latex-workshop.message.warning.show": false,
//从使用的包中自动补全命令和环境
"latex-workshop.intellisense.package.enabled": true,
//设置为never,为不清除辅助文件
"latex-workshop.latex.autoClean.run": "onFailed",
//设置vscode编译tex文档时的默认编译链
"latex-workshop.latex.recipe.default": "lastUsed",
// 用于反向同步的内部查看器的键绑定。ctrl/cmd +点击(默认)或双击
"latex-workshop.view.pdf.internal.synctex.keybinding": "double-click",
"workbench.editorAssociations": {
"*.pdf": "latex-workshop-pdf-hook"
},

部分命令已经带有了注释,可以自行修改。如果有同学想要进一步了解这些命令,可以自行查看LaTeX Workshop的文档,这里不赘述。此外还可以设置使用外部阅读器查看PDF,可以自行使用搜索引擎完成。

完成上述设置之后,就可以使用vscode编辑LaTeX了。

使用snippet和newcommad提高编辑效率

snippet就是自定义的代码补全,在vscodectrl+shift+p,搜索snippet,选择配置用户代码片段,在弹出的文本中输入即可。同时还需要再刚才的setting.json中输入"editor.snippetSuggestions": "top",

有一个网站可以自动将命令转化为snippet。读者可以自行去了解,使用snippet确实是一种非常好的提供效率的方式