Linux下安装TeXLive并配置VSCode中tex编写环境(2024最新)
这里我们以Ubuntu为例。
1.下载.iso镜像文件
终端使用curl获取.iso镜像文件:
sudo apt install curl && curl -L https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/Images/texlive.iso -o texlive.iso
==或者使用XDM获取:==
XDM安装教程:
https://www.m0rtzz.com/posts/6
2.安装TeXLive
首先安装一个GUI工具包:
sudo apt install libdigest-perl-md5-perl perl-tk
在下载iso的目录打开终端:
sudo chmod 777 texlive.isosudo mount -o loop texlive.iso /mntcd /mntlssudo ./install-tl -gui
单击Install
开始安装,显示Installed
才可点击Close
。
卸载挂载到/mnt
的镜像:
cd ~sudo umount /mnt
配置环境变量:
vi ~/.bashrc
在末尾添加以下内容(年份填你的,本文是2024):
# LaTeXexport MANPATH=${MANPATH}:/usr/local/texlive/2024/texmf-dist/doc/manexport INFOPATH=${INFOPATH}:/usr/local/texlive/2024/texmf-dist/doc/infoexport PATH=${PATH}:/usr/local/texlive/2024/bin/x86_64-linux
保存退出
source ~/.bashrc
输入tex -version
显示版本号即配置成功
3.配置VSCode
首先安装Perl模块(后边格式化代码时需要用到,我这里已经提前安装过了):
sudo apt update && sudo apt install cpanminus
sudo cpanm YAML::Tinysudo cpanm File::HomeDirsudo cpanm Unicode::GCStringsudo cpanm Log::Log4perlsudo cpanm Log::Dispatch
打开VSCode,点击侧边栏插件按钮,搜索LaTeX
,安装下图两个插件:
然后键入Ctrl+逗号
进入设置页面,单击右上角次按钮进入Json配置文件:
在末尾加入以下内容:
"files.autoSave": "afterDelay", // 自动保存"editor.formatOnPaste": true, // 粘贴后自动格式化"editor.formatOnType": true, // 键入后自动格式化"editor.formatOnSave": true, // 保存时自动格式化//LaTeX"latex-workshop.latex.recipes": [ { "name": "xelatex", "tools": [ "xelatex" ] }, { "name": "pdflatex", "tools": [ "pdflatex" ] }, { "name": "latexmk", "tools": [ "latexmk" ] }, { "name": "xelatex -> bibtex -> xelatex*2", "tools": [ "xelatex", "bibtex", "xelatex", "xelatex" ] }],"latex-workshop.latex.tools": [ { "name": "latexmk", "command": "latexmk", "args": [ "--shell-escape", "-synctex=1", "-interaction=nonstopmode", "-file-line-error", "-pdf", "%DOC%" ] }, { "name": "xelatex", "command": "xelatex", "args": [ "--shell-escape", "-synctex=1", "-interaction=nonstopmode", "-file-line-error", "%DOC%" ] }, { "name": "pdflatex", "command": "pdflatex", "args": [ "--shell-escape", "-synctex=1", "-interaction=nonstopmode", "-file-line-error", "%DOC%" ] }, { "name": "bibtex", "command": "bibtex", "args": [ "%DOCFILE%" ] }],"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.linter.enabled": false,// tex文件浏览器,可选项为"none" "browser" "tab" "external""latex-workshop.view.pdf.viewer": "tab",// 自动编译tex文件"latex-workshop.latex.autoBuild.run": "never",// 显示内容菜单:(1)编译文件;(2)定位游标"latex-workshop.showContextMenu": true,// 显示错误"latex-workshop.message.error.show": true,// 显示警告"latex-workshop.message.warning.show": false,// 从使用的包中自动补全命令和环境"latex-workshop.intellisense.package.enabled": true,// 设置为never,为不清除辅助文件"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"
配置完之后侧边栏会出现TEX
按钮,里面的内容对应了我们刚才的配置:
新建.tex
文件,输入:
\documentclass{article}\begin{document} {\Huge Hello, \LaTeX!}\end{document}
之后点击:
再点击:
若正常显示.pdf
文件,则配置成功
右键空白处选择使用...格式化文档
选择LaTeX Workshop
为默认格式化程序:
这时键入Ctrl+S
手动保存时,因刚才.json
文件中设置了"editor.formatOnSave": true, // 保存时自动格式化
,也安装了Perl模块,这时代码应该会自动格式化。
原先:
格式化后: