Class | Generators::TexParser |
In: |
generators/xhtml_generator.rb
|
Parent: | HyperlinkHtml |
Note that Japanese and English are described in parallel.
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 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 命令によるマクロを使用するには, 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 として 提供しています. サンプルとして利用してみてください.
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.
# File generators/xhtml_generator.rb, line 141 141: def initialize(*args) 142: super(*args) 143: end
# 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: file_name = context.file_relative_name 154: 155: if class_or_method 156: location += "#"+class_or_method 157: else 158: location = file_name 159: end 160: end
TEXBLOCK pattern \[…\] is converted to MathML format when —mathml option is given.
# File generators/xhtml_generator.rb, line 193 193: def handle_special_TEXBLOCK(special) 194: text = special.text 195: return text unless Options.instance.mathml 196: text.sub!(/^\\\[/, '') 197: text.sub!(/\\\]$/, '') 198: tex = MathMLWrapper.new 199: mathml, stat = tex.parse(text, true) 200: if !stat.zero? 201: $stderr.puts "Warning: in #{file_location}, following TeX commands can not be converted to MathML\n\n", 202: " #{text}\n\n" 203: end 204: return mathml 205: end
TEXINLINE pattern $…$ is converted to MathML format when —mathml option is given.
# File generators/xhtml_generator.rb, line 165 165: def handle_special_TEXINLINE(special) 166: text = special.text 167: return text unless Options.instance.mathml 168: raw_text = text.scan(/^.*?\$/).to_s.sub(/\$$/, '') 169: return text if text =~ /^.*?\$[A-Z]\w+:/ # CVS keywords are skipped 170: text.sub!(/^.*?\$/, '') 171: text.sub!(/\$$/, '') 172: tex = MathMLWrapper.new 173: mathml, stat = tex.parse(text) 174: if !stat.zero? 175: $stderr.puts "Warning: in #{file_location}, following TeX commands can not be converted to MathML\n\n", 176: " #{text}\n\n" 177: end 178: return raw_text + mathml 179: end
TEXINLINEDELIMITER pattern "\$" is converted to single dollar "$" when —mathml option is given.
# File generators/xhtml_generator.rb, line 184 184: def handle_special_TEXINLINEDELIMITER(special) 185: text = special.text 186: return text unless Options.instance.mathml 187: return text.gsub(/\\\$/, '$') 188: end