LoginSignup
2
5

More than 1 year has passed since last update.

LaTeX+VSCodeの環境構築につまづいた人へ~コンパイル編~

Posted at

はじめに

VSCode で LaTeX を使うとき,LaTeX Workshop という拡張機能を入れることが多いです.しかし,LaTeX Workshop のデフォルト設定では,日本語文書のコンパイルはできません.本記事では,その原因と対処法について説明します.

環境

  • TeXLive
  • Windows10
  • VSCode

環境構築

コンパイルエラーの話に重点を置くので簡単に

  • TeXLiveをインストール
  • VSCodeをインストール
  • VSCodeでLaTeX Workshopをインストール

デフォルト設定とコンパイルエラー

Vscode で latex を使うときには,さまざまな設定を settings.json で編集する必要があります.LaTeX のコンパイルで重要なのは,

  • latex-workshop.latex.recipes
  • latex-workshop.latex.tools

です.文書コンパイル時,LaTeX Workshop は設定された recipe にしたがって一連のコマンド(tool)を順次実行します.デフォルトでは次の2種類のrecipeが定義されています.

settings.json
"latex-workshop.latex.recipes": [
{
	"name": "latexmk 🔃",
	"tools": [
	"latexmk"
	]
},
{
	"name": "pdflatex ➞ bibtex ➞ pdflatex × 2",
	"tools": [
	"pdflatex",
	"bibtex",
	"pdflatex",
	"pdflatex"
	]
}
]

“name”で定義した"latexmk 🔃"と"pdflatex ➞ bibtex ➞ pdflatex × 2"がrecipeとしてCOMMANDS内に表れます.
2023-02-27 (3).png
それぞれのアイコンをクリックすると,nameに対応したtoolが実行されます.また,"Build LaTeX project"をクリックした場合には,一番上の recipe (今回はlatexmk 🔃)が実行されます.recipe 内の tool の詳細は,latex-workshop.latex.toolsに定義されています.デフォルトでは以下のようになります.

settings.json
"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%"
	]
}
]

例えば,latexmk 🔃 をクリックして test.tex をコンパイルした場合には,次のコマンドを terminal で実行したのと同じです.

>latexmk -synctex=1 -interaction=nonstopmode -file-line-error -pdf test.tex

このとき,test.tex は日本語非対応の pdflatex を用いてコンパイルされます.多くの場合,このせいで日本語の tex ファイルはコンパイルできないでしょう.一方,次のような英語の tex ファイルはこれでコンパイルできます.

test.tex
\documentclass[12pt]{article}
\title{How to Set Up LaTeX on VSCode}
\begin{document}
Hello \LaTeX!
\end{document}

日本語の tex ファイルをコンパイルする場合,pdflatex ではなく platex, uplatex, lualatex のいずれかを用いてコンパイルする必要があります.

日本語文書をコンパイルする

本記事では,近年の主流である uplatex でコンパイルする際の方法を2通り説明します.

latexmk と .latexmkrc ファイルを用いる

C:\Users\USERNAME フォルダに次の .latexmkrc ファイルを置きます.

.latexmkrc
#!/usr/bin/env perl
# LaTeX
$latex        = 'uplatex %O -halt-on-error -interaction=nonstopmode -file-line-error -synctex=1  %S';
# BibTeX
$bibtex       = 'upbibtex -kanji=uft8 %O %B';
$biber        = 'biber --bblencoding=utf8 -u -U --output_safechars %O %S';
# index
$makeindex    = 'mendex %O -o %D %S';
# DVI / PDF
$dvipdf       = 'dvipdfmx %O -o %D %S';
$max_repeat   = 5;
$pdf_mode     = 3;
$pvc_view_file_via_temporary = 0;

settings.json に次を書き加えます.

settings.json
    "latex-workshop.latex.tools": [{
        "name": "latexmk(.latexmkrc)",
        "command": "latexmk",
        "args": [
            "-cd",
            "%DOCFILE%"
        ]
    }],
    "latex-workshop.latex.recipes": [{
        "name": "latexmk(.latexmkrc)",
        "tools": [
          "latexmk(.latexmkrc)"
        ] 
    }]

この場合,.latexmkrc ファイルに書かれているようにコンパイルが行われます.

latexmk を用いるが,settings.json に直接 option を記述する

デフォルトの latex-workshop.latex.tools を次のように書き換えます.

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

.latexmkrc ファイルに書かれていたことを latex-workshop.latex.tools に直接書きました.
こちらの方法は .latexmkrc ファイルが不要なので楽です.

参考文献

https://texwiki.texjp.org/?Visual%20Studio%20Code%2FLaTeX
https://texwiki.texjp.org/?Latexmk
https://texwiki.texjp.org/?dvipdfmx
http://www2.yukawa.kyoto-u.ac.jp/~koudai.sugimoto/dokuwiki/doku.php?id=latex:latexmk%E3%81%AE%E8%A8%AD%E5%AE%9A
https://blog.sh4869.sh/post/2019/10/10/vscode-latexmk-setting/
https://github.com/James-Yu/LaTeX-Workshop/wiki/Compile
https://marketplace.visualstudio.com/items?itemName=James-Yu.latex-workshop
https://znuko.github.io/nukodocs/tex/latexmk/
https://qiita.com/wtsnjp/items/76557b1598445a1fc9da
https://qiita.com/Rumisbern/items/d9de41823aa46d5f05a8

2
5
0

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
2
5