M0rtzz.com

May 26, 2024Last Updated: September 18, 2026

Linux 下安装 TeXLive 并配置 VSCode 中 tex 编写环境(2024 最新)

Linux 下安装 TeXLive 并配置 VSCode 中 tex 编写环境(2024 最新)

这里我们以 Ubuntu 为例。

1.下载 .iso 镜像文件

下载页

image-20240819171528777

终端使用 curl 获取 .iso 镜像文件:

sudo apt install curl && curl -L https://mirrors.hust.edu.cn/CTAN/systems/texlive/Images/texlive.iso -o texlive.iso

image-20240819170549234

或者使用 XDM 获取:

image-20240819170818452

XDM 安装教程:

https://blog.csdn.net/M0rtzz/article/details/136023863

或:

https://www.m0rtzz.com/posts/6

2.安装 TeXLive

首先安装一个 GUI 工具包:

sudo apt install libdigest-perl-md5-perl perl-tk

image-20240329232608535

在下载 iso 的目录打开终端:

sudo mount -o ro,loop texlive.iso /mnt
cd /mnt
ls
sudo ./install-tl -gui

image-20240204145113354

image-20240329232410555

单击 Install 开始安装,显示 Installed 才可点击 Close

image-20240329234051336

卸载挂载到 /mnt 的镜像:

cd ~
sudo umount /mnt

配置环境变量:

vi ~/.bashrc

在末尾添加以下内容(年份填你的,本文是 2024):

# LaTeX
export MANPATH=${MANPATH}:/usr/local/texlive/2024/texmf-dist/doc/man
export INFOPATH=${INFOPATH}:/usr/local/texlive/2024/texmf-dist/doc/info
export PATH=${PATH}:/usr/local/texlive/2024/bin/x86_64-linux

保存退出

source ~/.bashrc

输入 tex -version 显示版本号即配置成功

image-20240329234435790

3.配置 VSCode

首先安装 Perl 模块(后边如果使用 latexindent 格式化代码时需要用到,我这里已经提前安装过了):

sudo apt update -y && sudo apt install -y cpanminus
sudo cpanm YAML::Tiny
sudo cpanm File::HomeDir
sudo cpanm Unicode::GCString
sudo cpanm Log::Log4perl
sudo cpanm Log::Dispatch

image-20240204134747369

安装 tex-fmt

sudo apt install -y cargo rustup && \
rustup update stable && \
rustup default stable && \
mkdir -p ${HOME}/.cargo && \
tee ${HOME}/.cargo/config.toml > /dev/null << EOF
[source.crates-io]
replace-with = 'hustmirror'

[source.hustmirror]
registry = "sparse+https://mirrors.hust.edu.cn/crates.io-index/"
EOF
cargo install tex-fmt && \
sudo cp /home/m0rtzz/.cargo/bin/tex-fmt /usr/local/bin && \
sudo chmod +x /usr/local/bin/tex-fmt

打开 VSCode,点击侧边栏插件按钮,搜索 LaTeX,安装下图两个插件:

image-20240204135028322

然后键入 Ctrl+逗号 进入设置页面,单击右上角次按钮进入 Json 配置文件:

image-20240204135608750

在末尾加入以下内容:

