0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MacでLaTeX環境を構築する

Posted at

はじめに

LaTeXは数理・技術文書の作成に優れたツールであり、数式・図表の挿入や注釈管理が容易であるため、学術用途で広く利用されている。

本稿では、Mac上でVS Codeから .tex を編集・コンパイルできるLaTeX環境を構築する手順について解説する。

各種インストール

Homebrewのインストール

HomebrewはmacOSおよびLinux用のパッケージマネージャーであり、コマンドから各種アプリケーションを導入可能とする。

1. Homebrew公式サイトにアクセスしインストールコマンドをコピー

https://brew.sh にアクセスし、画面上のインストールコマンドをコピーする。

2. ターミナルで貼り付けて実行

ターミナルを開き、コピーしたコマンドを貼り付けて実行する。指示に従い Enter を押す。

3. 表示されたコマンドを実行して PATH を通す

インストール後、ターミナルに表示される案内どおりにPATHを設定する(Apple Siliconの例)。

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

4. インストール確認

brew --version

バージョンが表示されればインストール成功である。

MacTeX(TeX Live)のインストール

Homebrewを用いてLaTeX環境を導入する。インストールには時間を要するため、余裕のあるときに実行することを推奨する。

1. MacTeX のインストール

brew install --cask mactex

パスワード入力を求められる場合がある。その後しばらく待機する。

2. TeX Liveのアップデート

sudo tlmgr update --self --all

3. デフォルト用紙サイズをA4に設定

sudo tlmgr paper a4

VS Codeのインストール

VS Codeは拡張性の高いテキストエディタであり、LaTeX開発においても有用である。

brew install visual-studio-code --cask

latexmkの設定

latexmkはLaTeX文書を自動コンパイルしてPDFを生成するツールである。ホームディレクトリに .latexmkrc を作成する。

.latexmkrc
#!/usr/bin/env perl

# LaTeX
$latex = 'platex -synctex=1 -halt-on-error -file-line-error %O %S';
$max_repeat = 5;

# BibTeX
$bibtex = 'pbibtex %O %S';
$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';
$pdf_mode = 3;

# preview
$pvc_view_file_via_temporary = 0;
if ($^O eq 'linux') {
    $dvi_previewer = "xdg-open %S";
    $pdf_previewer = "xdg-open %S";
} elsif ($^O eq 'darwin') {
    $dvi_previewer = "open %S";
    $pdf_previewer = "open %S";
} else {
    $dvi_previewer = "start %S";
    $pdf_previewer = "start %S";
}

# clean up
$clean_full_ext = "%R.synctex.gz";

保存先:/Users/自分のユーザー名/(ホームディレクトリ)
隠しファイル表示:Command + Shift + .

VS Codeの設定

1. 拡張機能「LaTeX Workshop」をインストール

2. settings.json に設定を追記

設定 → 右上 {} アイコンより settings.json を開く。

{
    "[tex]": {
        "editor.suggest.snippetsPreventQuickSuggestions": false,
        "editor.tabSize": 2
    },

    "[latex]": {
        "editor.suggest.snippetsPreventQuickSuggestions": false,
        "editor.tabSize": 2
    },

    "[bibtex]": {
        "editor.tabSize": 2
    },

    "latex-workshop.latex.clean.fileTypes": [
        "*.aux",
        "*.bbl",
        "*.blg",
        "*.idx",
        "*.ind",
        "*.lof",
        "*.lot",
        "*.out",
        "*.toc",
        "*.acn",
        "*.acr",
        "*.alg",
        "*.glg",
        "*.glo",
        "*.gls",
        "*.ist",
        "*.fls",
        "*.log",
        "*.fdb_latexmk",
        "*.snm",
        "*.nav",
        "*.dvi",
        "*.synctex.gz"
    ],

    "latex-workshop.latex.outDir": "out",

    "latex-workshop.latex.recipes": [
        {
            "name": "latexmk",
            "tools": [
                "latexmk"
            ]
        }
    ],

    "latex-workshop.latex.tools": [
        {
            "name": "latexmk",
            "command": "latexmk",
            "args": [
                "-silent",
                "-outdir=%OUTDIR%",
                "%DOC%"
            ]
        }
    ]
}

VS Codeをダークテーマで利用している場合、PDFプレビューが暗色反転して読みづらくなることがある。
必要に応じて以下の設定を追加すると、PDFプレビューの背景を白に固定でき、可読性が向上する。

"latex-workshop.view.pdf.color.dark.backgroundColor": "",
"latex-workshop.view.pdf.color.dark.pageColorsBackground": "#ffffff"

3. VS Codeを再起動

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?