search
HomeDevelopment ToolsVSCodeHow to write latex in vscode

How to write latex in vscode

1. Install texlive

Load the iso file of texlive 2019.

How to write latex in vscode

Right-click install-tl-windows and click Run as administrator to enter the installation interface.

How to write latex in vscode

You can click "Advanced" to enter advanced installation to cancel the macro packages you do not need to install.

How to write latex in vscode

Advanced installation interface

Click "Customize" to uncheck unnecessary macro packages.

How to write latex in vscode

I personally don’t need many macro packages and functions, so I uncheck them here. You can check the functions you need according to your needs. If you find it troublesome, install them all. It's ok, it doesn't consume much space.

After setting the installation path and other options, click "Install", then sit quietly for 20 minutes and wait for the installation to complete.

2. Install the Latex plug-in on VSCode

At the same time, download VSCode and install it. The installation of VSCode is very simple, so I won’t go into details here.

How to write latex in vscode

#After VSCode is installed, install the LaTeX Workshop plug-in in the extension store.

After the installation is complete, just open a tex source file.

How to write latex in vscode

You can see that the code has been highlighted.

3. Configure VSCode’s Latex plug-in

Put the following code into the settings area of ​​VSCode.

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

The default compilation tool of LaTeX Workshop is latexmk. You can modify the required tools and commands according to your needs. I don’t need to use latexmk, so I modified it to xelatex, which is commonly used in the Chinese environment. You can modify it according to your needs. Revise. (Thanks to huan Yu, replacing %DOC% in tools with %DOCFILE% can support files under Chinese paths)

"latex-workshop.latex.recipes": [
    {
        "name": "xelatex",
        "tools": [
            "xelatex"
        ]
    },
    {
        "name": "xe->bib->xe->xe",
        "tools": [
            "xelatex",
            "bibtex",
            "xelatex",
            "xelatex"
        ]
    }
],

is used to configure the compilation chain and is also placed in the settings area. The first recipe is the default compilation tool. If you need to use bibtex, you can click the small check mark in the lower left corner of the VSCode interface during compilation, click "Build LaTeX project", and select "xe->bib->xe-> xe", another method is to use the right column, or directly put the Recipe of "xe->bib->xe->xe" first, then it can be compiled as the default Recipe, but because of the number of compilations The more there are, the slower the speed will be. You can add the compilation chain you need according to the format according to your needs.

How to write latex in vscode

To use pdflatex, just add the following code at the beginning of the tex document:

%!TEX program = pdflatex

To use SumatraPDF to preview the compiled PDF file, add the following code to enter Settings area.

"latex-workshop.view.pdf.viewer": "external",
"latex-workshop.view.pdf.external.command": {
    "command": "E:/Programs/SumatraPDF/SumatraPDF.exe",
    "args": [
        "%PDF%"
    ]
},

"viewer" sets the reader to an external reader, and "command" is the path to SumatraPDF.exe, which should be modified according to the specific situation.

Now you can use VSCode to compile the tex file and preview it with SumatraPDF as the reader.

How to write latex in vscode

After several updates of the plug-in, the right-click menu option has been removed, and the options have been moved to the right column. Click the [Formula] icon in the right column, and then click the Recipe you want to use to compile. If you use the shortcut key, the first Recipe will be used by default to compile. Or you can use the shortcut Ctrl Alt V.

How to write latex in vscode

单击右上角的按钮即可打开 SumatraPDF 并预览。

4、配置正向搜索

"latex-workshop.view.pdf.external.synctex": {
    "command": "E:/Programs/SumatraPDF/SumatraPDF.exe",
    "args": [
        "-forward-search",
        "%TEX%",
        "%LINE%",
        "%PDF%"
    ]
},

添加代码进入设置区以配置正向搜索。“command”依旧是 SumatraPDF.exe 的存放位置,根据具体情况修改。

How to write latex in vscode

单击“SyncTeX from cursor”即可正向搜索。

How to write latex in vscode

可以看到,光标所在的行的内容在 PDF 中高亮显示。

5、配置反向搜索

打开 SumatraPDF,进入设置->选项->设置反向搜索命令行

How to write latex in vscode

添加以下命令:

"Code.exe" "resources\app\out\cli.js" -g "%f":"%l"

根据 VSCode 具体的安装位置将“Code.exe”和“resources\app\out\cli.js”换成 VSCode 在自己的电脑上的安装位置,例如:

"C:\Users\Marvey\AppData\Local\Programs\Microsoft VS Code\Code.exe" "C:\Users\Marvey\AppData\Local\Programs
\Microsoft VS Code\resources\app\out\cli.js" -g "%f":"%l"

(感谢Macrofuns指出,如果不加双引号,在文件路径有空格的情况下会导致无法反向搜索)

