VimでTeXを書こう! on Mac
概要
論文執筆にあたり、vimでTeXを書く環境を整えたいと思った。
これまではMakefileを書いておいて、PDFを確認したくなったらmakeを実行していた。
これでも悪くはないのだが、やっぱりリアルタイムプレビューとかが欲しい。
というわけでやってみた。
環境
- Mac OS 10.14.6
- Skim 1.5.2
- Vim 8.1
- XQuartz 2.7.11
- e-pTeX 3.14159265-p3.8.2-190131-2.6 (utf8.euc) (TeX Live 2019)
- latexmk 4.63b
手順
1. MacTeXのインストール
MacTeXはTeXに必要な諸々のソフトウェアをまとめてインストールしてくれるもの。
以下からダウンロードしてインストール。
2. latexmkの準備
latexmkはTeXからPDFまでの変換に必要な一連の処理を行ってくれるperlスクリプト。
MacTeXをインストールすると付いてくるので、別でインストールする必要はない。
vimのTeX用pluginの中でこれを使用してコンパイルを行う。
設定ファイルとして、カレントディレクトリ or ホームディレクトリに.latexmkrcという設定ファイルを作成する必要がある。
設定例を以下に示す。
#!/usr/bin/env perl $latex = 'platex -synctex=1 -halt-on-error'; $latex_silent = 'platex -synctex=1 -halt-on-error -interaction=batchmode'; $biber = 'biber -u -U --output_safechars'; $bibtex = 'pbibtex'; $dvipdf = 'dvipdfmx %O -o %D %S'; $makeindex = 'mendex %O -o %D %S'; $pdf_mode = 3; # Use dvipdf $pvc_view_file_via_temporary = 0; # Do not create backup file $max_repeat = 5; $preview_continuous_mode = 1; if ($^O eq 'darwin') { $pdf_previewer = 'open -ga Skim'; } elsif ($^O eq 'linux') { $pdf_previewer = 'evince'; }
設定内容の説明は省略するので、公式等を参照して欲しい。(URL: https://ctan.org/pkg/latexmk)
3. Vimのインストール
PDF Viewerと連携するためにclientserverという拡張をenableにする必要があるので、homebrew等のパッケージ管理ソフトではなくソースからコンパイルする。
clientserver機能付きでビルドできるようにシェルスクリプトを用意した。
以下のshellscriptを実行すると、/usr/local/binにインストールされる。
#!/bin/sh vim_repo_dir=vim if [ ! -e $vim_repo_dir ]; then git clone https://github.com/vim/vim.git fi cd $vim_repo_dir git pull make distclean ./configure \ --with-features=huge \ --disable-darwin \ --enable-python3interp \ --enable-luainterp \ --enable-gui \ make sudo make install
vim --version
を実行すると、有効になっている拡張機能が確認できる。
clientserverに+マークが付いていれば大丈夫。
VIM - Vi IMproved 8.1 (2018 May 18, compiled Aug 7 2019 13:37:24) macOS version w/o darwin feat. Included patches: 1-1824 Compiled by kkoiro@john Huge version with X11-Athena GUI. Features included (+) or not (-): +acl -farsi -mouse_sysmouse -tag_any_white +arabic +file_in_path +mouse_urxvt -tcl +autocmd +find_in_path +mouse_xterm +termguicolors +autochdir +float +multi_byte +terminal -autoservername +folding +multi_lang +terminfo +balloon_eval -footer -mzscheme +termresponse +balloon_eval_term +fork() +netbeans_intg +textobjects +browse -gettext +num64 +textprop ++builtin_terms -hangul_input +packages +timers +byte_offset +iconv +path_extra +title +channel +insert_expand -perl +toolbar +cindent +job +persistent_undo +user_commands +clientserver +jumplist +postscript +vartabs +clipboard +keymap +printer +vertsplit +cmdline_compl +lambda +profile +virtualedit +cmdline_hist +langmap -python +visual +cmdline_info +libcall +python3 +visualextra +comments +linebreak +quickfix +viminfo +conceal +lispindent +reltime +vreplace +cryptv +listcmds +rightleft +wildignore -cscope +localmap -ruby +wildmenu +cursorbind -lua +scrollbind +windows +cursorshape +menu +signs +writebackup +dialog_con_gui +mksession +smartindent +X11 +diff +modify_fname -sound +xfontset +digraphs +mouse +spell +xim -dnd +mouseshape +startuptime +xpm -ebcdic +mouse_dec +statusline +xsmp_interact +emacs_tags -mouse_gpm -sun_workshop +xterm_clipboard +eval -mouse_jsbterm +syntax -xterm_save +ex_extra +mouse_netterm +tag_binary +extra_search +mouse_sgr -tag_old_static system vimrc file: "$VIM/vimrc" user vimrc file: "$HOME/.vimrc" 2nd user vimrc file: "~/.vim/vimrc" user exrc file: "$HOME/.exrc" system gvimrc file: "$VIM/gvimrc" user gvimrc file: "$HOME/.gvimrc" 2nd user gvimrc file: "~/.vim/gvimrc" defaults file: "$VIMRUNTIME/defaults.vim" system menu file: "$VIMRUNTIME/menu.vim" fall-back for $VIM: "/usr/local/share/vim" Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_ATHENA -DMACOS_X -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -I/usr/X11/include Linking: gcc -L/usr/X11/lib -L/usr/local/lib -o vim -lXaw -lXmu -lXext -lXpm -lXt -lX11 -lSM -lICE -lm -lncurses -liconv -L/usr/local/var/pyenv/versions/3.7.4/lib/python3.7/config-3.7m-darwin -lpython3.7m -framework CoreFoundation
4. XQuartzのインストール
vimのclientserver機能を使用するにはX Window Systemが必要らしい。
Mac OSではX Quartzというソフトウェアをインストールすることで使用できる。
5. vimtexの導入
vimtexはvimでTeXを書きやすくしてくれる素晴らしいプラグイン。
以下のレポジトリからダウンロードして追加。
github.com
自分はdein.vim(URL)https://github.com/Shougo/dein.vimを利用しているので、dein_lazy.tomlに以下を追記した。
[[plugins]] repo = 'lervag/vimtex' on_ft = 'tex' hook_add = ''' let g:vimtex_compiler_latexmk_engines = {'_': '-pdfdvi'} let g:vimtex_view_method= 'skim' let g:vimtex_quickfix_latexlog = {'default': 0} ''' hook_source = ''' if empty(v:servername) && exists('*remote_startserver') call remote_startserver('VIM') endif '''
以下、解説。
- vimtex_compiler_latexmk_engines
latexmkでPDFをコンパイルする。-pdfdviはオプション。 - vimtex_view_method
PDF Viewerを指定。skim(後述)を指定している。 - vimtex_quickfix_latexlog
latexのwarningに関する設定。'default': 0を指定すると、latexのwarningを全て無視する。 - call remote_startserver
vimtexを読み込むときにVIMという名前でコマンドサーバを起動する。
詳しい設定は、vimで:help vimtex
を参照すること。
6. Skimのインストール
MacのPDF Viewerで有名なものらしい。(知らなかった)
SyncTeX機能を搭載しており、vimサーバとやりとりをして相互に連携可能なためこれを使う。
インストールは以下のリンクから。
インストールしたら、Preferenceを開き、SyncタブのPDF-TeX Sync support項目を以下のように設定。
- Preset: Custom
- Command: /usr/local/bin/vim
- Arguments: --remote-silent +"%line" "%file"
Argumentの詳細についてはvimの公式に説明があるので参照すること。(URL: https://vim-jp.org/vimdoc-ja/remote.html)
ここまでで必要な環境構築は終了。
使い方
vimtexでは\ll
でコンパイルモード開始、その後ファイルを編集し保存すると自動でコンパイルしてPDF Viewerに反映してくれる。
\lv
を押すと、PDF Viewerでカーソル行に移動しハイライト、PDF ViewerからはCommand+Shift+クリック
でvimのカーソルを移動してくれる。
感想
vimとskimをマルチウインドウで並べると、作業も捗るしとてもかっこいい!