安装扩展
ctrl+shift+x调出应用商店,搜索并安装LaTeX Workshop扩展:
配置LaTeX Workshop
LaTeX文件.tex的编译、预览、辅助文件的清理等都在扩展latex workshop中设置。
LaTeX Workshop中,编译LaTeX文件时顺序执行的一系列/组命令称为LaTeX食谱(recipes),定义在latex-workshop.latex.recipes。缺省情况,LaTeX Workshop包含2个基本的食谱,分别由latex-workshop.latex.recipes和latex-workshop.latex.Tools定义。第一个食谱的tools域中的工具定义在第二个食谱。latex workshop的参数含义见LaTeX Workshop的配置文件"package.json"或
LaTeX Workshop默认的编译方式为latexmk和pdflatex。如下配置修改了latex workshop的编译方式、预览、辅助文件等。特别添加了xelatex及包含bib时的编译。
ctrl+shift+p ->settings->首选项:打开用户设置(Json),在settings.json中添加如下内容:
"latex-workshop.showContextMenu":true, //右键菜单
"latex-workshop.intellisense.package.enabled": true, //根据加载的包,自动完成命令或包
"latex-workshop.latex.autoBuild.run": "never", //禁止保存文件时自动build
"latex-workshop.latex.recipes": [
{
"name": "latexmk",
"tools": [
"latexmk"
]
},
{
"name": "PDFlatex",
"tools": [
"pdflatex"
]
},
{
"name": "pdflatex -> bibtex -> pdflatex2",
"tools": [
"pdflatex",
"bibtex",
"pdflatex",
"pdflatex"
]
},
{
"name": "xelatex",
"tools": [
"xelatex"
]
},
{
"name": "xelatex -> bibtex -> xelatex",
"tools": [
"xelatex",
"bibtex",
"xelatex",
]
}
],
"latex-workshop.latex.tools":[
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"-outdir=%OUTDIR%",
"%DOC%"
],
"env": {}
},
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
],
"env": {}
},
{
"name": "xelatex",
"command": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
],
"env": {}
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
],
"env": {}
}
],
"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",
"*.nav",
"*.snm",
"*.synctex.gz"
],
"latex-workshop.view.pdf.viewer":"tab", //pdf文件的预览方式
快捷键
打开键盘快捷方式面板(左下侧齿轮,或快捷键ctrl+k,ctrl+s):搜索"切换侧栏可见性",设置快捷键为ctrl+k ctrl+b。
搜索 latex build,将默认的ctrl+alt+b替换为ctrl+b(与Sublime Text 3统一)。
搜索latex recipe,设置快捷键为ctlr+r,方便点菜(选择编译方式)!(ST3中是显示文档大纲)。
其他常用的快捷键:ctrl+k ctrl+a: 切换活动栏可见性(左侧图标开关)
ctrl+alt+x:显示LaTeX面板(左侧编译命令面板和文档大纲)。
ctrl+alt+c:清除辅助文件
ctrl+alt+v:查看编译的pdf文件(预览)
ctrl+alt+j:正向搜索。当设置"latex-workshop.view.pdf.viewer": "tab";时,在LaTeX源文件中按下快捷键,定位到PDF文档相应位置。(反向搜索见后面)
一些快捷处理
根据latex-workshop.latex.recipes中的name段设定,可在.tex文件首行指定编译方式。如%!TEX program = xelatex表示用xelatex编译文件,而%!TEX program = PDFlatex表示用latexpdf编译文件。多个文件情况,还可以用% !TEX root指定主文件,% !TEX bib指定bib的编译方式。
示例
%! TeX program = pdflatex
\documentclass{article}
\begin{document}
press ctrl+b to complie,press ctrl+alt+v to view pdf
\end{document}
设置LaTeX文件和PDF文件之间的正/反向搜索
正向搜索是指按某种方式操作源文件中的部分,会在PDF文件中立即到达对应的部分。反向搜索就是在PDF文档的正文中某个位置点击,光标会定位到源文件的相应位置。正反向搜索十分方便文档错误的修改。反向搜索需要用到SumatraPDF软件(或Zathura)。
SumatraPDF软件是一款小巧强悍的PDF阅读器,是免费开源软件。国内有多个PDF阅读器基于SumatraPDF修改而来。SumatraPDF官方下载地址:
下面操作的一些说明:我的VScode文件位置:VScode.exe在D:/ProgramFiles/Coding/VSCode文件夹
扩展安装在D:/ProgramFiles/Coding/VSCode/.vscode/。
即将默认的%USERPROFILE%\.vscode和%APPDATA%\Code转移到了D:/ProgramFiles/Coding/VSCode文件夹中,然后建立了联接:mklink /J "%USERPROFILE%\.vscode" ".vscode"
mklink /J ""%APPDATA%\Code"" "Code"
将SumatraPDF.exe复制到D:/ProgramFiles/Coding/VSCode/.vscode/,后面的设置只是针对这个位置的SumatraPDF.exe,设置不会影响其他位置的SumatraPDF.exe。因为我有时会使用Sumblime Text3,默认的SumatraPDF.exe已经对其设置。
步骤如下:
打开设置文件settings.json,添加如下设置(注意查找重复的配置项,如前面的配置中最后一行有latex-workshop.view.pdf.viewer,用下面的替换)(注意你的Code.exe和SumatraPDF.exe的路径):
// 设置预览方式
"latex-workshop.view.pdf.viewer":"external",
"latex-workshop.view.pdf.ref.viewer":"external",
// 设置外部PDF预览器
"latex-workshop.view.pdf.external.viewer.command": "D:/ProgramFiles/Coding/VSCode/.vscode/SumatraPDF.exe",
"latex-workshop.view.pdf.external.viewer.args": [
"%PDF%"
],
// 配置Syntex的正向搜索(这是官网的)
"latex-workshop.view.pdf.external.synctex.command": "D:/ProgramFiles/Coding/VSCode/.vscode/SumatraPDF.exe",
"latex-workshop.view.pdf.external.synctex.args": [
"-forward-search",
"%TEX%",
"%LINE%",
"-reuse-instance",
"-inverse-search",
"D:/ProgramFiles/Coding/VSCode/code D:/ProgramFiles/Coding/VSCode/resources/app/out/cli.js -r -g %f:%l",
"%PDF%"
],配置反向搜索(PDF->Latex源文件)
反向搜索在SumatraPDF中设置。打开SumatraPDF,进入设置->选项 对话框,在“设置反向搜索命令行”处填入如下内容(是一行内容,不是2行!):
"D:\ProgramFiles\Coding\VSCode\Code.exe" "D:\ProgramFiles\Coding\VSCode\resources\app\out\cli.js" -r -g "%f:%l"
双击PDF中的任意一处即可跳转到VSCode中所对应的内容的源代码处
反向搜索:打开一个已经编译的TeX文件,ctrl+alt+v打开PDF文件,在正文中双击鼠标左键,会切换到了源文件的相应位置。
正向搜索:ctrl+alt+x,找到"navigator,select and edit",点击第一项"syncTeX from cursor"(快捷键ctrl+alt+j),会切换到PDF文件的相应位置。
如果不成功,检查路径设置,或者文件名错误。
参考文章
最终的配置部分
"latex-workshop.showContextMenu":true, //右键菜单
"latex-workshop.intellisense.package.enabled": true, //根据加载的包,自动完成命令或包
"latex-workshop.latex.autoBuild.run": "never", //禁止保存文件时自动build
"latex-workshop.latex.recipes": [
{
"name": "latexmk",
"tools": [
"latexmk"
]
},
{
"name": "PDFlatex",
"tools": [
"pdflatex"
]
},
{
"name": "pdflatex -> bibtex -> pdflatex2",
"tools": [
"pdflatex",
"bibtex",
"pdflatex",
"pdflatex"
]
},
{
"name": "xelatex",
"tools": [
"xelatex"
]
},
{
"name": "xelatex -> bibtex -> xelatex",
"tools": [
"xelatex",
"bibtex",
"xelatex",
]
}
],
"latex-workshop.latex.tools":[
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"-outdir=%OUTDIR%",
"%DOC%"
],
"env": {}
},
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
],
"env": {}
},
{
"name": "xelatex",
"command": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
],
"env": {}
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
],
"env": {}
}
],
"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",
"*.nav",
"*.snm",
"*.synctex.gz"
],
// 设置预览方式
"latex-workshop.view.pdf.viewer":"external",
"latex-workshop.view.pdf.ref.viewer":"external",
// 设置外部PDF预览器
"latex-workshop.view.pdf.external.viewer.command": "D:/ProgramFiles/Coding/VSCode/.vscode/SumatraPDF.exe",
"latex-workshop.view.pdf.external.viewer.args": [
"%PDF%"
],
// 配置Syntex的正向搜索(这是官网的)
"latex-workshop.view.pdf.external.synctex.command": "D:/ProgramFiles/Coding/VSCode/.vscode/SumatraPDF.exe",
"latex-workshop.view.pdf.external.synctex.args": [
"-forward-search",
"%TEX%",
"%LINE%",
"-reuse-instance",
"-inverse-search",
"D:/ProgramFiles/Coding/VSCode/code D:/ProgramFiles/Coding/VSCode/resources/app/out/cli.js -r -g %f:%l",
"%PDF%"
],