15

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.

2
  • That is weird. The error messages say that it failed to compile, but you say the pdf file is correctly updated?! Did you run pdfLaTeX some other way? Feb 19, 2020 at 8:17
  • This shouldn't be the cause of the problem, but do you have perl installed (try typing perl -v at the command prompt)? Feb 19, 2020 at 8:18

4 Answers 4

6

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"
      ]
    },
  ],
20
  • 1
    At least we've narrowed down the source of the problem. Now it'd help to look for error messages. Press Ctrl+Shift+U or click on the menu View -> Output to bring up the output pane. On the lower pane's menu bar there is a drop down list to pick an output to look at. Look for error messages from Latex Workshop. Feb 15, 2020 at 10:38
  • 1
    Another place you can look for error messages is in the developers console. Click on the menu Help -> Toggle Developer tools. This will bring up a pane on the right. On its menu you should see Console. If you want, you can also click on the levels drop-down menu to focus on errors. Feb 15, 2020 at 10:46
  • 1
    Well, the problem isn't in those settings. It could be in the folder or workplace settings. Is there a .vscode/settings.json file or a *.code-workspace file? Mar 11, 2020 at 1:13
  • 1
    Yes, that would be it! Deleting/commenting that key & value should fix things. Mar 12, 2020 at 7:31
  • 1
    I just use the default build tool set up by latex-workshop. If you haven't overridden it with the task configuration, pressing Ctrl+Alt+B calls it. The default setting is to use latexmk (which requires perl). All the details about the customising it are on the wiki. Mar 22, 2020 at 19:39
3
+25

I installed the plugin LaTeX workshop to compile, and Markdown PDF to preview, which works fine for me.

3
  • 1
    Thank you for your answer. I have installed this extension (Markdown PDF 1.4.1), and disabled LaTeX Preview. However, I could not get it to work. My ultimate goal is to display the PDF file in a second tab, where it updates automatically upon being compiled. There is also no reference to LaTeX in the extension's readme. What exact steps did you take?
    – Marie. P.
    Feb 12, 2020 at 8:53
  • 1
    So after compile, do you get a PDF file in the folder? In my VSCode, on the upper right corner of the code tab, there is an icon with a red dot and a magnifier. Click on the icon, then the correct PDF preview shows on a second tab parallel to the code (.tex) tab on the right. Do you see the icon and does the icon work?
    – ZCH
    Feb 12, 2020 at 14:43
  • The PDF is generated in the folder, that is not a problem. But clicking the icon you describe is not having any such effect.
    – Marie. P.
    Feb 14, 2020 at 18:30
1

I just recently set up VS Code + LaTeX pretty much how you described (via Latex-workshop). I got it working on Windows, on my Linux machine I am still facing some issues. The only difference being that I wanted the PDF preview to be displayed in an external editor. Are you on Windows? Did you try changing the extension settings (Latex-workshop > View > Pdf:Viewer to browser/tab/external)? do any of them work?

1

I recently installed MikTex on my Windows 11 and after installing other prerequisites, I wasn't able to view the pdf on the adjacent tab of VSCode when clicked on 'View LatexPDF'. The solution that worked for me was:

  1. Press ctrl + shift + P
  2. Search LaTex Workshop : Set Default Viewer
  3. Select the VSCode Tab option here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .