Visual Studio Code 中的 PDF 预览

Visual Studio Code 中的 PDF 预览

这个问题,其中一个答案解释了 VS Code 中 LaTeX 的设置。我按照说明操作,确实能够在 VS Code 中编译 LaTeX 文档,并且 PDF 输出与预期一样位于同一文件夹中。

但是,PDF 的预览不起作用。(更新:它正在工作,但如果文件再次编译则不会更新。)

我正在使用:LaTeX Workshop 8.7.2。

选择“查看 PDF 文件”后,会出现以下消息

未找到命令‘vscode.preview.Html’

在右下角弹出。

我在网上搜索,发现这里我应该打开整个文件夹而不是.tex文件,我照做了,但无济于事。它还表明由于软件更改,该功能不再存在。

当我禁用预览插件时,ctrl + shift + b可以进行编译,但ctrl + alt + v不能。

如何在 VS Code 中预览 PDF 文件?是否应该使用其他扩展?我不确定 LaTeX Workshop 是否应该自带 PDF 查看器,但至少对我来说,它不起作用。


更新:Output来自 LaTeX 研讨会:

[10:08:34] Initializing LaTeX Workshop.
[10:08:34] Creating file watcher for .pdf files.
[10:08:34] pdflatex is provided by MiKTeX
[10:08:34] Creating LaTeX Workshop http and websocket server.
[10:08:34] LaTeX Workshop initialized.
[10:08:34] Found root file from active editor: c:\Users\d91776\Dropbox\CFA\_CFA.tex
[10:08:34] Root file changed from: undefined to c:\Users\d91776\Dropbox\CFA\_CFA.tex. Find all dependencies.
[10:08:34] Instantiating a new file watcher for c:\Users\d91776\Dropbox\CFA\_CFA.tex
[10:08:34] Creating file watcher for .bib files.
[10:08:35] Parsing c:\Users\d91776\Dropbox\CFA\_CFA.tex
[10:08:35] Server created on 127.0.0.1:50666
[10:08:35] Adding c:\Users\d91776\Dropbox\CFA\_CFA.tex to file watcher.
[10:08:35] Snippet data loaded.
[10:08:35] LaTeX Workshop version: 8.7.2
[10:09:04] Found root file from active editor: c:\Users\d91776\Dropbox\CFA\_CFA.tex
[10:09:04] Root file remains unchanged from: c:\Users\d91776\Dropbox\CFA\_CFA.tex.
[10:11:06] File watcher: responding to change in c:\Users\d91776\Dropbox\CFA\_CFA.tex
[10:11:06] Parsing c:\Users\d91776\Dropbox\CFA\_CFA.tex
[10:11:06] c:\Users\d91776\Dropbox\CFA\_CFA.tex changed. Auto build project.
[10:11:06] BUILD command invoked.
[10:11:06] Building root file: c:\Users\d91776\Dropbox\CFA\_CFA.tex
[10:11:06] Build root file c:\Users\d91776\Dropbox\CFA\_CFA.tex
[10:11:06] Recipe step 1: latexmk, --max-print-line=10000,-synctex=1,-interaction=nonstopmode,-file-line-error,-pdf,-outdir=./build,c:/Users/d91776/Dropbox/CFA/_CFA
[10:11:06] LaTeX build process spawned. PID: 2448.
[10:11:07] Recipe returns with error: 1/null. PID: 2448. message: latexmk: The script engine could not be found.
latexmk: Data: scriptEngine="perl.exe", scriptName="latexmk"
.
[10:11:07] Cleaning auxillary files and retrying build after toolchain error.
[10:11:07] Recipe step 1: latexmk, --max-print-line=10000,-synctex=1,-interaction=nonstopmode,-file-line-error,-pdf,-outdir=./build,c:/Users/d91776/Dropbox/CFA/_CFA
[10:11:07] LaTeX build process spawned. PID: 9760.
[10:11:07] Recipe returns with error: 1/null. PID: 9760. message: latexmk: The script engine could not be found.
latexmk: Data: scriptEngine="perl.exe", scriptName="latexmk"
.
[10:11:25] Found root file from active editor: c:\Users\d91776\Dropbox\CFA\_CFA.tex
[10:11:25] Root file remains unchanged from: c:\Users\d91776\Dropbox\CFA\_CFA.tex.
[10:11:30] ACTIONS command invoked.
[10:11:31] ACTIONS command invoked.
[10:12:05] Found root file from active editor: c:\Users\d91776\Dropbox\CFA\_CFA.tex
[10:12:05] Root file remains unchanged from: c:\Users\d91776\Dropbox\CFA\_CFA.tex.
[10:12:18] Found root file from active editor: c:\Users\d91776\Dropbox\CFA\_CFA.tex
[10:12:18] Root file remains unchanged from: c:\Users\d91776\Dropbox\CFA\_CFA.tex.
[10:12:46] Found root file from active editor: c:\Users\d91776\Dropbox\CFA\_CFA.tex
[10:12:46] Root file remains unchanged from: c:\Users\d91776\Dropbox\CFA\_CFA.tex.

