第一章 LaTex与VScode的相遇

0 小工具推荐

  1. 录制 .gif文件的工具:

  2. LaTex 中表格渲染工具:

  3. 自制的北京理工大学课程报告LaTex模版

1 在VScode中配置LaTex编译环境

想在本地高效使用 LaTeX 编辑学术论文、课程报告、技术文档?试试 VS Code 吧!这部分将一步步教你配置一套 支持中文、BibLaTeX、Biber、图表绘制、代码高亮等功能 的 VS Code + LaTeX 环境。

适合系统:Windows / macOS / Linux
适合用户:对 Overleaf 不够自由、想自定义样式或离线编译的朋友

1.1 安装必备组件

1.1.1 安装 Tex 套件

📦 Windows 用户建议使用 TeX Live 镜像站 下载安装更快。

alt text

  • 打开安装包,选择文件install-tl-windows,以管理员身份运行,进行安装,安装过程比较缓慢,大约几十分钟,请耐心等待。
  • 安装完毕后,Win+R打开并输入cmd,运行终端,在终端输入xelatex -v验证是否安装成功,如果出现如下图所示版本号,便安装成功,笔者安装的是Tex Live 2024

    alt text

此步也可能出现xelatex不是内部命令也不是可运行的程序,但是不用担心,这只是 Tex Live 的软件路径没有添加到系统环境变量中,并无太大影响。实在觉得不放心,可以按照以下步骤添加环境变量。

1.1.2 配置 Tex 套件环境变量

  • Win+R打开并输入sysdm.cpl,进入到系统属性页面,然后选择高级 \rightarrow 环境变量打开如下页面:

    alt text

  • 双击系统变量中的Path,打开后,选择新建,将xelatex.exe所在路径复制过来,然后点击确定即可,一般来说xelatex.exe的安装路径在\texlive\2024\bin\windows文件夹里面:
环境变量 Path 安装路径
alt text alt text alt text
  • 然后再次Win+R键输入cmd,输入xelatex -v,回车,便会发现安装成功。

1.1.3 安装 VScode

1.1.4 安装 VScode 插件

打开 VS Code → Extensions(扩展)界面,安装以下插件:

插件名 作用
LaTeX Workshop 核心 LaTeX 编辑插件,预览、编译、语法高亮
Chinese (Simplified) 中文界面(可选)
Code Spell Checker 拼写检查(可选)

1.2 配置 LaTex Workshop

  • 快捷键Ctrl+Shift+P打开搜索框,输入settings选择Open User Settings

    alt text

  • 会打开一个settings.json的配置文件,我们在花括号里面添加如下内容即可:

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
//------------------------------LaTeX 配置----------------------------------
// 设置是否自动编译
"latex-workshop.latex.autoBuild.run":"onFileChange",
//右键菜单
"latex-workshop.showContextMenu":true,
//从使用的包中自动补全命令和环境
"latex-workshop.intellisense.package.enabled": true,
//编译出错时设置是否弹出气泡设置
"latex-workshop.message.error.show": false,
"latex-workshop.message.warning.show": false,
// 编译工具和命令
"latex-workshop.latex.tools": [
{
"name": "xelatex",
"command": "D:\\1_Download_Software\\texlive\\2024\\bin\\windows\\xelatex.exe",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOCFILE%"
]
},
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOCFILE%"
]
},
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"-outdir=%OUTDIR%",
"%DOCFILE%"
]
},
{
"name": "biber",
"command": "biber",
"args": [
"%DOCFILE%"
]
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
]
}
],
// 用于配置编译链
"latex-workshop.latex.recipes": [
{
"name": "XeLaTeX",
"tools": [
"xelatex"
]
},
{
"name": "PDFLaTeX",
"tools": [
"pdflatex"
]
},
{
"name": "BibTeX",
"tools": [
"bibtex"
]
},
{
"name": "LaTeXmk",
"tools": [
"latexmk"
]
},
{
"name": "xelatex -> biber -> xelatex*2",
"tools": [
"xelatex",
"biber",
"xelatex",
"xelatex"
]
},
{
"name": "xelatex -> bibtex -> xelatex*2",
"tools": [
"xelatex",
"bibtex",
"xelatex",
"xelatex"
]
},
{
"name": "pdflatex -> bibtex -> pdflatex*2",
"tools": [
"pdflatex",
"bibtex",
"pdflatex",
"pdflatex"
]
}
],
//文件清理。此属性必须是字符串数组
"latex-workshop.latex.clean.fileTypes": [
"*.aux",
"*.bbl",
"*.blg",
"*.idx",
"*.ind",
"*.lof",
"*.lot",
"*.out",
"*.toc",
"*.acn",
"*.acr",
"*.alg",
"*.glg",
"*.glo",
"*.gls",
"*.ist",
"*.fls",
"*.log",
"*.fdb_latexmk"
],
//设置为onFaild 在构建失败后清除辅助文件
"latex-workshop.latex.autoClean.run": "onFailed",
// 默认使用的recipe编译组合
"latex-workshop.latex.recipe.default": "xelatex -> biber -> xelatex*2",
// 用于反向同步的内部查看器的键绑定。ctrl/cmd +点击(默认)或双击
"latex-workshop.view.pdf.internal.synctex.keybinding": "double-click",
"files.autoSave": "afterDelay",
"workbench.colorTheme": "Winter is Coming (Dark Blue)",
"workbench.iconTheme": "vscode-icons",
"notebook.compactView": false,
"explorer.compactFolders": false,
"pasteImage.prefix": "./",
"markdown.copyFiles.destination": {
"**/*.md": "../../source/img/pictures_markdown/"
},
  • 这里需要注意,我默认的编译链接是xelatex -> biber -> xelatex*2,如果要更换,请更改settings.json中的如下配置:
