如何给vscode配置LaTex

2026-09-26

引言

本博文记录我在 Windows、Linux 和 Mac 上给 VS Code 配置 LaTeX 的步骤,仅供自己以后查阅~

三个系统都是先把 TeX 装好,再安装 LaTeX Workshop 插件,最后把配置贴进 VS Code。

Windows下配置

安装 TeX Live

先把 TeX Live 装好,并确认终端里能调用到它,再去配 VS Code。

1. 下载

首先打开 tug.org,进入 TeX Live,再打开 Windows 安装页面,下载 install-tl-windows.exe。

这个文件只是安装程序,比较小。后面安装时还会从网上下载很多文件,所以安装过程要保持联网。官网太慢的话,可以用清华镜像的同一个文件:install-tl-windows.exe。

2. 安装

右键 install-tl-windows.exe,选择“以管理员身份运行”。

安装位置可以用默认的 C:\texlive\2026,也可以改到别的盘。路径里不要有中文和空格。

点安装之后要等一段时间,文件比较大,中间不要取消。看到安装完成就可以关掉窗口。以后装新年份时,会在 texlive 下面新建一个年份文件夹。

3. 配置

安装完记得检查路径。Windows 的安装程序一般会自动把 安装目录\bin\windows 加进 Path。

已经打开的终端和 VS Code 要先关掉再打开,不然还是旧的路径。

如果新开的终端里还是提示找不到命令,就手动加一次:

  • 在开始菜单搜索“环境变量”,打开“编辑系统环境变量”,再点“环境变量”
  • 在用户变量或系统变量里选中 Path,点“编辑”,然后点“新建”
  • 填入自己的目录,例如 C:\texlive\2026\bin\windows。盘符和年份按实际位置修改
  • 确定保存,然后重新打开终端

4. 验证

新开一个 PowerShell 或命令提示符,输入:

xelatex --version
pdflatex --version

能看到 TeX Live 和年份,就说明装好了。

再用下面的命令确认路径:

where.exe xelatex

输出应该在刚才的 bin\windows 目录里。

