Mac上でのPDFやePUB生成に使うMacTexの日本語出力対応込セットアップをTerminal上で完結させてみた

TeX Live をベースにした macOS 専用の TeX ディストリビューションのMacTexで、日本語出力に対応させる為の手順をTerminalですべて完結してみました。想定外のエラーが発生するようになったらしく、状況によっては数時間単位でかかります。
2020.08.08

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

PCからPDFファイルの作成を行う場合、ブラウザ上でファイルを開いてプリントにてPDF出力を選んでいます。レイアウト調整もやりやすくて大体はそれで完結するためです。

ただ、全く問題がないわけでもありません。印刷時のページ数が多くなるケースではブラウザが応答不能になる可能性もあります。

対策として、電子書籍のPDF等を作成する際に用いられるMacTexを試してみることにしました。が、インストールをTerminalで完結させようとしたところ、思いの外手間取ったため、完了するまでの過程を記録として残してみました。

MacTexの導入

HomeBrewでさくっと終わらせられます。

brew cask install mactex

問題は初期設定状態だと日本語文書が作成できない点です。パッケージを追加インストールする必要があります。

サブセットのインストール

インストールのためのコマンドはシンプルです。

sudo tlmgr update --self --all

もしも以下のようなエラーが出た場合は、初期登録のアップデート用リポジトリが機能していません。

/Library/TeX/texbin/tlmgr: TLPDB::from_file could not download https://ftp.kddilabs.jp/CTAN/systems/texlive/tlnet/tlpkg/texlive.tlpdb;
/Library/TeX/texbin/tlmgr: maybe the repository setting should be changed.

ミラーリングリポジトリを登録します。

sudo tlmgr option repository http://mirror.ctan.org/systems/texlive/tlnet

恐らくこれで上手くいくはずです。が、以下のようなエラーが出てくる場合、更に対策を取る必要があります。

/Library/TeX/texbin/tlmgr: unexpected return value from verify_checksum: -5

最新版のアップデート用バッチスクリプトを実行する

対策としては以下のようになります。

Please update with update-tlmgr-latest.sh, after that normal operation can be done.

ミラーリングリポジトリ上からupdate-tlmgr-latest.shを直接落として実行します。

wget http://mirror.ctan.org/systems/texlive/tlnet/update-tlmgr-latest.sh
sudo kpsewhich --var-value=SELFAUTOPARENT
sudo sh ./update-tlmgr-latest.sh
Verifying archive integrity... All good.
Uncompressing TeX Live Manager Updater  100%
./runme.sh: updating in /usr/local/texlive/2019...
./runme.sh: tlmgr version says this is TeX Live 2019,
./runme.sh: and this updater script created: Thu Jul  9 02:37:20 CEST 2020.
./runme.sh: proceeding with tlmgr update.
./runme.sh: updating /usr/local/texlive/2019/bin/x86_64-darwin ...
./runme.sh: /Library/TeX/texbin/tlmgr including objects: master/tlpkg/tlpobj/texlive.infra.tlpobj master/tlpkg/tlpobj/texlive.infra.x86_64-darwin.tlpobj
D:appending to package log file: /usr/local/texlive/2019/texmf-var/web2c/tlmgr.log
D:appending to command log file: /usr/local/texlive/2019/texmf-var/web2c/tlmgr-commands.log
D:tlmgr:main: ::tldownload_server not defined
D:setup_programs: preferring system versions
D:program curl found in path
D:program wget found in path
D:program lz4 not usable from path
D:(unix) trying to set up lz4, default /usr/local/texlive/2019/tlpkg/installer/lz4/lz4.x86_64-darwin, arg --version
D: Using shipped /usr/local/texlive/2019/tlpkg/installer/lz4/lz4.x86_64-darwin for lz4 (tested).
D:program gzip found in path
D:program xz found in path
./runme.sh: done.

サブセットのインストールを再度試してみます。