双击 PDF 中的任意一处即可跳转到 VSCode 中所对应的内容的源代码处。

How to write latex in vscode

这样 VSCode + texlive 就完全配置好了。

最好不要清理 xelatex 生成的 gz 后缀的临时文件,否则就不能进行正向和反向搜索;

之前的文章中,我提到了从 VSCode 预览按钮启动 SumatraPDF 会无法反向搜索的问题,现在已经解决,解决方法是在反向搜索命令中添加:

"resources\app\out\cli.js"

6、其他设置

LaTeX Workshop 默认保存的时候自动编译,如果不喜欢这个设置,可以添加以下代码进入设置区:

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

附录

    // LaTeX
    "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%"
            ]
        }
    ],
    "latex-workshop.latex.recipes": [
        {
            "name": "xelatex",
            "tools": [
                "xelatex"
            ]
        },
        {
            "name": "xe->bib->xe->xe",
            "tools": [
                "xelatex",
                "bibtex",
                "xelatex",
                "xelatex"
            ]
        },
        {
            "name": "pdflatex",
            "tools": [
                "pdflatex"
            ]
        }
    ],
    "latex-workshop.view.pdf.viewer": "external",
    "latex-workshop.view.pdf.external.command": {            // **********
        "command": "C:/Program Files/SumatraPDF/SumatraPDF.exe", // 注意修改路径
        "args": [                                // **********
            "%PDF%"
        ]
    },
    
    "latex-workshop.view.pdf.external.synctex": {             // **********
        "command": "C:/Program Files/SumatraPDF/SumatraPDF.exe", // 注意修改路径
        "args": [                               // **********
            "-forward-search",
            "%TEX%",
            "%LINE%",
            "%PDF%"
        ]
    },

PHP中文网,有大量免费的vscode入门教程,欢迎大家学习!

The above is the detailed content of How to write latex in vscode. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to format json with vscodeHow to format json with vscodeApr 16, 2025 am 07:54 AM

The ways to format JSON in VS Code are: 1. Use shortcut keys (Windows/Linux: Ctrl Shift I; macOS: Cmd Shift I); 2. Go through the menu ("Edit" > "Format Document"); 3. Install JSON formatter extensions (such as Prettier); 4. Format manually (use shortcut keys to indent/extract blocks or add braces and semicolons); 5. Use external tools (such as JSONLint and JSON Formatter).

How to compile vscodeHow to compile vscodeApr 16, 2025 am 07:51 AM

Compiling code in VSCode is divided into 5 steps: Install the C extension; create the "main.cpp" file in the project folder; configure the compiler (such as MinGW); compile the code with the shortcut key ("Ctrl Shift B") or the "Build" button; run the compiled program with the shortcut key ("F5") or the "Run" button.

How to install vscodeHow to install vscodeApr 16, 2025 am 07:48 AM

To install Visual Studio Code, please follow the following steps: Visit the official website https://code.visualstudio.com/; download the installer according to the operating system; run the installer; accept the license agreement and select the installation path; VSCode will start automatically after the installation is completed.

How to enlarge fonts with vscodeHow to enlarge fonts with vscodeApr 16, 2025 am 07:45 AM

The methods to enlarge fonts in Visual Studio Code are: open the settings panel (Ctrl, or Cmd,). Search and adjust "Font Size". Choose "Font Family" with the right size. Install or select a theme that provides the right size. Use keyboard shortcuts (Ctrl or Cmd) to enlarge the font.

How to connect to a remote server with vscodeHow to connect to a remote server with vscodeApr 16, 2025 am 07:42 AM

How to connect to a remote server through VSCode? Install Remote - SSH Extended Configuration SSH Create a Connection in VSCode Enter connection information: Host, Username, Port, SSH Key Double-click the saved connection in Remote Explorer

How to run vue with vscodeHow to run vue with vscodeApr 16, 2025 am 07:39 AM

Running a Vue project in VSCode requires the following steps: 1. Install the Vue CLI; 2. Create a Vue project; 3. Switch to the project directory; 4. Install project dependencies; 5. Run the development server; 6. Open the browser to visit http://localhost:8080.

How to compare two files with vscodeHow to compare two files with vscodeApr 16, 2025 am 07:36 AM

How to compare files in VSCode: 1. Open two files, 2. Enable the Differences view (View menu), 3. View the Differences (Add green, delete red, modify purple), 4. Use the arrow keys to navigate, 5. Accept or reject changes. Additional features include merging changes, copying changes, viewing details, and editing differences.

How to run js code with vscodeHow to run js code with vscodeApr 16, 2025 am 07:33 AM

How to run JS code in VSCode? Create .js files and write code; install Node.js and npm; install Debugger for Chrome; open the debug console; select Chrome; add debug configuration; set debug scripts; run code; debug code (optional).

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor