以下の記事にて、表題に関する設定が公開されていた。「自動コンパイル」のところである。
銭谷誠司氏のサイトにて公開されているsmart-compile.elを用いている。上記の記事ではリンク切れであったが、現在はgithubにて継続的に開発が続いている。
smart-compile.el自体のコンセプトは明解で, さまざまなソースファイルと対応するコンパイルコマンドをひもづけるためのインターフェイスを提供する。これをLaTeXソースのコンパイルに応用したのが上記の設定である。
現在はオリジナルから多少変更を加えて以下の設定を利用している(AUCTeX利用)。latexmkをバックグラウンドで走らせている。
(eval-when-compile (require 'smart-compile))
(declare-function smart-compile-string "smart-compile")
(defun run-latexmk ()
(when (string-match ".tex$" (buffer-file-name))
(let ((buf (get-buffer "*Background TeX proccess*")))
(if (bufferp buf) (kill-buffer buf)) )
(require 'smart-compile)
(start-process-shell-command
"Background TeX" "*Background TeX proccess*"
(smart-compile-string "latexmk %f"))))
(define-minor-mode AutoTeX-mode
"Mode for compiling latex sources and creating PDFs after saving."
:global nil
:lighter " Auto"
(if AutoTeX-mode
(add-hook 'after-save-hook 'run-latexmk t t)
(remove-hook 'after-save-hook 'run-latexmk t)))
(add-hook 'TeX-mode-hook #'(lambda () (AutoTeX-mode 1)))
after-save-hook
に引っ掛けることで、保存と同時にlatexmkを走らせてLaTeXソースをコンパイルしPDFを生成する。smart-compile-string
により実行される、latexmkへのオプション引数にはカスタマイズの余地はある。
ちなみにlatexmkの設定ファイル(.latexmkrc)の設定例は以下の通りである。
#!/usr/bin/env perl
$pdf_mode = 3;
$latex = "uplatex -synctex=1 -halt-on-error -interaction=nonstopmode %O %S";
$dvipdf = 'dvipdfmx %O -o %D %S';
$bibtex = 'upbibtex %O %S';
$biber = 'biber --bblencoding=utf8 -u -U --output_safechars %O %S';
$makeindex = 'mendex %O -o %D %S';
$max_repeat = 5;
個人的にpdf-toolsを用いているため、外部アプリによるプレビュー関連の設定はしていない。もしプレビューワの設定をするならば、$pdf_previewer
という変数に、プレビューワ起動に必要なコマンドラインを設定する。例えば
$pdf_previewer = 'open -a /Applications/Preview.app';
や
$pdf_previewer = 'evince %O %S';
などである。そのうえで上記のrun-latexmk
関数において以下の修正を施し、latexmkに-pv
オプションをつけることで、プレビューワを起動する。
(smart-compile-string "latexmk -pv %f")