VSCode で LaTeX をビルドできるようにする


環境

Windows 10 Home
Visual Studio Code バージョン 1.51.1
LaTeX Workshop v8.14.0

発生した問題

Visual Studio CodeにLaTeX Workshopを入れてビルドしようと思ったら、以下のようなエラーが出た。

そこで、Open compiler logをクリックして出力を見てみると、以下のような表示が。

No pages of output.
Transcript written on c:/Users/ユーザー名/Desktop/textest/1.log.
Latexmk: Log file says no output from latex
Latexmk: For rule 'pdflatex', no output was made
Collected error summary (may duplicate other messages):
  pdflatex: Command for 'pdflatex' gave return code 1
      Refer to 'c:/Users/ユーザー名/Desktop/textest/1.log' for details
Latexmk: Use the -f option to force complete processing,
 unless error was exceeding maximum runs, or warnings treated as errors.
=== TeX engine is 'pdfTeX'
Latexmk: Errors, so I did not complete making targets

どうやら、Latexmkからpdflatexが呼び出されている。TeXworksを使っていた時に、pdflatexは日本語を扱えないという制約があったことを思い出し、その時と同様にptex2pdfを使うように変更した。

解決法

以下をsettings.jsonに記述する。

settings.json
{
    "latex-workshop.latex.recipes": [
        {
            "name": "ptex2pdf",
            "tools": [
                "ptex2pdf"
            ]
        }
    ],
    "latex-workshop.latex.tools": [
        {
            "name": "ptex2pdf",
            "command": "ptex2pdf",
            "args": [
                "-l",
                "-ot",
                "-kanji=utf8 -synctex=1",
                "%DOC%"
            ]
        }
    ],
    "latex-workshop.view.pdf.viewer": "tab",
}

これで、Visual Studio Code上でビルドができるようになり、PDFのプレビューもできるようになった。

参考

TeXworks/設定
VSCodeプラグイン「LaTeX Workshop」の設定