VS Code+Sumatra PDF正反向搜索

简介

重新装机后,在配置VS Code和LaTeX的环境后,问题卡在了正反向搜索这边,简单记录下。

综合参考以下链接:

正向

在安装LaTeX Workshop后,打开VS Code的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
"latex-workshop.latex.tools": [
{
"name": "xelatex",
"command": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"%DOCFILE%"
]
},
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOCFILE%"
]
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
]
}
],

然后是由上述基本命令引出的编译链

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
"latex-workshop.latex.recipes": [
{
"name": "xelatex",
"tools": [
"xelatex"
]
},
{
"name": "pdflatex",
"tools": [
"pdflatex"
]
},
{
"name": "xe->bib->xe->xe",
"tools": [
"xelatex",
"bibtex",
"xelatex",
"xelatex"
]
},
{
"name": "pdf->bib->pdf->pdf",
"tools": [
"pdflatex",
"bibtex",
"pdflatex",
"pdflatex"
]
}
],

接着配置外部PDF阅读器

1
2
3
"latex-workshop.view.pdf.viewer": "external",
"latex-workshop.view.pdf.ref.viewer": "external",
"latex-workshop.view.pdf.external.viewer.command": "C:/Users/AppData/Local/SumatraPDF/SumatraPDF.exe",

接下来才是正向搜索:

  1. 首先保证.gz不会被删除

  2. 同样地,在settings.json写入配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    "latex-workshop.view.pdf.external.synctex.command": "C:/Users/AppData/Local/SumatraPDF/SumatraPDF.exe",
    "latex-workshop.view.pdf.external.synctex.args": [
    "-forward-search",
    "%TEX%",
    "%LINE%",
    "-reuse-instance",
    "-inverse-search",
    "%PDF%"
    ],

有人在latex-workshop.view.pdf.external.synctex.args加入反向搜索的命令,但其实不会起作用,反向搜索只要在Sumatra PDF中设置就可以了

  1. 设置正向搜索等快捷键,打开keybindings.json

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    {
    "key": "alt+s",
    "command": "latex-workshop.synctex",
    "when": "editorTextFocus && !isMac"
    },
    {
    "key": "alt+b",
    "command": "latex-workshop.build",
    "when": "editorTextFocus && !isMac"
    },
    {
    "key": "alt+t",
    "command": "latex-workshop.kill",
    "when": "editorTextFocus && !isMac"
    },
    {
    "key": "alt+e",
    "command": "latex-workshop.recipes"
    },

这样就可以通过alt+s来正向搜索

反向

在Sumatra PDF的设置->选项中,设置反向搜索命令:

1
"C:\Users\AppData\Local\Programs\Microsoft VS Code\Code.exe" "C:\Users\AppData\Local\Programs\Microsoft VS Code\resources\app\out\cli.js" --ms-enable-electron-run-as-node -r -g "%f":"%l"
  • 第一个参数是VS Code的所在位置
  • 第二个参数的cli.js和第三个参数的--ms-enable-electron-run-as-node是避免VS Code使用子进程打开Sumatra PDF后,Sumatra PDF无法反向搜索的情况
一分一毛,也是心意。