目录中的 PDF 文件已正确创建并更新。


控制台中的条目:

只有这些在运行 pdfLaTeX 时才会出现。它们似乎只是一些与 Python 相关的错误(我之前在与 LaTeX 文档所在的文件夹不同的文件夹中运行过 Python 程序)。

console.ts:137 [Extension Host] Info Python Extension: 2020-02-18 10:24:22: Cached data exists getEnvironmentVariables, c:\Users\d91776\Dropbox\CFA
2console.ts:137 [Extension Host] Info Python Extension: 2020-02-18 10:24:22: > C:\Users\max\AppData\Local\Programs\Python\Python38\python.exe -c "import sys;print(sys.executable)"

具体来说,当我单击工具栏中的图标时,输出如下: 当我单击工具栏中的图标时,会发生以下情况:

[18:21:20] VIEW command invoked with mode: file:///c%3A/Users/me/Dropbox/CFA/_CFA.tex.

[18:21:20] Found root file from active editor: c:\Users\me\Dropbox\CFA\_CFA.tex

[18:21:20] Root file remains unchanged from: c:\Users\me\Dropbox\CFA\_CFA.tex.

[18:21:20] Cannot find PDF file c:\Users\me\Dropbox\CFA\build\_CFA.pdf

它似乎正在子文件夹中搜索build,我该如何更改?我发现这个问题,看似相关,但却没有帮助。


这是该文件settings.json夹中的文件.vscode

{  
    "cSpell.enabled": true,
    "editor.cursorBlinking": "solid",
    "editor.wordWrap": "on",
    "editor.wordWrapColumn": 80,
    "editor.wrappingIndent": "same",
    "latex-workshop.latex.outDir": "./build",
    "latex-workshop.view.pdf.viewer": "tab",
    "latex-workshop.latex.autoBuild.cleanAndRetry.enabled": true,
    "latex-workshop.latex.autoClean.run": "onBuilt",
    "latex-workshop.latex.clean.subfolder.enabled": true,
    "latex-workshop.latex.clean.fileTypes": [ "*.aux", "*.toc" ]
    "latex-workshop.debug.showUpdateMessage": false,
    "telemetry.enableCrashReporter": false,
    "telemetry.enableTelemetry": false,
    "python.pythonPath": "C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python38\\python.exe",
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "restructuredtext.confPath": ""
  }  

删除该行"latex-workshop.latex.outDir": "./build",有效。


遗憾的是,编译 LaTeX 文件时 PDF 查看器不会更新。

我添加了下面三行:

{
    "version": "2.0.0",
    "tasks": [
          {
            "label": "Run pdflatex",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "command": "pdflatex",
            "args": [
                "-interaction=nonstopmode",
                "-file-line-error",
                "_CFA.tex"
            ]
        },
        {
            "label": "Run bibtex",
            "type": "shell",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "command": "bibtex",
            "args": [
                "-terse",
                "*.aux"
            ]
        },
        {
            "label": "update pdf", 
            "dependsOn": "build", 
            "command": "${command:latex-workshop.refresh-viewer}" 
        }     
    ]
   } 

