在Windows中,配置VScode做爲LaTeX的編輯器(IDE),並使用SumatraPDF預覽PDF文件。主要是LaTeX Workshop
擴展的設置,若是要使用外部的PDF閱讀器,則還須要設置SumatraPDF。html
ctrl+shift+x
調出應用商店,搜索並安裝LaTeX Workshop
擴展:git
LaTeX Workshop
LaTeX文件.tex
的編譯、預覽、輔助文件的清理等都在擴展latex workshop
中設置。github
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"或https://github.com/James-Yu/LaTeX-Workshop/wiki/Compile#latex-recipes瞭解配置狀況。json
LaTeX Workshop
默認的編譯方式爲latexmk
和pdflatex
。以下配置修改了latex workshop
的編譯方式、預覽、輔助文件等。特別添加了xelatex及包含bib時的編譯。app
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
):ide
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}
正向搜索是指按某種方式操做源文件中的部分,會在PDF文件中當即到達對應的部分。反向搜索就是在PDF文檔的正文中某個位置點擊,光標會定位到源文件的相應位置。正反向搜索十分方便文檔錯誤的修改。反向搜索須要用到SumatraPDF軟件(或Zathura)。ui
SumatraPDF軟件是一款小巧強悍的PDF閱讀器,是免費開源軟件。國內有多個PDF閱讀器基於SumatraPDF修改而來。SumatraPDF官方下載地址:
https://www.sumatrapdfreader.org/download-free-pdf-viewer.html
根據本身電腦操做系統,選擇相應的軟件下載並安裝。操作系統
下面操做的一些說明:
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"
D:/ProgramFiles/Coding/VSCode/.vscode/
,後面的設置只是針對這個位置的SumatraPDF.exe,設置不會影響其餘位置的SumatraPDF.exe。由於我有時會使用Sumblime Text3,默認的SumatraPDF.exe已經對其設置。步驟以下:
打開設置文件settings.json
,添加以下設置(注意查找重複的配置項,如前面的配置中最後一行有latex-workshop.view.pdf.viewer
,用下面的替換):
// 設置預覽方式 "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.exe D:/ProgramFiles/Coding/VSCode/resources/app/out/cli.js -r -g %f:%l", "%PDF%" ],
"D:\ProgramFiles\Coding\VSCode\Code.exe" "D:\ProgramFiles\Coding\VSCode\resources\app\out\cli.js" -r -g "%f:%l"
ctrl+alt+v
打開PDF文件,在正文中雙擊鼠標左鍵,會切換到了源文件的相應位置。ctrl+alt+x
,找到"navigator,select and edit",點擊第一項"syncTeX from cursor"(或右鍵選擇,或快捷鍵ctrl+alt+j
),會切換到PDF文件的相應位置。若是不成功,檢查路徑設置,或者文件名錯誤。
Viewing & Synctex
使用VSCode編寫LaTeX
"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*2", "tools": [ "xelatex", "bibtex", "xelatex", "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.autoClean.run": "onBuilt", "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", "*.bcf", "*.run.xml" ], "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->PDF) "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%" ],