使用 VS Code 編寫 LaTeX 論文時,安裝 LaTeX Workshop 插件可以實現非常多的功能,但是由於 LaTeX Workshop 默認配置的編譯命令是 latexmk
,而在編寫中文論文時通常需要使用 xelatex
命令來編譯文件源代碼,所以為了正常使用 LaTeX Workshop 編寫中文論文,通常需要對 LaTeX Workshop 進行自定義修改。
以下是筆者根據官方文檔自己修改的設置選項信息,每一項的設置上面都寫好了中文注釋,也為了日后筆者更方便的進行配置、修改。
關於 LaTeX Workshop 的配置官方文檔信息,可以參考 LaTeX Workshop GitHub Wiki
本文給出 3 種編譯方式:
- 使用
xelatex
命令編譯兩次
通常生成目錄時,通常先編譯一次生成目錄所需的輔助文件,例如目錄項等,然后編譯第二遍結合輔助文件生成最終的 PDF
- 使用
BibTeX
參考文獻工具時所需用到的編譯命令 - 使用
BibLaTeX
參考文獻所需用到的編譯命令
// ******** LaTeX Workshop 配置信息 ********
// 文件修改時不自動編譯
// "never", "onSave", "onFileChange"
"latex-workshop.latex.autoBuild.run": "never",
// LaTeX Workshop 編譯源代碼文件的快捷鍵默認為:ctrl + alt + b
// 但是在有些情況下,ctrl + alt 快捷鍵被占用
// 將下面設置項改為 true 可以啟動替代的快捷鍵
// ctrl + l / alt + letter
"latex-workshop.bind.altKeymap.enabled": false,
// 編譯文件時選用哪種 recipes 方案
// recipes 的定義在下文
// "first" (默認)為定義在下文 recipes 中的第一項
// "lastUsed" 為上次使用運行的 recipe
"latex-workshop.latex.recipe.default": "lastUsed",
// 預覽生產的 pdf 文件方式:在 vscode 窗口中預覽
"latex-workshop.view.pdf.viewer": "tab",
// 設置在使用 LaTeX Workshop 編譯后,自動清理輔助文件
// 也可以設置為 "never" 表示不自動清理輔助文件
// 設置 "onFailed" 為當編譯失敗時自動清理輔助文件
"latex-workshop.latex.autoClean.run": "onBuilt",
// 編譯 LaTeX 時使用的工具(tool)順序
// 工具(tool)需要自定義
"latex-workshop.latex.recipes": [
// 沒有參考文獻的編譯方式
// 為了正確生成目錄項,一般需要編譯兩次源代碼
{
"name": "xelatex",
"tools": [
"xelatex",
"xelatex"
]
},
// 使用 BibTeX 參考文獻工具的編譯方式
{
"name": "xelatex ➞ bibtex ➞ xelatex × 2",
"tools": [
"xelatex",
"bibtex",
"xelatex",
"xelatex"
]
},
// 使用 BibLaTeX 參考文獻工具的編譯方式
{
"name": "xelatex ➞ biber ➞ xelatex × 2",
"tools": [
"xelatex",
"biber",
"xelatex",
"xelatex"
]
}
],
// 定義 recipes 中工具的命令以及參數
// 以下列出 LaTeX Workshop 定義好的占位符
// %DOC% The root file full path without the extension
// %DOC_W32% The root file full path without the extension with \ path separator on Windows
// %DOCFILE% The root file name without the extension
// %DOC_EXT% The root file full path with the extension
// %DOC_EXT_W32% The root file full path with the extension with \ path separator on Windows
// %DOCFILE_EXT% The root file name with the extension
// %DIR% The root file directory
// %DIR_W32% The root file directory with \ path separator on Windows
// %TMPDIR% A temporary folder for storing ancillary files
// %OUTDIR% The output directory configured in latex-workshop.latex.outDir
// %OUTDIR_W32% The output directory configured in latex-workshop.latex.outDir with \ path separator on Windows
// %WORKSPACE_FOLDER% The current workspace path
// %RELATIVE_DIR% The root file directory relative to the workspace folder
// %RELATIVE_DOC% file root file path relative to the workspace folder
"latex-workshop.latex.tools": [
{
"name": "xelatex",
"command": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
],
"env": {}
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
],
"env": {}
},
{
"name": "biber",
"command": "biber",
"args": [
"%DOCFILE%"
],
"env": {}
}
],
使用以上配置選項將上述代碼拷貝到你的 VS Code 的 json
設置文件下即可。