vscode的设置

  • 在 VS Code 里安装 LaTeX Workshop 插件
  • 按 F1 或 Ctrl+Shift+P,在搜索框输入 json,选择 Preferences: Open User Settings (JSON)。有的版本显示为 Preferences: Open Settings (JSON)。然后把下面的配置贴进去
{
    "latex-workshop.latex.autoBuild.run": "never",
    "latex-workshop.showContextMenu": true,
    "latex-workshop.intellisense.package.enabled": true,
    "latex-workshop.message.error.show": false,
    "latex-workshop.message.warning.show": false,
    // Latex workshop
    "latex-workshop.latex.tools": [
      {
        // 编译工具和命令  (把%DOC%改为%DOCFILE%就可以支持编译中文路径下的文件了)
        "name": "latexmk",
        "command": "latexmk",
        "args": [
        "-synctex=1",
        "-interaction=nonstopmode",
        "-file-line-error",
        "-pdf",
        "%DOCFILE%"
        ]
      },
      {
        "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": "BibTeX",
        //     "tools": [
        //         "bibtex"
        //     ]
        // },
        // {
        //     "name": "LaTeXmk",
        //     "tools": [
        //         "latexmk"
        //     ]
        // },
        // {
        //     "name": "xelatex -> bibtex -> xelatex*2",
        //     "tools": [
        //         "xelatex",
        //         "bibtex",
        //         "xelatex",
        //         "xelatex"
        //     ]
        // },
        {
            "name": "pdflatex -> bibtex -> pdflatex*2",
            "tools": [
                "pdflatex",
                "bibtex",
                "pdflatex",
                "pdflatex"
            ]
        }
    ],
//定义编译链
"latex-workshop.latex.clean.fileTypes": [
    "*.aux",
    "*.bbl",
    "*.blg",
    "*.idx",
    "*.ind",
    "*.lof",
    "*.lot",
    "*.out",
    "*.toc",
    "*.acn",
    "*.acr",
    "*.alg",
    "*.glg",
    "*.glo",
    "*.gls",
    "*.ist",
    "*.fls",
    "*.log",
    "*.fdb_latexmk"
  ],
  "latex-workshop.view.pdf.viewer": "browser",  
  "ltex.enabled": true, // 启用插件
  "ltex.language": "en",// 设置语言
  "latex-workshop.latex.autoBuild.run": "onFileChange",   //保存tex文件时自动编译
  "latex-workshop.latex.autoClean.run": "onBuilt",  //无论是否编译成功,都选择自动清除的辅助文件类型
  "latex-workshop.latex.clean.subfolder.enabled": true, //清理编译的辅助文件
  "latex-workshop.intellisense.package.enabled": true, //设置为true,则该拓展能够从使用的宏包中自动提取命令和环境,从而补全正在编写的代码。
  "latex-workshop.latex.recipe.default": "lastUsed", //设置默认编译链(first:使用第一条编译链,lastUsed:使用最近一次编译所用的编译链)
  "latex-workshop.view.pdf.internal.synctex.keybinding": "double-click",  //反向同步,即点击编译后的pdf可以跳到相应的代码位置。
  "latex-workshop.hover.preview.enabled":true,//进行公式预览
  "latex-workshop.hover.preview.scale":0.7,//进行公式预览
  "window.zoomLevel": 0.8,
  "debug.console.fontSize": 12,
  "markdown.preview.fontSize": 12,
  "terminal.integrated.fontSize": 12,
  "editor.fontSize": 15,
  "editor.minimap.enabled": false,
  "editor.wordWrap": "on",
"latex-workshop.message.update.show": false,
"git.enableSmartCommit": true,
"remote.SSH.remotePlatform": {
    "arclab": "linux"
},
"git.confirmSync": false,
"explorer.confirmDelete": false
  }

编译时选择 pdflatex -> bibtex -> pdflatex*2 这一条。

按 Ctrl+Alt+J 可以从代码跳到 PDF。

Linux下配置

  • 先安装 VS Code,再安装 LaTeX Workshop 插件
  • 安装 TeX Live。下面两句都可以用,texlive-full 是完整版,占的空间更大。如果是服务器,当前账号可能没有权限,要先切换到有 sudo 的用户
sudo apt-get install texlive
sudo apt-get install texlive-full
  • 切换用户
su user
  • 如果用上面的命令装不上,可以按官方的方法安装,见link
cd /tmp

wget https://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz

zcat < install-tl-unx.tar.gz | tar xf -

cd install-tl-*

sudo perl ./install-tl --no-interaction

export PATH="/usr/local/texlive/2024/bin/x86_64-linux:$PATH"

cd install-tl-* 如果报错,按 Tab 补全实际的文件夹名字。sudo perl ./install-tl --no-interaction 需要 sudo 权限。export 里的路径要改成自己机器上的,改完后执行 source ~/.bashrc。

  • 查看 TeX Live 版本。能打出版本号,就说明安装成功
tex --version
  • VS Code 的配置和 Windows 一样。按 F1 或 Ctrl+Shift+P,搜索 json,打开用户设置的 JSON,把下面的配置贴进去
{
    // "latex-workshop.latex.autoBuild.run": "never",
    "latex-workshop.showContextMenu": true,
    // "latex-workshop.intellisense.package.enabled": true,
    "latex-workshop.message.error.show": true,
    "latex-workshop.message.warning.show": false,
    // Latex workshop
    "latex-workshop.latex.tools": [
      // {
      //   // 编译工具和命令  (把%DOC%改为%DOCFILE%就可以支持编译中文路径下的文件了)
      //   "name": "latexmk",
      //   "command": "latexmk",
      //   "args": [
      //   "-synctex=1",
      //   "-interaction=nonstopmode",
      //   "-file-line-error",
      //   "-pdf",
      //   "%DOCFILE%"
      //   ],
      //   "env": {"PATH": "/usr/local/texlive/2024/bin/x86_64-linux:"}
      // },
      // {
      //   "name": "xelatex",
      //   "command": "xelatex",
      //   "args": [
      //   "-synctex=1",
      //   "-interaction=nonstopmode",
      //   "-file-line-error",
      //   "%DOCFILE%"
      //     ]
      //     ,
      //   "env": {"PATH": "/usr/local/texlive/2024/bin/x86_64-linux:"}
      // },          
      {
        "name": "pdflatex",
        "command": "pdflatex",
        "args": [
        "-synctex=1",
        "-interaction=nonstopmode",
        "-file-line-error",
        "%DOCFILE%"
        ],
        "env": {"PATH": "/usr/local/texlive/2024/bin/x86_64-linux:"}
      },
      {
        "name": "bibtex",
        "command": "bibtex",
        "args": [
        "%DOCFILE%"
        ]
        ,
        "env": {"PATH": "/usr/local/texlive/2024/bin/x86_64-linux:"}
      }
    ],
    //下面设置编译工具
    "latex-workshop.latex.recipes": [
        // {
        //     "name": "XeLaTeX",
        //     "tools": [
        //         "xelatex"
        //     ]
        // },
        // {
        //     "name": "PDFLaTeX",
        //     "tools": [
        //         "pdflatex"
        //     ]
        // },
        // {
        //     "name": "BibTeX",
        //     "tools": [
        //         "bibtex"
        //     ]
        // },
        // {
        //     "name": "LaTeXmk",
        //     "tools": [
        //         "latexmk"
        //     ]
        // },
        // {
        //     "name": "xelatex -> bibtex -> xelatex*2",
        //     "tools": [
        //         "xelatex",
        //         "bibtex",
        //         "xelatex",
        //         "xelatex"
        //     ]
        // },
        {
            "name": "pdflatex -> bibtex -> pdflatex*2",
            "tools": [
                "pdflatex",
                "bibtex",
                "pdflatex",
                "pdflatex"
            ]
        }
    ],
//定义编译链
"latex-workshop.latex.autoClean.run": "onBuilt",  //无论是否编译成功,都选择自动清除的辅助文件类型
"latex-workshop.latex.autoBuild.cleanAndRetry.enabled": true,
"latex-workshop.latex.clean.method": "glob",
"latex-workshop.latex.clean.fileTypes": [
    "*.aux",
    "*.bbl",
    "*.blg",
    "*.idx",
    "*.ind",
    "*.lof",
    "*.lot",
    "*.out",
    "*.toc",
    "*.acn",
    "*.acr",
    "*.alg",
    "*.glg",
    "*.glo",
    "*.gls",
    "*.ist",
    "*.fls",
    "*.log",
    "*.fdb_latexmk"
  ],
  "latex-workshop.view.pdf.viewer": "browser",  
  "ltex.enabled": true, // 启用插件
  "ltex.language": "en",// 设置语言
  "latex-workshop.latex.autoBuild.run": "onFileChange",   //保存tex文件时自动编译
  // "latex-workshop.latex.autoClean.run": "onBuilt",  //无论是否编译成功,都选择自动清除的辅助文件类型
  "latex-workshop.latex.clean.subfolder.enabled": true, //清理编译的辅助文件
  "latex-workshop.intellisense.package.enabled": true, //设置为true,则该拓展能够从使用的宏包中自动提取命令和环境,从而补全正在编写的代码。
  "latex-workshop.latex.recipe.default": "lastUsed", //设置默认编译链(first:使用第一条编译链,lastUsed:使用最近一次编译所用的编译链)
  "latex-workshop.view.pdf.internal.synctex.keybinding": "double-click",  //反向同步,即点击编译后的pdf可以跳到相应的代码位置。
  "latex-workshop.hover.preview.enabled":true,//进行公式预览
  "latex-workshop.hover.preview.scale":0.7,//进行公式预览
  "window.zoomLevel": 0.8,
  "debug.console.fontSize": 12,
  "markdown.preview.fontSize": 12,
  "terminal.integrated.fontSize": 12,
  // "editor.fontSize": 15,//字体大小?
  "editor.minimap.enabled": false,
  "editor.wordWrap": "on",
  "latex-workshop.message.update.show": false,

    "remote.SSH.remotePlatform": {
        "147.8.89.88": "linux",
        "arclab": "linux",
        "arclab-A100x4": "linux"
    },
    "git.enableSmartCommit": true,
    "git.confirmSync": false,
    "git.ignoreRebaseWarning": true
}

Mac下配置

  • 在 Mac 上安装的是 MacTeX。装完后用下面的命令检查是否成功
latex --version
  • 再看一下有没有 TeX 的路径。Mac 的路径和 Windows、Ubuntu 不一样,要按自己的实际路径改
echo $PATH

如果没有,就执行下面两句:

export PATH=$PATH:/Library/TeX/texbin
. ~/.bash_profile
  • 在 VS Code 里安装 LaTeX Workshop 插件,和 Windows 一样
  • 配置方法也一样。按 F1 或 Ctrl+Shift+P,搜索 json,打开用户设置的 JSON,把下面的配置贴进去
{
    // "latex-workshop.latex.autoBuild.run": "never",
    "latex-workshop.showContextMenu": true,
    // "latex-workshop.intellisense.package.enabled": true,
    "latex-workshop.message.error.show": true,
    "latex-workshop.message.warning.show": false,
    // Latex workshop
    "latex-workshop.latex.tools": [
      // {
      //   // 编译工具和命令  (把%DOC%改为%DOCFILE%就可以支持编译中文路径下的文件了)
      //   "name": "latexmk",
      //   "command": "latexmk",
      //   "args": [
      //   "-synctex=1",
      //   "-interaction=nonstopmode",
      //   "-file-line-error",
      //   "-pdf",
      //   "%DOCFILE%"
      //   ],
      //   "env": {"PATH": "/usr/local/texlive/2024/bin/x86_64-linux:"}
      // },
      // {
      //   "name": "xelatex",
      //   "command": "xelatex",
      //   "args": [
      //   "-synctex=1",
      //   "-interaction=nonstopmode",
      //   "-file-line-error",
      //   "%DOCFILE%"
      //     ]
      //     ,
      //   "env": {"PATH": "/usr/local/texlive/2024/bin/x86_64-linux:"}
      // },          
      {
        "name": "pdflatex",
        "command": "pdflatex",
        "args": [
        "-synctex=1",
        "-interaction=nonstopmode",
        "-file-line-error",
        "%DOCFILE%"
        ],
        "env": {"PATH": "/Library/TeX/texbin:"}
      },
      {
        "name": "bibtex",
        "command": "bibtex",
        "args": [
        "%DOCFILE%"
        ]
        ,
        "env": {"PATH": "/Library/TeX/texbin:"}
      }
    ],
    //下面设置编译工具
    "latex-workshop.latex.recipes": [
        // {
        //     "name": "XeLaTeX",
        //     "tools": [
        //         "xelatex"
        //     ]
        // },
        // {
        //     "name": "PDFLaTeX",
        //     "tools": [
        //         "pdflatex"
        //     ]
        // },
        // {
        //     "name": "BibTeX",
        //     "tools": [
        //         "bibtex"
        //     ]
        // },
        // {
        //     "name": "LaTeXmk",
        //     "tools": [
        //         "latexmk"
        //     ]
        // },
        // {
        //     "name": "xelatex -> bibtex -> xelatex*2",
        //     "tools": [
        //         "xelatex",
        //         "bibtex",
        //         "xelatex",
        //         "xelatex"
        //     ]
        // },
        {
            "name": "pdflatex -> bibtex -> pdflatex*2",
            "tools": [
                "pdflatex",
                "bibtex",
                "pdflatex",
                "pdflatex"
            ]
        }
    ],
//定义编译链
"latex-workshop.latex.autoClean.run": "onBuilt",  //无论是否编译成功,都选择自动清除的辅助文件类型
"latex-workshop.latex.autoBuild.cleanAndRetry.enabled": true,
"latex-workshop.latex.clean.method": "glob",
"latex-workshop.latex.clean.fileTypes": [
    "*.aux",
    "*.bbl",
    "*.blg",
    "*.idx",
    "*.ind",
    "*.lof",
    "*.lot",
    "*.out",
    "*.toc",
    "*.acn",
    "*.acr",
    "*.alg",
    "*.glg",
    "*.glo",
    "*.gls",
    "*.ist",
    "*.fls",
    "*.log",
    "*.fdb_latexmk"
  ],
  "latex-workshop.view.pdf.viewer": "browser",  
  "ltex.enabled": true, // 启用插件
  "ltex.language": "en",// 设置语言
  "latex-workshop.latex.autoBuild.run": "onFileChange",   //保存tex文件时自动编译
  // "latex-workshop.latex.autoClean.run": "onBuilt",  //无论是否编译成功,都选择自动清除的辅助文件类型
  "latex-workshop.latex.clean.subfolder.enabled": true, //清理编译的辅助文件
  "latex-workshop.intellisense.package.enabled": true, //设置为true,则该拓展能够从使用的宏包中自动提取命令和环境,从而补全正在编写的代码。
  "latex-workshop.latex.recipe.default": "lastUsed", //设置默认编译链(first:使用第一条编译链,lastUsed:使用最近一次编译所用的编译链)
  "latex-workshop.view.pdf.internal.synctex.keybinding": "double-click",  //反向同步,即点击编译后的pdf可以跳到相应的代码位置。
  "latex-workshop.hover.preview.enabled":true,//进行公式预览
  "latex-workshop.hover.preview.scale":0.7,//进行公式预览
  "window.zoomLevel": 0.3,
  "debug.console.fontSize": 12,
  "markdown.preview.fontSize": 12,
  "terminal.integrated.fontSize": 12,
  // "editor.fontSize": 15,//字体大小?
  "editor.minimap.enabled": false,
  "editor.wordWrap": "on",
  "latex-workshop.message.update.show": false,

    "remote.SSH.remotePlatform": {
        "147.8.89.88": "linux",
        "arclab": "linux",
        "arclab-A100x4": "linux"
    },
    "git.enableSmartCommit": true,
    "git.confirmSync": false,
    "git.ignoreRebaseWarning": true
}

参考资料