LoginSignup
30
17

More than 1 year has passed since last update.

Windows Subsystem for LinuxとVSCodeで快適LaTeX

Last updated at Posted at 2020-06-17

概要

Windows Subsystem for Linux(WSL)のUbuntuと、VSCodeを使って快適なLaTeX環境を構築します。

令和3年1月9日改訂
lualatexを利用するように変更しました。

環境
Windows 10

LaTeXってなんだって方へ

わかってる方は次へお進みください。
LaTeXとは、文書処理システムの一つです。

HTMLみたいにテキストに印をつけながら記述していくというなんとも面倒そうなものですが、実際は段落の処理やフォントの設定を勝手にしてくれたり、科学論文やレポートなどに多く採用されていたり、Wordと違ってテキストベースなのでたくさん書いても動作が軽快です。あとはなんと言っても簡単にきれいな数式が書けるのがいい。

私は最近大学の授業で習いましたが、もっと早く使えるようにっておけば、Wordで苦戦して数式を入力しなくてよかったのに!って思いました。
理系なら必須のスキルでしょう。

もっと詳しく知りたい方は、こちらを見ていただきたいです。

WSLの導入

WSLを利用するには先にWSLの有効化を行っておく必要があります。
Qiita内にも記事は多数ありますので、適宜参照してください。
https://www.atmarkit.co.jp/ait/articles/1903/18/news031.html
https://qiita.com/rubytomato@github/items/fdfc0a76e848442f374e

今回は、上記2つの記事の内容が完了できているとして話をすすめます。
準備として、Ubuntuのコマンドラインで以下を順に実行します。

Terminal
sudo apt update
sudo apt upgrade
sudo apt install texlive-full
sudo cpan Log::Log4perl Log::Dispatch

VSCodeの導入

Windows側VSCodeをインストールしてください。
WSL側でないので注意してください。

そして、拡張機能として
Remort-WSL
LaTeX Workshopをインストールします。

LaTeX WorkshopはWSLにインストールしてください。(ややこしいな)
インストールのボタンが「インストールonWSL」になってたら大丈夫かと。

ここからはお好みですが、
私はVimの操作感にしたいのでVimと、見やすいカラースキームのMonokaiをインストールしています。今回必須ではないので、やり方はご自身で調べてください。

LaTeX環境整備

ここからが本番です。
Ctrl+Shift+Pを押してコマンドパレットを起動してopen settingと入力し、
基本設定:設定(JSON)を開く
を選択してください。
jsonファイルが表示されるので、

settings.json
{
    //もし元から{}の中にになにか書いてあれば、ここから↓
    "latex-workshop.intellisense.package.enabled": true,
    "latex-workshop.view.pdf.viewer": "tab",
    "latex-workshop.latex.tools": [
        {
            "name": "lualatex",
            "command": "lualatex",
            "args": [
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOCFILE%.tex"
            ]
        },
        {
            "name": "platex",
            "command": "wsl.exe",
            "args": [
                "platex",
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "-kanji=utf8",
                "-guess-input-enc",
                "%DOCFILE%.tex"
            ]
        },
        {
            "name": "dvipdfmx",
            "command": "wsl.exe",
            "args": [
                "dvipdfmx",
                "-f",
                "yu-win10.map",
                "%DOCFILE%"
            ]
        }
    ],
    "latex-workshop.latex.recipes": [
        {
            "name": "lualatex",
            "tools": [
                "lualatex",
            ]
        },
        {
            "name": "platex",
            "tools": [
                "platex",
                "platex",
                "dvipdfmx"
            ]
        }
    ],
    "editor.formatOnSave": true,
    //ここまでをコピーして{}内に追記する
}

ここからはやりたい人だけやってください。
次に、左下の歯車のボタンをクリックして、「ユーザースペニット」を選択して、latexを選んでください。