sudo tlmgr update --self -all
tlmgr: package repository http://ftp.jaist.ac.jp/pub/CTAN/systems/texlive/tlnet (verified)
tlmgr: saving backups to /usr/local/texlive/2019/tlpkg/backups
[   1/1412] auto-remove: guide-latex-fr ... done
[   2/1412] auto-remove: presentations ... done
[   3/1412] auto-remove: einfuehrung ... done
[   4/1412] auto-remove: einfuehrung2 ... done
..
[  42/1412] auto-remove: tetex ... done
[  43/1412] auto-remove: bezos ... done
[  44/1412, ??:??/??:??] update: 2up [56k] (41578 -> 55076) ... done
[  45/1412, 00:02/17:04:22] update: IEEEtran [564k] (38238 -> 51065) ... done
[  46/1412, 00:04/03:03:17] update: SIstyle [338k] (15878 -> 54080) ... done
[  47/1412, 00:06/02:57:56] update: a0poster [119k] (15878 -> 54071) ... done
[  48/1412, 00:08/03:31:03] update: a2ping [69k] (49161 -> 52964) ... done
[  49/1412, 00:09/03:43:09] update: aastex [1267k] (47692 -> 51836) ... done
[  50/1412, 00:11/02:09:22] update: abnt [121k] (49188 -> 55471) ... done
[  51/1412, 00:13/02:25:37] update: academicons [126k] (48100 -> 54512) ... done
[  52/1412, 00:14/02:29:26] update: achemso [895k] (50025 -> 55899) ... done
[  53/1412, 00:19/02:31:42] update: acmart [3498k] (50968 -> 55004) ... done
[  54/1412, 00:26/01:44:33] update: acro [1226k] (50045 -> 55090) ... done
...
running mktexlsr ...
done running mktexlsr.
running mtxrun --generate ...
done running mtxrun --generate.
running updmap-sys ...
done running updmap-sys.
regenerating language.dat
regenerating language.def
regenerating language.dat.lua
regenerating fmtutil.cnf in /usr/local/texlive/2019/texmf-dist
running fmtutil-sys --no-error-if-no-engine=luajithbtex,luajittex,mfluajit --no-error-if-no-format --byengine aleph ...
done running fmtutil-sys --no-error-if-no-engine=luajithbtex,luajittex,mfluajit --no-error-if-no-format --byengine aleph.
running fmtutil-sys --no-error-if-no-engine=luajithbtex,luajittex,mfluajit --no-error-if-no-format --byengine luajithbtex ...
done running fmtutil-sys --no-error-if-no-engine=luajithbtex,luajittex,mfluajit --no-error-if-no-format --byengine luajithbtex.
running fmtutil-sys --no-error-if-no-engine=luajithbtex,luajittex,mfluajit --no-error-if-no-format --byengine ptex ...
...
tlmgr: package log updated: /usr/local/texlive/2019/texmf-var/web2c/tlmgr.log

ログに出ている時間を見るとわかるように、完了までにはそれなりの時間がかかります。

実行するコマンドまとめ

以下の順にコマンドを実行しましょう。

brew cask install mactex
sudo tlmgr option repository http://mirror.ctan.org/systems/texlive/tlnet
wget http://mirror.ctan.org/systems/texlive/tlnet/update-tlmgr-latest.sh
sudo kpsewhich --var-value=SELFAUTOPARENT
sudo sh ./update-tlmgr-latest.sh
sudo tlmgr update --self -all

実際にPDFが生成されることを確認する

実際に日本語を文書中に使っている、TechBooster公開のTechBooster/ReVIEW-Templateにて確認してみます。

gem install bundler
git clone git@github.com:TechBooster/ReVIEW-Template.git
cd ReVIEW-Template
npm install
npm run pdf
open articles/ReVIEW-Template.pdf

ReVIEW-template/articles下にReVIEW-Template.pdfが生成されます。尚、gitignoreに該当pdfファイルが含まれているため、git diffしても差分として出てくることはありません。

あとがき

GUIインストーラによる説明は多く存在しましたが、Terminalでのインストール手順になかなかたどり着けない状態でした。コマンド貼り付けだけで済ませたい場合にはおすすめです。

参考リンク