"files.autoSave": "afterDelay", // 自动保存
"editor.formatOnPaste": true, // 粘贴后自动格式化
"editor.formatOnType": true, // 键入后自动格式化
"editor.formatOnSave": true, // 保存时自动格式化
// LaTeX
"latex-workshop.latex.recipes": [
  {
    "name": "lualatex",
    "tools": [
      "lualatex"
    ]
  },
  {
    "name": "xelatex",
    "tools": [
      "xelatex"
    ]
  },
  {
    "name": "pdflatex",
    "tools": [
      "pdflatex"
    ]
  },
  {
    "name": "latexmk",
    "tools": [
      "latexmk"
    ]
  },
  {
     "name": "pdflatex -> bibtex -> pdflatex * 2",
    "tools": [
      "pdflatex",
      "bibtex",
      "pdflatex",
      "pdflatex"
    ]
  },
  {
    "name": "xelatex -> bibtex -> xelatex * 2",
    "tools": [
      "xelatex",
      "bibtex",
      "xelatex",
      "xelatex"
    ]
  },
  {
    "name": "lualatex -> bibtex -> lualatex * 2",
    "tools": [
      "lualatex",
      "bibtex",
      "lualatex",
      "lualatex"
    ]
  },
],
"latex-workshop.latex.tools": [
  {
    "name": "pdflatex",
    "command": "pdflatex",
    "args": [
      "--shell-escape",
      "-synctex=1",
      "-interaction=nonstopmode",
      "-file-line-error",
      "-halt-on-error",
      "%DOC%"
    ]
  },
  {
    "name": "xelatex",
    "command": "xelatex",
    "args": [
      "--shell-escape",
      "-synctex=1",
      "-interaction=nonstopmode",
      "-file-line-error",
      "-halt-on-error",
      "%DOC%"
    ]
  },
  {
    "name": "lualatex",
    "command": "lualatex",
    "args": [
      "--shell-escape",
      "-synctex=1",
      "-interaction=nonstopmode",
      "-file-line-error",
      "-halt-on-error",
      "%DOC%"
    ]
  },
  {
    "name": "latexmk",
    "command": "latexmk",
    "args": [
      "--shell-escape",
      "-synctex=1",
      "-interaction=nonstopmode",
      "-file-line-error",
      "-halt-on-error",
      "-pdf",
      "%DOC%"
    ]
  },
  {
    "name": "bibtex",
    "command": "bibtex",
    "args": [
      "%DOCFILE%"
    ]
  }
],
"latex-workshop.latex.clean.fileTypes": [
  "*.acn",
  "*.acr",
  "*.alg",
  "*.aux",
  "*.bbl",
  "*.bcf",
  "*.blg",
  "*.fdb_latexmk",
  "*.fls",
  "*.glg",
  "*.glo",
  "*.gls",
  "*.idx",
  "*.ind",
  "*.ist",
  "*.lof",
  "*.log",
  "*.lot",
  "*.nav",
  "*.out",
  "*.pdfsync",
  "*.pre",
  "*.rubbercache",
  "*.run.xml",
  "*.snm",
  "*.synctex",
  "*.synctex(busy)",
  "*.synctex.gz",
  "*.synctex.gz(busy)",
  "*.thm",
  "*.toc",
  "*.vrb"
],
// 使用 "glob" 方法清理上述后缀名的文件
"latex-workshop.latex.clean.method": "glob",
// 语法检查
"latex.linter.enabled": false,
// tex 文件浏览器,可选项为 "none" "browser" "tab" "external"
"latex-workshop.view.pdf.viewer": "tab",
// 自动编译 tex 文件
"latex-workshop.latex.autoBuild.run": "onSave",
// 显示内容菜单:(1)编译文件;(2)定位游标
"latex-workshop.showContextMenu": true,
// 显示错误
"latex-workshop.message.error.show": true,
// 显示警告
"latex-workshop.message.warning.show": true,
// 从使用的包中自动补全命令和环境
"latex-workshop.intellisense.package.enabled": true,
// 清除辅助文件
"latex-workshop.latex.autoClean.run": "never",
// 设置 vscode 编译 tex 文档时的默认编译链
"latex-workshop.latex.recipe.default": "lastUsed",
// 用于反向同步的内部查看器的键绑定。ctrl/cmd + 点击(默认)或双击
"latex-workshop.view.pdf.internal.synctex.keybinding": "double-click",
// 禁用 Magic Comments
"latex-workshop.latex.build.enableMagicComments": false,
// "latexindent" 或 "tex-fmt"
"latex-workshop.formatting.latex": "tex-fmt",
// "tex-fmt" 参数
"latex-workshop.formatting.tex-fmt.args": [
  "--nowrap",
  "--tabsize",
  "4"
],
// BibTeX 字段缩进使用 4 个空格
"latex-workshop.bibtex-format.tab": "4 spaces",
// 将每个字段的等号纵向对齐
"latex-workshop.bibtex-format.align-equal.enabled": true,
// 最后一个字段后不保留逗号
"latex-workshop.bibtex-format.trailingComma": false,
// 统一字段名(如 title,author)为小写
"latex-workshop.bibtex-format.case.field": "lowercase",
// 统一条目类型名(如@article,@inproceedings)为小写
"latex-workshop.bibtex-format.case.type": "lowercase",
// 处理重复条目:在编辑器中高亮标记重复的文献(防止重复引用)
"latex-workshop.bibtex-format.handleDuplicates": "Highlight Duplicates",
// 设置字段值的包裹符号:使用大括号
"latex-workshop.bibtex-format.surround": "Curly braces",
// 保存或手动格式化时不自动对所有文献条目进行排序
"latex-workshop.bibtex-format.sort.enabled": false,
// 开启条目内部字段的排序功能
"latex-workshop.bibtex-fields.sort.enabled": true,
// 定义字段的排列顺序,未列出的字段将按字母顺序排在这些字段后面
"latex-workshop.bibtex-fields.order": [
  // 1. 文献标识:标题与作者
  "title",
  "author",
  // 2. 时间维度:紧跟载体
  "year",
  "month",
  // 3. 发表载体:会议、期刊、学校或机构
  "booktitle",
  "journal",
  "school", // 学位授予单位
  "institution", // 报告发布机构
  "organization", // 会议/出版相关组织
  // 4. 出版细节:卷、期、页码
  "volume",
  "number",
  "series", // 论文系列
  "pages",
  "numpages", // 总页数
  // 5. 出版与地理信息
  "publisher",
  "address", // 出版地
  "location", // 会议举办地
  // 6. 数字身份与链接
  "doi",
  "url",
  "urldate",
  "isbn",
  "issn",
  "eprint", // 预印本标识(如 arXiv)
  // 7. 补充信息与长文本
  "note",
  "abstract",
  "keywords"
]

配置完之后侧边栏会出现 TEX 按钮,里面的内容对应了我们刚才的配置:

image-20240204144520352

新建 .tex 文件,输入:

\documentclass{article}
\begin{document}

 {\Huge Hello, \LaTeX!}

\end{document}

之后点击:

image-20240204143115049

再点击:

image-20240204143158321

若正常显示 .pdf 文件,则配置成功

image-20240204144037612

右键空白处选择 使用...格式化文档

image-20240204143432155

选择 LaTeX Workshop 为默认格式化程序:

image-20240204143534684

这时键入 Ctrl+S 手动保存时,因刚才 .json 文件中设置了 "editor.formatOnSave": true, // 保存时自动格式化,也安装了 Perl 模块,这时代码应该会自动格式化。

原先:

image-20240204144131168

格式化后:

image-20240204144344447