[Tex/LaTex] PDF Preview in Visual Studio Code

previewvisual-studio-code

In this question, the setup of LaTeX in VS Code is explained in one of the answers. I followed the instructions, and am indeed able to compile a LaTeX document in VS Code, with the PDF output as expected in the same folder.

However, the preview of the PDF is not working. (Update: it is working, but does not update if the file is compiled again.)

I am using: LaTeX Workshop 8.7.2.

Upon choosing "view PDF file", the message

command 'vscode.preview.Html' not found

pops up in the bottom right.

I searched online, and found here that I should open the complete folder and not the .tex file, which I followed, but to no avail. It also suggests that the feature is no longer existing due to a software change.

When I disable the preview plugin, ctrl + shift + b compiles, but ctrl + alt + v does not.

How can preview the PDF file in VS Code? Is there another extension that should be used? I am not sure if LaTeX Workshop is supposed to bring its own PDF viewer, but at least for me, it is not working.


Update: Output from LaTeX Workshop:

[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.

The PDF-file in the directory is correctly created and updated.


Entries in the Console:

Only these appear upon running pdfLaTeX. They seem to be only some errors relating to Python (I previously ran a python program in a different folder than the one with my LaTeX documents).

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)"

In particular, when I click the icon in the toolbar, this is the output:
This is what happens when I click the icon in the toolbar:

[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

It seems to be searching in the subfolder build, how can I change this? I found this question, which seems related, but it doesn't help.


This is the settings.json file in the folder .vscode in the folder in question:

{  
    "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": ""
  }  

Deleting the line "latex-workshop.latex.outDir": "./build", worked.


Sadly, the PDF viewer does not update when the LaTeX file is compiled.

I added the bottom three lines:

{
    "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}" 
        }     
    ]
   } 

to the tasks.json file, as suggested here, but to no avail.

To see where a problem could be located, following this site, in the file settings.json, I added this line:

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

And indeed, upon opening the PDF preview in the VS Code tab, the fit is adjusted to page fit.

Best Answer

In my setup I only installed LaTeX Workshop (version 8.7.2), without LaTeX Preview, and it worked out of the box (with MikTeX and the latexmk package already installed).

I would recommend disabling LaTeX Preview and Markdown PDF (it sounds like you already tried that and it didn't work) then try to open the pdf in these other ways to see if they work:

  • On the toolbar next to the file tab click on this button: preview button
  • Press Ctrl + Shift + P and start typing latex workshop view latex pdf file and click on it when it appears.
  • Edit: On the side bar, click on the "TeX" button, then "View LaTeX PDF". Try expanding the menu and try the different options.
  • On the side bar click on this button to open the Explorer: explorer button, then click on the pdf file. If you see a message about it being a binary file click on something like "open anyway".

If none of these work, you have a problem with VS Code's pdf viewer. If only the last one works it's a problem with LaTeX workshop. If all of them work there's something wrong with the keyboard shortcut.

Update

For updated question:

It looks like the tasks.json is set so that calling "update pdf" will call "build" before running, but you want calling "build" to call "update pdf" after running.

I think the tasks.json should look like this:

{
    "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"]
        }
    ]
}

Here I have created a new task that calls "Run pdflatex" and then "update pdf" in sequence. I have also made it the default build tool, so it will run with Ctrl+Shift+B. If you want to run bibtex as well, you can add it to the "dependsOn" field.

Note that LaTeX-Workshop's build recipe runs with Ctrl+Alt+B rather than Ctrl+Shift+B. To get that to work, you can remove latexmk form the list of recipes by putting this in your settings.json:

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