1
2
// 设置默认编译链接
"latex-workshop.latex.recipe.default": "xelatex -> biber -> xelatex*2",
  • 如果更改为如下配置,表示使用上次的recipe编译组合:
1
"latex-workshop.latex.recipe.default": "lastUsed",
  • 如果更改为如下配置,表示使用编译组合为xelatex -> bibtex -> xelatex*2
1
"latex-workshop.latex.recipe.default": "xelatex -> bibtex -> xelatex*2"

这里如果想要更深入了解 biber 和 bibtex 的区别,请前往第三章 biber VS bibtex

1.3 编译 .tex 文件

  1. 整体测试编译流程示意:

    alt text

  2. 新建一个工作空间,打开后,在文件夹中新建一个test.tex文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xeCJK} % 中文支持
\usepackage{fontspec}
\setCJKmainfont{SimSun} % 设置中文字体

\usepackage{biblatex}
\addbibresource{refs.bib} % 指定 bib 文件

\title{LaTeX 文献测试}
\author{你的名字}
\date{\today}

\begin{document}

\maketitle

这是一个文献引用示例 \cite{lamport94}。

\printbibliography

\end{document}

  1. 在文件夹中新建一个refs.bib文件来保存参考文献内容,如下:
1
2
3
4
5
6
7
@book{lamport94,
author = {Leslie Lamport},
title = {LaTeX: A Document Preparation System},
year = {1994},
publisher = {Addison Wesley},
edition = {2}
}
  1. 直接选择编译即可,结果如下图:
    alt text

2 双向跳转的快捷键

VScode 中,有双向跳转的快捷键,可以便于 PDF 预览与源码之间的互相跳转,内容定位。

  1. 由 PDF \rightarrow 源码:Ctrl+鼠标左键双击即可快速定位到源码:

    alt text

  2. 由源码 \rightarrow PDF:首先选中源码内容,使用快捷键Ctrl+Shift+J即可快速定位到 PDF 预览中的对应位置:

    alt text

3 LaTex基本语法

4 常遇见的问题及解决

4.1 LaTex添加附录

  1. 问题描述:有时候我们会发现因为附录为了不加标题我们使用了 \section*{附录},会导致执行生成目录的语法 \tableofcontents 后,附录并未出现在目录中。

  2. 解决方法:我们需要在 \section*{附录}中一行命令,使得其被添加到目录中:

    1
    2
    \section*{附录}
    \addcontentsline{toc}{section}{附录}

4.2 设置行内代码等宽字体

  1. 问题描述:有时我们在写行内代码,比如 Python 中 for 循环的用途,这里面的 Pythonfor都是代码,用等宽字体比较好看。

  2. 解决方法:我们使用\verb|内容|来设置等宽字体,具体显示对比效果如下:

    alt text

4.3 图片显示位置与插入位置不符

  • 问题描述:如下图所示,源代码是在等多个维度后插入的,但是由于图片属性设置为[htbp],即浮动,因此在页面大小不够的时候,会自动移动到下一页,来保证页面填充完全:

    alt text

  • 问题解决:按照如下示例的格式修改即可:

1
2
3
4
5
6
7
8
\usepackage{float} % ← 导言区添加了这个,OK!

\begin{figure}[H] % H = Here exactly,不浮动
\centering
\includegraphics[width=0.7\textwidth]{fig/fig3.png}
\caption{干扰类别}
\label{fig:myfig3}
\end{figure}

4.4 目录页数大于2时第1页目录没有页眉

  • 问题描述:如下图所示,当目录页数大于2的时候,会发现只有第二页有页眉,第一页没有。

    alt text

  • 问题分析:目录第一页没有页眉,而目录第二页才开始显示页眉。这是因为 \tableofcontents 默认会在目录首页自动插入 \thispagestyle{plain},从而取消页眉。
  • 问题解决:在目录页相关的LaTex代码中添加:
1
2
3
\fancypagestyle{plain}{
\pagestyle{fancy}
}