Class Generators::TexParser
In: generators/xhtml_generator.rb
Parent: HyperlinkHtml

Note that Japanese and English are described in parallel.

TeX の数式を MathML に変換

TeX で記述された数式を MathML に変換します. インラインで表示したい場合, TeX の数式を以下のように $ … $ でくくって 記述してください. $ の 前後には半角空白を一文字以上入れて下さい. (なお, "$ID: … $" や "$LOG: … $ といった, CVS のキーワードとして 用いられている書き方は数式として扱われません.)

  インラインで表示する数式は $ f(x) = x^2 + 1 $ のように記述します.
  ($ の前後に空白をお忘れなく).

ブロックで表示する場合, 以下のように \[ と \] とでくくって記述してください. \[, は必ず行頭に記述してください.

  \[
     \sum_{i=1}^nf_n(x)
  \]

数式を複数行で表示する場合には, 改行する部分に "\] \[" を記述してください.

 \[
     d\zeta/dt + J(\psi,\zeta) = Ra \; dT/dx + \nabla\zeta, \] \[
     dT/dt + J(\psi,T) - d\psi/dx = \nabla T,               \] \[
     \nabla\psi = \zeta
 \]

TeX の数式から MathML 変換には Ruby 用 MathML ライブラリのバージョン 0.6b を使用しています. このライブラリはひらくの工房 にて公開されています. 使用できる TeX コマンドの詳細に関しても こちらのサイトを参照してください.

作成されたドキュメントを閲覧する際には MathML に対応した ブラウザを使用する必要が あります. MathML 日本語情報MathML Software - Browsers などを参照してください.

TeX is converted to MathML

TeX formula is converted to MathML. When inline display, TeX formula should be bundled by $ … $ as follows. One or more normal-width blank is necessary before and behind "$". (The format of CVS keywords, that is "$ID: … $" or "$LOG: … $ etc. is ignored.)

  Inline formula is $ f(x) = x^2 + 1 $ .

When block display, TeX formula should be bundled by \[ … \] as follows. Describe \[ at the head of line.

  \[
     \sum_{i=1}^nf_n(x)
  \]

To write equations across multiple lines, describe "\] \[" as follows.

 \[
     d\zeta/dt + J(\psi,\zeta) = Ra \; dT/dx + \nabla\zeta, \] \[
     dT/dt + J(\psi,T) - d\psi/dx = \nabla T,               \] \[
     \nabla\psi = \zeta
 \]

MathML library for Ruby version 0.6b is needed to convert TeX formula to MathML. This library is available from Bottega of Hiraku (JAPANESE only). See this site about available TeX commands.

When you browse generated documents, you need to use browers that can handle MathML. See MathML Software - Browsers, etc.

\newcommand, \newenvironment の使用方法

\newcommand や \newenvironment 命令によるマクロを使用するには, Ruby のロードパス以下に ‘mathml/macro’ ディレクトリを 作成し, そのディレクトリ以下にマクロ命令を記したファイルを置いてください. ファイル名は問いません.

例えば, ‘/usr/lib/ruby/1.8’ が Ruby のロードパスである場合, ‘/usr/lib/ruby/1.8/mathml’ および ‘/usr/lib/ruby/1.8/mathml/macro’ ディレクトリを作成し, そのディレクトリ以下に, ‘D6math.sty’ というファイルを作成します (前述したようにこのファイル名は何でも構いません). そのファイル内に 以下のような \newcommand 命令を記述しておくことで, ‘\DP{}{}’ (偏微分を簡単に記述するためのマクロ) コマンドが使用可能になります.

    \newcommand{\DP}[2]{\frac{\partial #1}{\partial #2}}

地球流体電脳倶楽部で提供している TeX マクロ (通称: 電脳スタイル) に含まれる ‘D6math.sty’ を Ruby 用 Mathml ライブラリで使用できるように 修正したパッケージを libmathml-macro-dennou-ruby として 提供しています. サンプルとして利用してみてください.

Usage of \newcommand and \newenvironment

If you want to use macros defined by \newcommand and \newenvironment commands, make ‘mathml/macro’ directory under load paths of Ruby, and prepare a file that macro commands are described in the directory. A name of the file is free.

For example, if your load path of Ruby is ‘/usr/lib/ruby/1.8’, you should make ‘/usr/lib/ruby/1.8/mathml’ and ‘/usr/lib/ruby/1.8/mathml/macro’ directories, and make ‘D6math.sty’ file (already mentioned, the file name is free). When you describe a following \newcommand command, you can use ‘\DP{}{}’ (a macro for partial differentiations) command.

    \newcommand{\DP}[2]{\frac{\partial #1}{\partial #2}}

As a sample, please use libmathml-macro-dennou-ruby. The original style file is ‘D6math.sty’ in TeX macro (Dennou style). libmathml-macro-dennou-ruby is a reconfigured package for Mathml library for Ruby.

Methods

Public Class methods

[Source]

     # File generators/xhtml_generator.rb, line 141
141:     def initialize(*args)
142:       super(*args)
143:     end

Public Instance methods

[Source]

     # File generators/xhtml_generator.rb, line 145
145:     def file_location
146:       if @context.context.parent
147:         class_or_method = @context.context.name
148:       end
149:       context = @context.context
150:       while context.parent
151:         context = context.parent
152:       end
153:       location = context.file_relative_name
154:       if class_or_method
155:         location += "#"+class_or_method
156:       end
157:       return location
158:     end

TEXBLOCK pattern \[…\] is converted to MathML format when —mathml option is given.

[Source]

     # File generators/xhtml_generator.rb, line 191
191:     def handle_special_TEXBLOCK(special)
192:       text = special.text
193:       return text unless Options.instance.mathml
194:       text.sub!(/^\\\[/, '')
195:       text.sub!(/\\\]$/, '')
196:       tex = MathMLWrapper.new
197:       mathml, stat = tex.parse(text, true)
198:       if !stat.zero?
199:         $stderr.puts "Warning: in #{file_location}, following TeX commands can not be converted to MathML\n\n",
200:         "    #{text}\n\n"
201:       end
202:       return mathml
203:     end

TEXINLINE pattern $…$ is converted to MathML format when —mathml option is given.

[Source]

     # File generators/xhtml_generator.rb, line 163
163:     def handle_special_TEXINLINE(special)
164:       text = special.text
165:       return text unless Options.instance.mathml
166:       raw_text = text.scan(/^.*?\$/).to_s.sub(/\$$/, '')
167:       return text if text =~ /^.*?\$[A-Z]\w+:/  # CVS keywords are skipped
168:       text.sub!(/^.*?\$/, '')
169:       text.sub!(/\$$/, '')
170:       tex = MathMLWrapper.new
171:       mathml, stat = tex.parse(text)
172:       if !stat.zero?
173:         $stderr.puts "Warning: in #{file_location}, following TeX commands can not be converted to MathML\n\n",
174:         "    #{text}\n\n"
175:       end
176:       return raw_text + mathml
177:     end

TEXINLINEDELIMITER pattern "\$" is converted to single dollar "$" when —mathml option is given.

[Source]

     # File generators/xhtml_generator.rb, line 182
182:     def handle_special_TEXINLINEDELIMITER(special)
183:       text = special.text
184:       return text unless Options.instance.mathml
185:       return text.gsub(/\\\$/, '$')
186:     end

[Validate]