redmine の wiki に数式を入れる

redminewiki で数式を扱えるようにする。

redmine 用の LaTeX プラグインは複数あるかもしれないけれど、とりあえず、ここで配布されているもの を入れてみた。

これは、あらかじめシステムにインストール済みの LaTeX 処理系を前提としている。
ちなみに、現在使っているPCには texlive がインストールしてある。

なお、プラグイン一般のインストール手順は、ここ を参照 すればよい。

ダウンロードした LaTeX プラグインのフォルダ名は、nisrael-redmine-wiki_latex_plugin-28000b9 と長たらしいが、これを wiki_latex_plugin に修正しないと動かない。

また、/wiki_latex_plugin/app/controllers/wiki_latex_controller.rb に、wiki 中の LaTeX の数式コードを画像に変換するプログラムが記述されているが、オリジナルのままでは動作しなかったので、若干チューニングを施した。以下は、修正後のプログラムである。

class WikiLatexController < ApplicationController

  def image
    @latex = WikiLatex.find_by_image_id(params[:image_id])
    @name = params[:image_id]
    if @name != "error"
        image_file = File.join([RAILS_ROOT, 'tmp', 'wiki_latex_plugin', @name+".png"])
    else
        image_file = File.join([RAILS_ROOT, 'public', 'plugin_assets', 'wiki_latex_plugin', 'images', @name+".png"])
    end
    if (!File.exists?(image_file))
        render_image
    end
    if (File.exists?(image_file))
      render :file => image_file, :layout => false, :content_type => 'image/png'
    else
        render_404
    end
    rescue ActiveRecord::RecordNotFound
      render_404
  end

private
  def render_image
    dir = File.join([RAILS_ROOT, 'tmp', 'wiki_latex_plugin'])
    begin
      Dir.mkdir(dir)
    rescue
    end
    basefilename = File.join([dir,@name])
    temp_latex = File.open(basefilename+".tex",'w')
    temp_latex.puts('\documentclass[24pt]{article}')
    temp_latex.puts('% add additional packages here')
    temp_latex.puts('\pagestyle{empty}')
    temp_latex.puts('\usepackage{amsmath}')
    temp_latex.puts('\usepackage{amsfonts}')
    temp_latex.puts('\usepackage{amssymb}')
    temp_latex.puts('\usepackage{color}')
    temp_latex.puts('%\usepackage[active,displaymath,textmath,graphics]{preview}')
    temp_latex.puts('\begin{document}')
    temp_latex.puts @latex.source
    temp_latex.puts '\end{document}'
    temp_latex.flush
    temp_latex.close

    fork_exec(dir, "/usr/bin/latex --interaction=nonstopmode "+@name+".tex 2> /dev/null > /dev/null")
    fork_exec(dir, "/usr/bin/dvipng -q  -D 120 -T tight -M -pp 1 "+@name+".dvi -o "+@name+".png")
    ['tex','dvi','log','aux','ps'].each do |ext|
        if File.exists?(basefilename+"."+ext)
            File.unlink(basefilename+"."+ext)
        end
    end
  end

  def fork_exec(dir, cmd)
    pid = fork{
      Dir.chdir(dir)
      exec(cmd)
      exit! ec
    }
    ec = nil
    begin
      Process.waitpid pid
      ec = $?.exitstatus
    rescue
    end
  end

end

さらに、数式コードの中に中括弧{}が含まれていると、このプログラムが正常動作しないが、LaTeX では中括弧を多用するから、このままでは使いものにならない。
ただし、これはプラグインではなく redmine 側の文字列解析の問題だ。
すなわち、redmine の中の wiki フォーマットを制御しているプログラムを修正する必要がある。
具体的には、/var/lib/redmine/lib/redmine/wiki_formatting/textile/formatter.rb 内の "MACROS_RE = /" で始まるブロックを、次のように書き換える。

(!)?                        # escaping
(
 \{\{                       # opening tag
([\w]+)                     # macro name
(\(((.(?!\}\}))*)\))?       # optional arguments
\}\}                        # closing tag
)
/mx unless const_defined?(:MACROS_RE)

で、wiki の中に、たとえば、

{{latex($\hat{\sigma}^2=\displaystyle \frac {1}{N-p} (\mathbf{y}-\mathbf{Xb})^T (\mathbf{y}-\mathbf{Xb})$)}}

なんて書くと、、、

まぁ、いちおう数式が美しく表示されるのだが、まだまだ細かいところの調整が必要かも・・