VScode 配置為 LaTeX IDE
在Windows中,配置VScode作為LaTeX的編輯器(IDE),并使用SumatraPDF預覽PDF文件。主要是LaTeX Workshop擴展的設置,如果要使用外部的PDF閱讀器,則還需要設置SumatraPDF。
安裝擴展
ctrl+shift+x
調出應用商店,搜索并安裝LaTeX Workshop擴展:
配置LaTeX Workshop
LaTeX文件.tex的編譯、預覽、輔助文件的清理等都在擴展latex workshop中設置。
LaTeX Workshop中,編譯LaTeX文件時順序執(zhí)行的一系列/組命令稱為LaTeX食譜(recipes),定義在latex-workshop.latex.recipes。缺省情況,LaTeX Workshop包含2個基本的食譜,分別由latex-workshop.latex.recipes和latex-workshop.latex.Tools定義。第一個食譜的tools域中的工具定義在第二個食譜。latex workshop的參數(shù)含義見LaTeX Workshop的配置文件"package.json"或https://github.com/James-Yu/LaTeX-Workshop/wiki/Compile#latex-recipes了解配置情況。
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, //根據(jù)加載的包,自動完成命令或包 "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):
其他常用的快捷鍵:
"latex-workshop.view.pdf.viewer": "tab"
;時,在LaTeX源文件中按下快捷鍵,定位到PDF文檔相應位置。(反向搜索見后面)一些快捷處理
根據(jù)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官方下載地址:https://www.sumatrapdfreader.org/download-free-pdf-viewer.html
根據(jù)自己電腦操作系統(tǒng),選擇相應的軟件下載并安裝。
下面操作的一些說明:
我的VScode文件位置:VScode.exe在D:/ProgramFiles/Coding/VSCode
文件夾
擴展安裝在D:/ProgramFiles/Coding/VSCode/.vscode/
。即將默認的%USERPROFILE%\.vscode和%APPDATA%\Code轉移到了D:/ProgramFiles/Coding/VSCode
文件夾中,然后建立了聯(lián)接:
將SumatraPDF.exe復制到D:/ProgramFiles/Coding/VSCode/.vscode/,后面的設置只是針對這個位置的SumatraPDF.exe,設置不會影響其他位置的SumatraPDF.exe。因為我有時會使用Sumblime Text3,默認的SumatraPDF.exe已經(jīng)對其設置。
使用下面的設置,需要修改其中的VScode.exe、cli.js、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的正向搜索(這是官網(wǎng)的) "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%" ],
配置反向搜索(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中所對應的內容的源代碼處
反向搜索的使用:打開一個已經(jīng)編譯的TeX文件,ctrl+alt+v
打開PDF文件。在PDF文件中的某行文字處,雙擊鼠標左鍵,會切換到了源文件的相應位置。如果點擊的是公式、圖等,可能不會正確返回。
正向搜索的使用:將光標移動到TeX文件的正文。ctrl+alt+x
,找到"navigator,select and edit
",點擊第一項"syncTeX from cursor
"(或右鍵選擇,或快捷鍵ctrl+alt+j
),會切換到PDF文件的相應位置。
如果不成功,檢查路徑設置,或者文件名錯誤。正反向搜索用到了.synctex.gz文件,不能刪除。
參考文章
Viewing Synctex
使用VSCode編寫LaTeX
最后的設置(sumatraPDF預覽+正反搜索)
"latex-workshop.showContextMenu":true, "latex-workshop.intellisense.package.enabled": true, //根據(jù)加載的包,自動完成命令或包 "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%" ],
到此這篇關于詳解VScode 配置為 LaTeX 編輯器(IDE)的文章就介紹到這了,更多相關VScode LaTeX編輯器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!