LoginSignup
19
19

More than 3 years have passed since last update.

[LaTex]VSCodeとWSLで作るLaTex環境構築の備忘録

Last updated at Posted at 2019-05-16

TL;DR

Windows環境においてのLatex環境構築の備忘録です。
巷でWSL2などWindows環境が期待される分、需要は増えるかな?と思い記事にしました。
下記の手順をたどれば、あなたも論文執筆 or 技術書執筆 マスター!!

①WSL(Windows Subsystem for Linux)の導入

WSLの導入はここを参考にするとうまくいくはずです。(端的でわかりやすかった。)
本記事では、リンク先同様 WSLのOSとしてubuntuを使用することにします。
導入完了後、
wsl上において、次のコマンドで texlive の日本語版と理系向けパッケージをインストールします。

Ubuntu上
sudo apt install texlive-lang-japanese texlive-science

②Visual Studio Codeのインストール

Visual Studio Code はMS社高級なメモ帳です。
Visual Studio とは少々異なり、高級メモ帳から個々人の好きな環境を設定していくことになります。
ここからそぞれぞれの環境にあったものでインストールします。
ここに関しては特になし。なはず...

③各種設定

ツールの設定

ツールといいますか、VSCodeの拡張機能でのファイルパスの扱い方とWSL側の扱い方が少し異なるので、整合性を合わせてあげる必要があります。
そこで、自作(参考文献からお借りする)のシェルスクリプトで毎回自動で整合性を合わせます。
ここではCドライブ直下に下記のlatex-tools.shをおき、後述の設定で呼び出しがかかるようにしておきます。

latex-tools.sh
#!/bin/bash
#Windows特有の c:/hoge/piyo → /mnt/c/hoge/piyo に書き換えてコマンドを実行するshellscript
newArgs=()
for arg in $@; do
      if [ ${arg:1:1} = : ]; then
          arg=/mnt/${arg:0:1}${arg#*:}
      fi
          newArgs+=( $arg )
      done
${newArgs[@]}

VSCode側の設定

VSCodeの左にあるタブからExtensionsを開きます。(下記のようなマーク)  

そこで、"LaTeX Workshop"と検索をかけてインストールします。
これはVSCode上でLaTexを扱いやすくする拡張機能です。
次に、
Ctrl + ,
で設定画面を開き、右上の方にある { } ボタンでsetting.jsonを開きます。
そして下記を追記します。

setting.json
....
....
....

"latex-workshop.latex.tools": [
    {
        "name": "latexmk",
        "command": "wsl.exe",
        "args": [
            "/mnt/c/latex-tools.sh",
            "latexmk",
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "-pdf",
            "%DOC%"
        ]
    },
    {
        "name": "pdflatex",
        "command": "wsl.exe",
        "args": [
            "/mnt/c/latex-tools.sh",
            "pdflatex",
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "%DOC%"
        ]
        },
    {
        "name": "bibtex",
        "command": "wsl.exe",
        "args": [
            "/mnt/c/latex-tools.sh",
            "bibtex",
            "%DOCFILE%"
        ]
    },
    {
        "name":"ptex2pdf",
        "command": "wsl.exe",
        "args": [
            "/mnt/c/latex-tools.sh",
            "ptex2pdf",
            "-l",
            "-ot",
            "-kanji=utf8 -synctex=1",
            "-interaction=nonstopmode",
            "%DOC%"
        ]
    },
    {
        "name":"ptex2pdf (uplatex)",
        "command": "wsl.exe",
        "args": [
            "/mnt/c/latex-tools.sh",
            "ptex2pdf",
            "-l",
            "-u",
            "-ot",
            "-kanji=utf8 -synctex=1",
            "-interaction=nonstopmode",
            "%DOC%"
        ]
    },
    {
        "name": "pbibtex",
        "command": "wsl.exe",
        "args": [
            "/mnt/c/latex-tools.sh",
            "pbibtex",
            "-kanji=utf8",
            "%DOCFILE%"
        ]
    }
],

//レシピの設定

"latex-workshop.latex.recipes": [
    {
        "name": "ptex2pdf",
        "tools": [
            "ptex2pdf"
        ]
    },
    {
        "name": "latexmk",
        "tools": [
            "latexmk"
        ]
    },
    {
        "name": "pdflatex",
        "tools": [
            "pdflatex",
        ]
    },
    {
        "name": "pdflatex -> bibtex -> pdflatex",
        "tools": [
            "pdflatex",
            "bibtex",
            "pdflatex",
        ]
    },
    {
        "name": "ptex2pdf -> pbibtex -> ptex2pdf",
        "tools": [
            "ptex2pdf",
            "pbibtex",
            "ptex2pdf",
        ]
    },
    {
        "name": "ptex2pdf (uplatex)",
        "tools": [
            "ptex2pdf (uplatex)",
        ]
    },
    {
        "name": "ptex2pdf (uplatex) -> pbibtex -> ptex2pdf (uplatex)",
        "tools": [
            "ptex2pdf (uplatex)",
            "pbibtex",
            "ptex2pdf (uplatex)",
        ]
    },
]

適当なTexファイルを用意して、Ctrl + Shift + Pでコマンドパレットを開き、Build with recipe を選択し任意のコンパイルが通るはずです。

ちなみにdefaultでは保存するたびBuildされるので、嫌な人はLatex-Workshopの設定変更が必要です。

よき執筆ライフを!

参考文献

WSLに入れたLaTeXをWindowsのVS Codeで使う

何かありましたら、お気軽にどうぞ(編集ガバをしている可能性が高い)

19
19
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
19