[[Visual Studio Code]] > LaTeX

----
#contents
----

*LaTeX 環境の整備 [#rd4c94a6]

**LaTeX Workshop [#b34f0500]

LaTeX Workshop は Visual Studio Code に LaTeX の統合環境を追加する extension です。

-[[LaTeX Workshop:https://marketplace.visualstudio.com/items?itemName=James-Yu.latex-workshop]]
--https://marketplace.visualstudio.com/items/James-Yu.latex-workshop/changelog
-https://github.com/James-Yu/LaTeX-Workshop
--https://github.com/James-Yu/LaTeX-Workshop/commits/master
--https://github.com/James-Yu/LaTeX-Workshop/blob/master/CHANGELOG.md
-[[MyTeXpert:Visual Studio Code/LaTeX]]
-[[vs code で latex:https://kandai.hatenadiary.jp/entry/2019/02/21/091432]]
-[[VSCode で LaTeX を書く (2018):https://qiita.com/popunbom/items/7d02fc75ad23b735d28c]]
-[[最速で最小構成のLaTeX環境をVSCodeに構築する:https://qiita.com//geek_shanshan/items/d2b6218bbd66e3f62497]]

***日本語文書向けの設定 [#f7cc6d6c]

Ctrl+Comma で Settings タブを開きます。
「設定の検索」で「Tools」を入力して「Latex-workshop > Latex: Tools」を探して「settings.json で編集」をクリックします。
左側に既定のユーザー設定、右側にユーザー設定 (settings.json) が開かれます。
左側に表示されている既定のユーザー設定の latex-workshop.latex.tools は以下のように設定されています。

   "latex-workshop.latex.tools": [
     {
       "name": "latexmk",
       "command": "latexmk",
       "args": [
         "-synctex=1",
         "-interaction=nonstopmode",
         "-file-line-error",
         "-pdf",
         "%DOC%"
       ]
     },
     {
       "name": "pdflatex",
       "command": "pdflatex",
       "args": [
         "-synctex=1",
         "-interaction=nonstopmode",
         "-file-line-error",
         "%DOC%"
       ]
     },
     {
       "name": "bibtex",
       "command": "bibtex",
       "args": [
         "%DOCFILE%"
       ]
     }
   ]

Windows で Latexmk-upLaTeX を使用する場合は右側に表示されている settings.json の {} の中に以下の設定を追加・保存して既定のユーザー設定を上書きします。

     "latex-workshop.latex.tools": [
       {
         "name": "latexmk",
         "command": "latexmk",
         "args": [
           "-e",
           "$latex=q/uplatex %O -kanji=utf8 -no-guess-input-enc -synctex=1 -interaction=nonstopmode -file-line-error %S/",
           "-e",
           "$bibtex=q/upbibtex %O %B/",
           "-e",
           "$biber=q/biber %O --bblencoding=utf8 -u -U --output_safechars %B/",
           "-e",
           "$makeindex=q/upmendex %O -o %D %S/",
           "-e",
           "$dvipdf=q/dvipdfmx %O -o %D %S/",
           "-norc",
           "-pdfdvi",
           "%DOC%"
         ]
       }
     ]

Windows で Latexmk-LuaLaTeX を使用する場合は右側に表示されている settings.json の {} の中に以下の設定を追加・保存して既定のユーザー設定を上書きします。

     "latex-workshop.latex.tools": [
       {
         "name": "latexmk",
         "command": "latexmk",
         "args": [
           "-e",
           "$lualatex=q/lualatex %O -synctex=1 -interaction=nonstopmode -file-line-error %S/",
           "-e",
           "$bibtex=q/upbibtex %O %B/",
           "-e",
           "$biber=q/biber %O --bblencoding=utf8 -u -U --output_safechars %B/",
           "-e",
           "$makeindex=q/upmendex %O -o %D %S/",
           "-norc",
           "-pdflua",
           "%DOC%"
         ]
       }
     ]

macOS, Linux で Latexmk-upLaTeX を使用する場合は右側に表示されている settings.json の {} の中に以下の設定を追加・保存して既定のユーザー設定を上書きします。

     "latex-workshop.latex.tools": [
       {
         "name": "latexmk",
         "command": "latexmk",
         "args": [
           "-e",
           "$latex=q/uplatex %O -synctex=1 -interaction=nonstopmode -file-line-error %S/",
           "-e",
           "$bibtex=q/upbibtex %O %B/",
           "-e",
           "$biber=q/biber %O --bblencoding=utf8 -u -U --output_safechars %B/",
           "-e",
           "$makeindex=q/upmendex %O -o %D %S/",
           "-e",
           "$dvipdf=q/dvipdfmx %O -o %D %S/",
           "-norc",
           "-pdfdvi",
           "%DOC%"
         ]
       }
     ]

macOS, Linux で Latexmk-LuaLaTeX を使用する場合は右側に表示されている settings.json の {} の中に以下の設定を追加・保存して既定のユーザー設定を上書きします。

     "latex-workshop.latex.tools": [
       {
         "name": "latexmk",
         "command": "latexmk",
         "args": [
           "-e",
           "$lualatex=q/lualatex %O -synctex=1 -interaction=nonstopmode -file-line-error %S/",
           "-e",
           "$bibtex=q/upbibtex %O %B/",
           "-e",
           "$biber=q/biber %O --bblencoding=utf8 -u -U --output_safechars %B/",
           "-e",
           "$makeindex=q/upmendex %O -o %D %S/",
           "-norc",
           "-pdflua",
           "%DOC%"
         ]
       }
     ]

***使い方 [#mdd9327c]

ファイルの内容を変更して Ctrl+S で保存するとビルドコマンドが実行されます。ファイルの内容を変更していない状態でビルドコマンドを実行したい場合は Ctrl+Alt+B でビルドコマンドを実行します。

Ctrl+Alt+V あるいは右上の PDF ファイルのアイコンを左クリックで指定した PDF ビューアで PDF ファイルを表示します。
タブで開く場合は右側に新しいタブで PDF ファイルを表示します。
ブラウザで開く場合は既定のブラウザで PDF ファイルを表示します。
Ctrl+Alt+J で forward search,Ctrl+左クリックで inverse search を行います。

Ctrl+Alt+X あるいは左側の Activity Bar の TeX アイコンをクリックで以下のリストが表示されリストの項目から Action を選択すると実行されます。

>
Build LaTeX project&br;
View LaTeX PDF&br;
View Log messages&br;
Navigate, select, and exit&br;
Miscellaneous&br;
<

**マジックコメント [#z988d211]

LaTeX Workshop は % !TEX root と % !TEX program のマジックコメントをサポートしています。

***root ファイル(メインファイル)の指定 [#k193d136]

LaTeX Workshop は root ファイル(メインファイル)を自動判別しますがうまく動作しない場合はマジックコメントで指定することができます。
root ファイル(メインファイル)を指定する場合は

 % !TEX root = relative/or/absolute/path/to/root/file.tex

のように設定します。

***ビルドコマンドの変更 [#x21aa1d8]

「普段は Latexmk-upLaTeX を使うように設定しているけれど特定の文書だけ Latexmk-LuaLaTeX を使いたい」といった場合は設定を変更しなくてもマジックコメントを追加して Latexmk-LuaLaTeX を使用することができます。

settings.json の {} の中に以下の設定を追加・保存します。

       "latex-workshop.latex.magic.args": [
         "-e",
         "$lualatex=q/lualatex %O -synctex=1 -interaction=nonstopmode -file-line-error %S/",
         "-e",
         "$bibtex=q/upbibtex %O %B/",
         "-e",
         "$biber=q/biber %O --bblencoding=utf8 -u -U --output_safechars %B/",
         "-e",
         "$makeindex=q/upmendex %O -o %D %S/",
         "-norc",
         "-pdflua",
         "%DOC%"
       ]

LaTeX 文書の root ファイル(メインファイル)に以下の行を追加します。

 % !TEX program = latexmk

Ctrl+S で保存あるいは Ctrl+Alt+B でビルドコマンドを実行すると Latexmk-LuaLaTeX が実行されます。

**タブで PDF を開く [#h0e50815]

settings.json の {} の中に以下の設定を追加・保存します。

     "latex-workshop.view.pdf.viewer": "tab"

**既定のブラウザで PDF を開く [#m1b4a870]

settings.json の {} の中に以下の設定を追加・保存します。

     "latex-workshop.view.pdf.viewer": "browser"

**SumatraPDF との連携 [#d61e968a]

***forward search [#ydcc4b93]

settings.json の {} の中に以下の設定を追加・保存します。

     "latex-workshop.view.pdf.viewer": "external",
     "latex-workshop.view.pdf.external.command": {
         "command": "C:/Program Files/SumatraPDF/SumatraPDF.exe",
         "args": [
           "-reuse-instance",
           "%PDF%"
         ]
     },
     "latex-workshop.view.pdf.external.synctex": {
         "command": "C:/Program Files/SumatraPDF/SumatraPDF.exe",
         "args": [
           "-reuse-instance",
           "-forward-search",
           "%TEX%",
           "%LINE%",
           "%PDF%"
         ]
     }

***inverse search [#he69ec2b]

SumatraPDF の [☰] &gt; [設定(S)] &gt; [オプション(O)...] の「逆順検索コマンドラインの設定」に以下の設定をします。

 "C:\Program Files\Microsoft VS Code\bin\code.cmd" -g "%f:%l"