latex.json
{
    "document": {
        "prefix": "document",
        "body": [
            "%lualatex-jaの設定",
            "\\documentclass[${1:report},a4paper,titlepage]{jlreq}",
            "\\usepackage{luatexja}",
            "\\usepackage[haranoaji,match,scale=0.924865]{luatexja-preset}",
            "\\usepackage[math-style=TeX,bold-style=ISO]{unicode-math}",
            "\\usepackage{graphicx,xcolor}",
            "\\usepackage{listings}",
            "%\\usepackage{tikz}",
            "%\\usepackage{circuitikz}",
            "\\usepackage[top=20mm,bottom=20mm,right=20mm,left=20mm]{geometry}",
            "%\\usepackage[top=20mm,bottom=20mm,right=15mm,left=25mm]{geometry}",
            "\\usepackage[hang,small,bf]{caption}",
            "\\usepackage[subrefformat=parens]{subcaption}",
            "\\captionsetup{compatibility=false}",
            "",
            "%使用するフォントの指定",
            "\\setmainfont{Latin Modern Roman}[",
            "\tOpticalSize = 0,",
            "\tSmallCapsFont = lmromancaps10-regular,",
            "\tSlantedFont = lmromanslant10-regular,",
            "\tFontFace = {sb}{n}{lmromandemi10-regular}",
            "]",
            "\\setsansfont{Source Sans Pro}[",
            "\tFontFace = {l}{n}{SourceSansPro-Light},",
            "\tBoldFont = SourceSansPro-Semibold",
            "]",
            "\\setmonofont{Source Code Pro}[Scale = 0.875]",
            "",
            "\\definecolor{keywordColor}{rgb}{0.50,0.00,0.33} %キーワード色(darkviolet)",
            "\\definecolor{commentColor}{rgb}{0.25,0.50,0.37} %コメント色(green)",
            "\\definecolor{stringColor}{rgb}{0.16,0.00,1.00} %文字色(blue)",
            "\\lstset{",
            "\tlanguage = ${2:[11]C++},",
            "\tbackgroundcolor={\\color[gray]{.98}},",
            "\tbreaklines = true,",
            "\tbreakindent = 10pt,",
            "\tbasicstyle = \\ttfamily\\footnotesize,",
            "\tclassoffset = 0,",
            "\tkeywordstyle={\\bfseries\\color{keywordColor}},",
            "\tcommentstyle={\\color{commentColor}},",
            "\tstringstyle={\\color{stringColor}},",
            "\tframe = tBRl,",
            "\tframeround = tttt,",
            "\tframesep = 5pt,",
            "\tnumbers = left,",
            "\tnumberstyle={\\ttfamily\\scriptsize},",
            "\tstepnumber = 1,",
            "\ttabsize = 2,",
            "\tcaptionpos = t",
            "}",
            "\\renewcommand{\\thesection}{\\mdseries\\S \\arabic{section}}",
            "\\renewcommand{\\thesubsection}{\\thesection.\\arabic{subsection}}",
            "\\renewcommand{\\lstlistingname}{Program}",
            "\\makeatletter",
            "\t\\AtBeginDocument{",
            "\t\\renewcommand*{\\thelstlisting}{\\arabic{section}.\\arabic{subsection}.\\arabic{lstlisting}}",
            "\t\\@addtoreset{lstlisting}{section}}",
            "\\makeatother",
            "\\begin{document}",
            "\\title{${3}}",
            "\\author{${4}}",
            "\\date{${5:\\和暦\\today}}",
            "\\maketitle",
            "",
            "$0",
            "",
            "\\end{document}"
        ],
        "description": "set lualatex document template"
    },
    "Figure": {
        "prefix": "figure",
        "body": [
            "\\begin{figure}[${1:hbp}]",
            "\\centering",
            "\\includegraphics[${2:width=\\linewidth}]{$3}",
            "\\caption{$4}",
            "\\label{fig:$5}",
            "\\end{figure}"
        ],
        "description": "set figure"
    },
    "Table": {
        "prefix": "table",
        "body": [
            "\\begin{table}[${1:hbp}]",
            "\\centering",
            "\\caption{$2}",
            "\\label{tabl:$3}",
            "\\begin{tabular}{$4}\\hline",
            "$5",
            "\\end{tabular}\\end{table}"
        ],
        "description": "set table"
    },
    "Answer": {
        "prefix": "answer",
        "body": [
            "\\quad□",
        ],
        "description": "qed/answer"
    },
    "Lim": {
        "prefix": "limit",
        "body": [
            "\\lim_{${1:x} \\to $2} $0",
        ],
        "description": "set limit"
    },
    "Sum": {
        "prefix": "sum",
        "body": [
            "\\sum_{${1:i} = ${2:0}}^{${3:n}} $0",
        ],
        "description": "set sum"
    },
    "displayinline": {
        "prefix": "displayinline",
        "body": [
            "$ \\displaystyle $0 $",
        ],
        "description": "set displaystyleinline"
    },
}

内容は自分に使いやすいように適当に変更してみてください。

テンプレートはGitHubにも上げてあります

いざLaTeX

さて、準備が整ったので記述していきましょう。

Ubuntuを起動して、code .で起動するか、
VSCodeを起動して左端の><をクリックしてWSL:New Windowで起動するかはおまかせします。

LaTeXソースの書き方は省略します。(今回は環境整備メインなので...)
なかなか奥が深いので、何もわからない方はじっくり勉強なさってください。
保存すると、自動でタイプセットされ、PDFファイルが出来上がります。

タイプセットした内容を確認するのも、evinceなんかのビューアは不要です。
なんせ、VSCode内でCtrl+Alt+Vでビューアを起動しその場で確認できるのですから。
(一応ショートカットキーを使わない方法を説明しておくと、右上にあるファイルアイコンに虫眼鏡がついているアイコンをクリックするとみることができます。)

30
17
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
30
17