使用Latex Worksop时,如何引入Minted宏包?

2 min read

我习惯使用VScode(现在是Cursor)+Latex Workshop工具链来书写Latex,但是最近引入Minted宏包时遇到了一些困难,并困扰了我一阵(详见The latexmk recipe can find pygmentize, but the xelatex and pdflatex recipes cannot.),因此打算记录一下。

  1. pip install Pygments. Minted依赖pygmentize来提供语法高亮支持。
  2. 打开VScodesettings.json,找到(或添加)latex-workshop.latex.tools配置,并在各个toolsargs选项中添加-shell-escape选项。兹列出我的配置如下:
  "latex-workshop.latex.tools": [
    {
      "name": "xelatex",
      "command": "/Library/TeX/texbin/xelatex",
      "args": [
        "-shell-escape",
        "-synctex=1",
        "-interaction=nonstopmode",
        "-file-line-error",
        "%DOCFILE%"
      ]
    },
    {
      "name": "pdflatex",
      "command": "/Library/TeX/texbin/pdflatex",
      "args": [
        "-synctex=1",
        "-interaction=nonstopmode",
        "-file-line-error",
        "-shell-escape",
        "%DOCFILE%"
      ]
    },
    {
      "name": "latexmk",
      "command": "latexmk",
      "args": [
        "-synctex=1",
        "-interaction=nonstopmode",
        "-file-line-error",
        "-pdf",
        "-xelatex=xelatex -shell-escape %O %S",
        "-outdir=%OUTDIR%",
        "%DOC%"
      ]
    },
    {
      "name": "bibtex",
      "command": "/Library/TeX/texbin/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"]
    }
  ],

务必确保--shell-escape选项在%DOCFILE%%DOC%之前!

Last updated on 2025-04-15