tasks.json文件中,建议在这里,但无济于事。

要查看问题可能出在哪里,请执行以下操作本网站,在文件中settings.json,我添加了以下行:

"latex-workshop.view.pdf.zoom": "page-fit",

确实,在 VS Code 选项卡中打开 PDF 预览后,适合度会调整为页面适合。

答案1

在我的设置中,我只安装了 LaTeX Workshop(版本 8.7.2),没有安装 LaTeX Preview,但它可以开箱即用(latexmk已安装 MikTeX 和包)。

我建议禁用 LaTeX Preview 和 Markdown PDF(听起来你已经尝试过了但没有效果),然后尝试通过以下其他方式打开 pdf 看看它们是否有效:

  • 在文件选项卡旁边的工具栏上单击此按钮: 预览按钮
  • 按下Ctrl + Shift + P并开始输入latex workshop view latex pdf file,然后在它出现时单击它。
  • 编辑:在侧边栏上,单击“TeX”按钮,然后单击“查看 LaTeX PDF”。尝试展开菜单并尝试不同的选项。
  • 在侧栏上单击此按钮可打开资源管理器:资源管理器按钮,然后点击 pdf 文件。如果您看到有关它是二进制文件的消息,请点击“仍然打开”之类的内容。

如果这些都不起作用,则 VS Code 的 pdf 查看器存在问题。如果只有最后一个起作用,则 LaTeX workshop 存在问题。如果所有都起作用,则键盘快捷键存在问题。

更新

对于更新的问题:

看起来tasks.json设置是这样的,调用“update pdf”将在运行之前调用“build”,但是您希望在运行后调用“build”来调用“update pdf”。

我认为tasks.json应该是这样的:

{
    "version": "2.0.0",
    "tasks": [
          {
            "label": "Run pdflatex",
            "type": "shell",
            "group": "build",
            "command": "pdflatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "_CFA.tex"
            ]
        },
        {
            "label": "Run bibtex",
            "type": "shell",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "command": "bibtex",
            "args": [
                "-terse",
                "*.aux"
            ]
        },
        {
            "label": "update pdf",
            "command": "${command:latex-workshop.refresh-viewer}"
        },
        {
            "label": "Compile and update",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOrder": "sequence",
            "dependsOn": ["Run pdflatex", "update pdf"]
        }
    ]
}

这里我创建了一个新任务,依次调用“运行 pdflatex”和“更新 pdf”。我还将其设为默认构建工具,因此它将随 一起运行。如果您也Ctrl+Shift+B想运行,可以将其添加到字段中。bibtex"dependsOn"

请注意,LaTeX-Workshop 的构建配方使用Ctrl+Alt+B而不是 来运行Ctrl+Shift+B。要使其正常工作,您可以latexmk通过将其放入配方列表中来删除配方settings.json

  "latex-workshop.latex.recipes": [
    {
      "name": "pdflatex ➞ bibtex ➞ pdflatex × 2",
      "tools": [
        "pdflatex",
        "bibtex",
        "pdflatex",
        "pdflatex"
      ]
    },
  ],

答案2

我安装了插件LaTeX workshop来编译和Markdown PDF预览,对我来说效果很好。

答案3

我最近刚刚按照您描述的方式设置了 VS Code + LaTeX(通过 Latex-workshop)。我在 Windows 上成功运行了它,但在 Linux 机器上仍然遇到一些问题。唯一的区别是我希望 PDF 预览显示在外部编辑器中。您使用的是 Windows 吗?您是否尝试过更改扩展设置(Latex-workshop > View > Pdf:Viewer to browser/tab/external)?它们有用吗?

答案4

我最近在 Windows 11 上安装了 MikTex,在安装了其他先决条件后,单击“查看 LatexPDF”时,我无法在 VSCode 的相邻选项卡上查看 pdf。对我有用的解决方案是:

  1. ctrl + shift + P
  2. 搜索 LaTex 研讨会:设置默认查看器
  3. 在此处选择 VSCode Tab 选项

相关内容