【LaTex】还在用 Word 写论文、报告?VScode + LaTex 的配置让你爱上用 LaTex 写论文

在这里插入图片描述


1. 安装 texlive 和 SumatraPDF

1.1 下载

选择texlive2021.iso 下载:

texlive镜像下载

SumatraPDF下载:https://www.sumatrapdfreader.org/download-free-pdf-viewer.html

1.2 加载 texlive 2020 的 iso 文件

1.2.1 解压

注意不能在中文路径下解压,这样安装时会出错!!!

1.2.2 运行

右键以管理员身份运行 install-tl-windows.bat 文件。

2. 配置 VScode

2.1 安装LaTeX Workshop插件

2.2 配置 VSCode 的 LaTex 插件

配置文件内容如下:

    // 取消 LaTex 保存时自动编译
    //"latex-workshop.latex.autoBuild.run": "never",
    // 去掉编译出错时的气泡
    // "latex-workshop.message.error.show": false,
    // "latex-workshop.message.warning.show": false,

    "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": "pdflatex",
            "tools": [
                "pdflatex"
            ]
        },
        {
            "name": "xe->bib->xe->xe",
            "tools": [
                "xelatex",
                "bibtex",
                "xelatex",
                "xelatex"
            ]
        },
        {
            "name": "pdf->bib->pdf->pdf",
            "tools": [
                "pdflatex",
                "bibtex",
                "pdflatex",
                "pdflatex"
            ]
        }
    ],
    // 使用 SumatraPDF 预览编译好的PDF文件
    "latex-workshop.view.pdf.viewer": "external",
    "latex-workshop.view.pdf.external.viewer.command": "E:/SumatraPDF/SumatraPDF.exe",
    "latex-workshop.view.pdf.external.viewer.args": [
        "-forward-search",
        "%TEX%",
        "%LINE%",
        "-reuse-instance",
        "-inverse-search",
        "\"E:/Microsoft VS Code/Code.exe\" \"E:/Microsoft VS Code/resources/app/out/cli.js\" -gr \"%f\":\"%l\"",
        "%PDF%"
    ],
    // 配置正向和反向搜索
    "latex-workshop.view.pdf.external.synctex.command": "E:/SumatraPDF/SumatraPDF.exe",
    "latex-workshop.view.pdf.external.synctex.args": [
        "-forward-search",
        "%TEX%",
        "%LINE%",
        "-reuse-instance",
        "-inverse-search",
        "\"E:/Microsoft VS Code/Code.exe\" \"E:/Microsoft VS Code/resources/app/out/cli.js\" -gr \"%f\":\"%l\"",
        "%PDF%",
    ],

2.3 Bug

编译时弹出如下bug:

Recipe terminated with fatal error: spawn xelatex ENOENT.

解决方法:
在用户和系统环境变量中添加:

E:\texlive\2021\bin\win32

参考来自:Recipe terminated with fatal error: spawn xelatex ENOENT

3. 锦上添花的 VScode插件

参考:VScode插件

4. 保存时自动格式化

参考来自:https://blog.csdn.net/Aloneingchild/article/details/108716987

下载:https://ctan.org/tex-archive/support/latexindent,解压后放在指定目录下,如 E:\texlive\latexindent下,将其添加到用户和系统环境变量中。
配置文件中添加:

// LaTex 格式化
  "latex-workshop.latexindent.path": "E:/texlive/latexindent/latexindent.exe",

若要在保存时自动格式化,需要参考VScode插件这里的相关配置。
安装这个插件(可能暂时不能格式化中文):
在这里插入图片描述

5. 最新配置文件

{
  "workbench.colorTheme": "Material Theme",
  "workbench.iconTheme": "material-icon-theme",
  // 取消 LaTex 保存时自动编译
  "latex-workshop.latex.autoBuild.run": "never",
  // 去掉编译出错时的气泡
  "latex-workshop.message.error.show": false,
  "latex-workshop.message.warning.show": false,
  // 配置 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": "pdflatex",
      "tools": ["pdflatex"]
    },
    {
      "name": "xe->bib->xe->xe",
      "tools": ["xelatex", "bibtex", "xelatex", "xelatex"]
    },
    {
      "name": "pdf->bib->pdf->pdf",
      "tools": ["pdflatex", "bibtex", "pdflatex", "pdflatex"]
    }
  ],
  // 在侧边栏中预览PDF文件
  "latex-workshop.view.pdf.viewer": "tab",
  // 使用 SumatraPDF 预览编译好的PDF文件
  // "latex-workshop.view.pdf.viewer": "external",
  // "latex-workshop.view.pdf.external.viewer.command": "E:/SumatraPDF/SumatraPDF.exe",
  // "latex-workshop.view.pdf.external.viewer.args": [
  //   "-forward-search",
  //   "%TEX%",
  //   "%LINE%",
  //   "-reuse-instance",
  //   "-inverse-search",
  //   "\"E:/Microsoft VS Code/Code.exe\" \"E:/Microsoft VS Code/resources/app/out/cli.js\" -gr \"%f\":\"%l\"",
  //   "%PDF%"
  // ],
  // 配置正向和反向搜索
  // "latex-workshop.view.pdf.external.synctex.command": "E:/SumatraPDF/SumatraPDF.exe",
  "latex-workshop.view.pdf.external.synctex.args": [
    "-forward-search",
    "%TEX%",
    "%LINE%",
    "-reuse-instance",
    "-inverse-search",
    "\"E:/Microsoft VS Code/Code.exe\" \"E:/Microsoft VS Code/resources/app/out/cli.js\" -gr \"%f\":\"%l\"",
    "%PDF%"
  ],
  // LaTex 格式化,好像 latexindent 不支持中文
  // "latex-workshop.latexindent.path": "E:/texlive/latexindent/latexindent.exe",

  // Prettier + ESLint VSCode config
  "editor.formatOnSave": true, // 保存时格式化
  "[jsonc]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[latex]": {
    "editor.defaultFormatter": "James-Yu.latex-workshop"
  },
  // 自动换行
  "editor.wordWrap": "on",

  // "[latex]": {
  //   "editor.defaultFormatter": "nickfode.latex-formatter"
  // }

  // LaTex 编译时自动删除多余的文件,如果自动删除 .aux 文件,则 \ref{} 会报错; .out 文件被删除也会产生一个警告
  "latex-workshop.latex.autoClean.run": "onBuilt", //注意结尾是 t 不是 d
  "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"
  ]
}

6. 参考链接

[1] https://zhuanlan.zhihu.com/p/38178015


关注公众号“ 大学生内卷生存指北” 获取更多笔记、源码、学习资源!

在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宇宙爆肝锦标赛冠军

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值