- Latex, VSCode

Configure Latex compilation environment in VSCode

Abstract

As we know, VSCode is a famous code editor with multiple language environment support. With the supporting of latex extension, it can be used to compile latex files as well. This article aims at record how to configure latex compilation environment in VSCode.

Step 1. Install latex environment on your computer

Make sure that the latex environment has been installed on your computer. In this article, we use MiKTeX environment as an example.

Step 2. Install Latex Workshop extension

Search and Install the extension “Latex Workshop” in VSCode:

Step 3. Open VSCode settings in JSON file

You can add the configurations for the extension to either user settings of VSCode or workspace settings of this workspace. In order to use the extension conveniently, we add the configurations to user settings of VSCode in this example so that it can be applied in global. Read my article below to learn how to configure user settings or workspace settings in VSCode:

Step 4. Add compilation tools configuration to user settings

Three commonly used compilation tools for latex are “xelatex”, “pdflatex” and “bibtex”. Therefore, configure the three tools with settings below:

    "latex-workshop.latex.tools": [
        {
            "name": "xelatex",
            "command": "xelatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOCFILE%"
            ]
        },
        {
            "name": "pdflatex",
            "command": "pdflatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOCFILE%"
            ]
        },
        {
            "name": "bibtex",
            "command": "bibtex",
            "args": [
                "%DOCFILE%"
            ]
        }
    ],

Step 5. Add compilation recipes configuration to user settings

The compile links can be represented by different recipes. In this example, we configure four generally used receipes “xelatex”, “pdflatex”, “xe->bib->xe->xe” and “pdf->bib->pdf->pdf”, where the first configured recipe is the default compile recipe:

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

(Optional) Step 6. Advanced Settings

Use internal pdf viewer to preview

"latex-workshop.view.pdf.viewer": "tab",

Press Ctrl + Alt + V to view the pdf file, and the view of pdf will update automatically when the tex file built successfully.

Auto build when the tex file saved

"latex-workshop.latex.autoBuild.run": "onFileChange",

If you do not like this, change the configuration to:

"latex-workshop.latex.autoBuild.run": "never",

Hide the window pops up automatically when a warning or error occurs during compilation

"latex-workshop.message.error.show": false,
"latex-workshop.message.warning.show": false,

The final configuration(copy to settings.json directly):

{
    "latex-workshop.latex.tools": [
        {
            "name": "xelatex",
            "command": "xelatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOCFILE%"
            ]
        },
        {
            "name": "pdflatex",
            "command": "pdflatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOCFILE%"
            ]
        },
        {
            "name": "bibtex",
            "command": "bibtex",
            "args": [
                "%DOCFILE%"
            ]
        }
    ],
    "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"
            ]
        }
    ],
    "latex-workshop.view.pdf.viewer": "tab",
    "latex-workshop.latex.autoBuild.run": "onFileChange",
    "latex-workshop.message.error.show": false,
    "latex-workshop.message.warning.show": false,
}

Solution of error occurs

If compile failed with error perl.exe does not exists, just download perl on official website: https://www.perl.org/get.html#win32

Finally

Above the process of configuring finished, you can write a simple tex file to test, it can be compiled successfully if all the configurations are correct.

About Ziqi.Yang394

Read All Posts By Ziqi.Yang394

Leave a Reply

Your email address will not be published. Required fields are marked *