LoginSignup
14
12

More than 5 years have passed since last update.

TeXを監視して自動コンパイルする

Last updated at Posted at 2015-01-05

rubyのguard/guardを使ってファイルの変更を監視して、
裏で自動コンパイルする

準備

まずguardをインストールする

以下の様なGemfileを用意する

source 'http://rubygems.org'
gem 'guard'
gem 'guard-shell'
gem 'rb-readline'
gem 'rb-inotify'

そして

# gem install bundle
bundle install

guardなどをインストールする

ファイルの監視およびコンパイルの設定

まずguardの初期設定をする

bundle exec guard init

するとGuardfileが作成される
これを以下の様に書き換える

Guardfile
guard :shell do
  watch(/(.*).tex/) {|m|
    `platex -interaction=nonstopmode  #{m[1]}`
    `dvipdfmx #{m[1]}`
    `open -g -a Preview #{m[1]}.pdf`
  }
end

これで、texファイルに変更があればplatexdvipdfmxでpdfファイルに変換し、バックグラウンドのPreview.appで開くようになる
なお、Preview.appにフォーカスをあてないと変更が可視化されないので注意
やや無理矢理な解決策としては

Guardfile
guard :shell do
  watch(/(.*).tex/) {|m|
    `platex -interaction=nonstopmode #{m[1]}`
    `dvipdfmx #{m[1]}`
    `open -a Preview #{m[1]}.pdf`
    `open -a TexShop`
  }
end

とすることで一度Preview.appにフォーカスをあててから再度TexShop.appにフォーカスをあてることが出来るため、
Preview.appも更新されつつフォーカスは(一瞬奪われるが)TexShopに保つことが出来る

追記(2015/01/05 17:25)

SkimというPDFビューワーには
ファイルの変更を検知して表示を更新してくれる機能があるので、これを使えば

Guardfile
guard :shell do
  watch(/(.*).tex/) {|m|
    `platex -interaction=nonstopmode #{m[1]}`
    `dvipdfmx #{m[1]}`
    `open -g -a Skim #{m[1]}.pdf`
  }
end

とすればフォーカスを奪われることなく表示の更新ができて便利でした

14
12
2

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
14
12