VScode 配置爲 LaTeX 編輯器(IDE)

VScode 配置爲 LaTeX IDE

在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.recipeslatex-workshop.latex.Tools定義。第一個食譜的tools域中的工具定義在第二個食譜。latex workshop的參數含義見LaTeX Workshop的配置文件"package.json"或https://github.com/James-Yu/LaTeX-Workshop/wiki/Compile#latex-recipes瞭解配置狀況。json

LaTeX Workshop默認的編譯方式爲latexmkpdflatex。以下配置修改了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}

設置LaTeX文件和PDF文件之間的正/反向搜索

正向搜索是指按某種方式操做源文件中的部分,會在PDF文件中當即到達對應的部分。反向搜索就是在PDF文檔的正文中某個位置點擊,光標會定位到源文件的相應位置。正反向搜索十分方便文檔錯誤的修改。反向搜索須要用到SumatraPDF軟件(或Zathura)。ui

SumatraPDF軟件是一款小巧強悍的PDF閱讀器,是免費開源軟件。國內有多個PDF閱讀器基於SumatraPDF修改而來。SumatraPDF官方下載地址:
https://www.sumatrapdfreader.org/download-free-pdf-viewer.html
根據本身電腦操做系統,選擇相應的軟件下載並安裝。操作系統

下面操做的一些說明:

  • 個人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已經對其設置。
  • 使用下面的設置,須要修改下面其中的VScode.exe、cli.js、SumatraPDF.exe這三個文件的路徑爲本身電腦對應的路徑。

步驟以下:

  1. 打開設置文件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%"
    ],
  2. 配置反向搜索(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中所對應的內容的源代碼處
  3. 反向搜索:打開一個已經編譯的TeX文件,ctrl+alt+v打開PDF文件,在正文中雙擊鼠標左鍵,會切換到了源文件的相應位置。
  4. 正向搜索:將光標移動到TeX文件的正文。ctrl+alt+x,找到"navigator,select and edit",點擊第一項"syncTeX from cursor"(或右鍵選擇,或快捷鍵ctrl+alt+j),會切換到PDF文件的相應位置。
  5. 若是不成功,檢查路徑設置,或者文件名錯誤。

參考文章

Viewing & Synctex
使用VSCode編寫LaTeX

最後的設置(sumatraPDF預覽+正反搜索)

"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%"
    ],

有幫助,打賞支持!

avatar

相關文章
相關標籤/搜索