如何使用WSL+VScode+Texlive+git+SSH搭建LaTeX环境

前言:为什么要大费周章在WSL里面使用LaTeX?

一句话,Windows下编译LaTeX太慢了,仅此而已

安装WSL

  • 在开始菜单搜索“启用或关闭Windows功能”
  • 勾选“适用于 Linux 的 Windows 子系统”
  • 安装wsl,可选择一下两种方法
    • 在Microsoft Store中搜索Ubuntu,安装Ubuntu 22.04 LTS
    • 在命令行中输入
      1
      wsl --list --online
      查看可用分支,再输入
      1
      wsl --install -d <分支名称>
  • 安装完成后,点击启动,等待安装完成
  • 设置用户名和密码

git&SSH

git配置

  • 输入
    1
    vi ~/.gitconfig
  • 在文件中黏贴以下内容(这样最快)
    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
    [user]
    name = YOUR_NAME
    email = YOUR_EMAIL
    [alias]
    co = commit
    st = status
    logl = log --graph --all --oneline
    [core]
    quotepath = false
    editor = code --wait --new-window
    excludesfile = ~/.gitignore
    [filter "lfs"]
    process = git-lfs filter-process
    required = true
    clean = git-lfs clean -- %f
    smudge = git-lfs smudge -- %f
    [diff]
    tool = vscode-diff
    [difftool]
    prompt = false
    [difftool "vscode-diff"]
    cmd = code --wait --diff $LOCAL $REMOTE
    [merge]
    tool = vscode-merge
    [mergetool "vscode-merge"]
    cmd = code --wait $MERGED

    [init]
    defaultBranch = main
    [http]
    sslVerify = false
    [https]
    sslVerify = false
    [branch]
    autosetuprebase = always
  • 记得修改前两行的YOUR_NAME和YOUR_EMAIL
  • 保存退出
  • 输入
    1
    vi ~/.gitignore
  • 在文件中黏贴以下内容
    1
    2
    3
    4
    5
    6
    7
    8
    *.log
    *.orig
    *.exe
    *.aux
    *.fdb_latexmk
    *.fls
    *.out
    *.toc
  • 保存退出

ssh-key配置

  • 输入
    1
    ssh-keygen -t rsa -C "YOUR_EMAIL"
    一直回车,不用输入密码
  • 在Github/GitLab上添加ssh-key

本人的不成熟的Git工作流

  • 在Github/GitLab上创建一个仓库
  • 远程仓库中只有main一个分支,用作同步
  • 本地在WSL中新建一个WSL分支,在Windows下建立一个Windows分支,每次在对应的分支下工作,做完之后使用cherry-pick将提交pick到main上进行同步

Texlive安装

这里也可以按照官网教程做,不过这个比较简单(虽然版本比较老,不过我是用不出差别)

  • 更新一下
    1
    sudo apt update
    不做这一步下面找不到安装包 texlive-full
  • 在Ubuntu中安装Texlive
    1
    sudo apt install texlive-full
  • 安装完成后,输入
    1
    tex --version
    查看版本,如果出现版本号,说明安装成功

VScode配置

  • 在目标文件夹下输入
    1
    code .
  • 打开VScode

常见问题

  • VSCode 使用LaTeX-workshop编译时报错,提示
    1
    Does the executable exist? $PATH: undefined
    解决方法:
    • 如果是通过官网安装的,看一下~/.bashrc里面有没有添加到PATH里面,如果没有,手动添加一下:
    • 如果确认已经在PATH中了,输入
      1
      source ~/.bashrc
      如果还没好,退出重启一下就好(这里非常玄学,有时候自己就好了,有时候得重启几次)

附录

本人的部分配置文件(setting.json)关于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
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
"latex-workshop.latex.autoBuild.run": "never",
"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": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
]
},
{
"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 * 2",
"tools": [
"xelatex",
"xelatex"
]
},
{
"name": "XeLaTeX",
"tools": [
"xelatex"
]
},
{
"name": "PDFLaTeX",
"tools": [
"pdflatex"
]
},
{
"name": "BibTeX",
"tools": [
"bibtex"
]
},
{
"name": "LaTeXmk",
"tools": [
"latexmk"
]
},
{
"name": "xe->bibtex->xe*2",
"tools": [
"xelatex",
"bibtex",
"xelatex",
"xelatex"
]
},
{
"name": "pdf->bibtex->pdf*2",
"tools": [
"pdflatex",
"bibtex",
"pdflatex",
"pdflatex"
]
},
{
"name": "xe->biber->xe*2",
"tools": [
"xelatex",
"biber",
"xelatex",
"xelatex"
]
},
],
"latex-workshop.latex.recipe.default": "lastUsed",
"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",
],
"latex-workshop.latex.autoClean.run": "onBuilt",
"latex-workshop.view.pdf.internal.synctex.keybinding": "double-click",
"latex-workshop.view.pdf.viewer": "tab",
"latex.linter.enabled": false,//禁用警告