履歴: ソフトウェア/pukiwiki/数式プラグイン

各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 24 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます

日時タイトル文字数追加削除内容差分基準
2018/7/2 18:40:57ソフトウェア/pukiwiki/数式プラグイン 最新版32,412+1 -1表示差分—
2018/7/2 18:40:57ソフトウェア/pukiwiki/数式プラグイン 32,418+1 -2表示差分選択
2018/7/2 9:40:57ソフトウェア/pukiwiki/数式プラグイン 32,350+2 -1表示差分選択
2018/7/2 9:40:57ソフトウェア/pukiwiki/数式プラグイン 32,343+6 -0表示差分選択
2015/4/22 6:16:43ソフトウェア/pukiwiki/数式プラグイン 32,243+8 -0表示表示中選択
2015/4/22 6:16:43ソフトウェア/pukiwiki/数式プラグイン 32,016+1 -1表示差分選択
2015/4/22 6:16:43ソフトウェア/pukiwiki/数式プラグイン 32,015+7 -0表示差分選択
2014/6/13 11:14:29ソフトウェア/pukiwiki/数式プラグイン 31,871+14 -3表示差分選択
2014/5/2 7:19:58ソフトウェア/pukiwiki/数式プラグイン 31,715+5 -5表示差分選択
2014/5/2 7:19:58ソフトウェア/pukiwiki/数式プラグイン 31,674+9 -0表示差分選択
2014/4/14 9:38:39ソフトウェア/pukiwiki/数式プラグイン 31,511+442 -6表示差分選択
2014/4/12 12:14:30ソフトウェア/pukiwiki/数式プラグイン 19,364+17 -1表示差分基準
2014/4/11 14:35:22ソフトウェア/pukiwiki/数式プラグイン 19,069+181 -0表示差分選択
2013/4/11 0:02:52ソフトウェア/pukiwiki/数式プラグイン 12,851+0 -8表示差分選択
2013/4/11 0:02:52ソフトウェア/pukiwiki/数式プラグイン 12,983+8 -0表示差分選択
2013/4/8 3:28:03ソフトウェア/pukiwiki/数式プラグイン 12,851+99 -1表示差分選択
2013/4/4 11:23:29ソフトウェア/pukiwiki/数式プラグイン 10,537+4 -2表示差分選択
2013/4/4 11:23:29ソフトウェア/pukiwiki/数式プラグイン 10,449+21 -0表示差分選択
2013/3/10 2:41:01ソフトウェア/pukiwiki/数式プラグイン 10,041+10 -0表示差分選択
2011/10/22 15:45:46ソフトウェア/pukiwiki/数式プラグイン 9,833+0 -8表示差分選択
2011/10/21 6:52:51ソフトウェア/pukiwiki/数式プラグイン 9,966+8 -0表示差分選択
2011/5/19 11:09:24ソフトウェア/pukiwiki/数式プラグイン 9,833+8 -0表示差分選択
2011/5/4 3:50:36ソフトウェア/pukiwiki/数式プラグイン 9,676+78 -28表示差分選択
2011/5/4 3:02:07ソフトウェア/pukiwiki/数式プラグイン 8,128— —表示—選択

基準: ソフトウェア/pukiwiki/数式プラグイン (2014/4/12 12:14:30) → 比較: ソフトウェア/pukiwiki/数式プラグイン (2015/4/22 6:16:43)

@@ -442,11 +442,11 @@
* いろいろバージョンが上がって動かなくなりました [#mfe0920a]
-上記のコードがうまく動かなくなったため、より良い方法を検討しました。
+上記の latex を使って svg を作成するコードがうまく動かなくなったため、より良い方法を検討しました。
-** TeX コードから svg への変換 [#n2bb7388]
+** TeX ソースから svg への変換 [#n2bb7388]
- LANG:sh
+ LANG:console
$ platex
This is e-pTeX, Version 3.1415926-p3.3-110825-2.4 (utf8.euc) (TeX Live 2012/Debian)
$ platex -interaction=nonstopmode math.tex
@@ -626,17 +626,488 @@
1つの画面に複数の数式を貼る場合、
同じ名前で異なる Glyph が大量に定義されてしまい、
-困ったことになる。
+困ったことになります。
-そこで、自動生成された svg の Glyph 名に、数式をハッシュ化した文字列を追加することで
-Glyph 名の重複を避けることにした。
+そこで、自動生成された svg の Glyph 名を数式をハッシュ化した文字列を追加することで
+各 Glyph に一意な名前を付け、さらに同じ Glyph を異なる名前で複数回登録する無駄を
+省くために、同一ページにすでに同じ Glyph の定義があれば再定義をしない工夫をします。
-本当は、同じ Glyph を異なる名前で複数回登録する無駄を省くために、
-同じ名前の Glyph を検索して再利用すればよいのだけれど・・・
-それは時間があればまた後ほど。
+*** 副作用 [#yd378246]
+ただ、そのようにするとブラウザで「選択領域のみ印刷」した際、
+選択領域に Glyph の定義が入っていないと文字が出力されずに空白ができてしまいます。
+あと、Glyph 置き換えはサーバーへの負担も大きいし、一長一短ある感じかもしれません。
+** ソースコード [#s297b32b]
+
+一応、以下のコードで動いているみたい?
+
+作った svg ファイルは次回表示する際に負荷を軽減するためファイルにキャッシュしています。
+このキャッシュファイルの数が膨大になるため(現状で1万以上になっています・・・)、
+1フォルダあたりのファイル数を少なく保つよう工夫しました。
+
+具体的には、数式自身をハッシュ化して決めたファイル名の先頭の2文字を元にフォルダを掘り、
+その中へ入れています。1万ファイルが256フォルダに分けられるので、
+1フォルダあたりに40ファイル程度になっています。
+
+ LANG:html(linenumber)
+ <?php
+ //
+ // pukiwiki用 数式プラグイン (math.inc.php)
+ // Copywrite 2011 Osamu Takeuchi <osamu@big.jp>
+ //
+ // [履歴]
+ // 2011.05.04 初期リリース
+ // 2014.04.14 svg 変換を最新版 pstoedit に対応するため大幅書き直し
+ // svg 中の glyph を展開せずに使うことで svg をスリム化
+ // さらに同じ glyph を複数の svg で使い回すことでスリム化
+ // namespace を追加して関数名等の重複を避ける
+ //
+ // [インストール]
+ // ソースファイルを (pukiwiki)/plugin/math.inc.php として保存
+ //
+ // [設定]
+ // MATHIMG_DIR に画像ファイルが作成される
+ // 標準では (pukiwiki)/mathimg に画像ファイルが作成される
+ // function user_macros() に TeX マクロを記述できる
+ // function use_svg() でインライン svg を使うかどうかを判断する
+ //
+ // [使い方]
+ // &math(y=ax^2+bx+c); のように、&math(); 中に TeX ソース形式で
+ // 数式を記述する
+ // TeX ソースとしては amsmath, amssymb, bm, braket, color, multirow,
+ // bigdelim,graphicx パッケージを利用し、align* 環境で評価される
+ //
+ // [設定]
+ //
+ // 画像ファイルの保存場所
+ // constant MATHIMG_DIR
+ //
+ // function usepackage() // 利用するパッケージ
+ // function preamble_commands() // プリアンブルに追加するコード
+ // function declare_math_operators() // MathOperator の定義
+ // function in_document_commands() // document 内に追加するコード
+ // function in_equation_commands() // equation 環境内に追加するコード
+ // function math2tex($math) // tex ソースのひな形
+ //
+
+ namespace math2html {
+
+ // 画像ファイルの保存場所
+ define('MATH2HTML_DIR', 'mathimg');
+
+ // プリアンブルに追加するコード
+ function preamble_commands()
+ {
+ return
+ '';
+ }
+
+ // MathOperator の定義
+ function declare_math_operators()
+ {
+ $math_operators = array (
+ 'Res',
+ 'rot',
+ 'grad',
+ 'rank',
+ 'tr',
+ array('Image', 'Im'),
+ array('Kernel', 'Ker'),
+ array('DIV', 'div'),
+ array('ROT', 'rot'),
+ array('GRAD', 'grad'),
+ array('trace', 'tr')
+ );
+
+ $declare_math_operators = '';
+
+ for($i=0; $i<count($math_operators); $i=$i+1){
+ $name = '';
+ $decl = '';
+ if( is_array($math_operators[$i]) ) {
+ $name = $math_operators[$i][0];
+ $decl = $math_operators[$i][1];
+ } else {
+ $name = $math_operators[$i];
+ $decl = $math_operators[$i];
+ }
+ $declare_math_operators .=
+ sprintf("\\DeclareMathOperator*{\\$name}{%s}", $decl);
+ }
+
+ return $declare_math_operators;
+ }
+
+ // document 内に追加するコード
+ function in_document_commands()
+ {
+ return
+ '\newcommand{\MARU}[1]
+ {{\ooalign{\hfil#1\/\hfil\crcr\raise.167ex\hbox{\mathhexbox20D}}}}
+ \newcommand{\transpose}[0]{\,{}^t\!}
+ \newcommand{\D}[0]{{\rm d}}
+ \newcommand{\PD}[0]{\partial}
+ \newcommand{\comment}[1]{}';
+ }
+
+ // equation 環境内に追加するコード
+ function in_equation_commands()
+ {
+ return
+ '%%コーシーの主値積分%%%%
+ \def\Xint#1{\mathchoice
+ {\XXint\displaystyle\textstyle{#1}}%
+ {\XXint\textstyle\scriptstyle{#1}}%
+ {\XXint\scriptstyle\scriptscriptstyle{#1}}%
+ {\XXint\scriptscriptstyle\scriptscriptstyle{#1}}%
+ \!\int}
+ \def\XXint#1#2#3{{\setbox0=\hbox{$#1{#2#3}{\int}$}
+ \vcenter{\hbox{$#2#3$}}\kern-.5\wd0}}
+ \def\ddashint{\Xint=}
+ \def\dashint{\Xint-}
+ %%%%%%%%%%%%%%%%
+ \makeatletter
+ \def\@tvsp{\mathchoice{{}\mkern-4.5mu}{{}\mkern-4.5mu}{{}\mkern-2.5mu}{}}
+ \def\llangle{\left<\@tvsp\left<}
+ \def\rrangle{\right>\@tvsp\right>}
+ \def\bigllangle{\left<\@tvsp\bigl<}
+ \def\bigrrangle{\bigr>\@tvsp\right>}
+ \def\hsymbu#1{\smash{\lower1.8ex\hbox{\scalebox{2}{$#1$}}}}
+ \def\hsymbl#1{\smash{\hbox{\scalebox{2}{$#1$}}}}
+ \makeatother
+ %
+ \def\red#1{\textcolor{red}{#1}}
+ %';
+ }
+
+ // 利用するパッケージ
+ function usepackage()
+ {
+ return 'amsmath,amssymb,bm,braket,color,multirow,bigdelim,graphicx';
+ }
+
+ // tex ソースを作成
+ function math2tex($math)
+ {
+ $format =
+ '\documentclass[12pt,fleqn]{jarticle}
+ \pagestyle{empty}
+ \usepackage{' . usepackage() . '}
+ %%
+ %% preamble commands
+ %%
+ %s
+ %%
+ %% declare math operators
+ %%
+ %s
+ \begin{document}
+ %%
+ \setlength{\\hoffset}{-1in}
+ \setlength{\\voffset}{-1in}
+ \setlength{\\topmargin}{0mm}
+ \setlength{\\headheight}{0mm}
+ \setlength{\\headsep}{0mm}
+ \setlength{\\oddsidemargin}{0mm}
+ \setlength{\\mathindent}{0mm}
+ %%
+ %% in document commands
+ %%
+ %s
+ \begin{equation*}
+ %%
+ %% in equation commands
+ %%
+ %s
+ %%
+ \boxed{
+ \begin{split}
+ %s
+ \end{split}
+ }
+ %%
+ \end{equation*}
+ \end{document}
+ %%';
+
+ return sprintf(
+ $format,
+ preamble_commands(),
+ declare_math_operators(),
+ in_document_commands(),
+ in_equation_commands(),
+ str_replace("\n", "", $math)
+ );
+ }
+
+ // svg の transform matrix を再現
+ function transform_by_matrix($x, $y, $a, $b, $c, $d, $e, $f)
+ {
+ return [
+ $a * $x + $b * $y + $e,
+ $c * $x + $d * $y + $f
+ ];
+ }
+
+ // svg ソースを整形する
+ // - 外枠サイズにトリムする
+ // - 外枠を外す
+ // - Glyph に一意な名前を付ける
+ // - <title> を付加する
+ function format_svg($svg, $math)
+ {
+ // $math は TeX ソース
+ // $svg は改行で split されて渡される
+
+ $glyphs = array(); // ローカル Glyph 名から一意な Glyph 名への連想配列
+
+ $after_defs = 0; // </defs> 以前を探索中
+ $top_line = 0; // 外形線の上線の書かれた行
+ $bottom_line = 0; // 外形線の下線の書かれた行
+ for( $i = 0; $i < count($svg); ++$i ) {
+ if (!$after_defs) {
+ // </defs> 以前を探索中
+ if (preg_match('/\A<symbol .*id="([^"]+)"/', $svg[$i], $matches)) {
+ // Glyph 定義を発見
+ $glyph_id = $matches[1];
+ // path 情報から一意な名前を算出
+ $glyph_path = $svg[$i+1];
+ for( $j = $i+2; mb_substr($svg[$j],0,5)=="<path"; ++$j){
+ $glyph_path .= $svg[$j];
+ }
+ $glyph_hash = sha1($glyph_path);
+ // 名前を登録&書き換え
+ $glyphs[$glyph_id] = $glyph_hash;
+ $svg[$i] = preg_replace('/ id="[^"]+"/', " id=\"{$glyph_hash}\"", $svg[$i]);
+ // 処理済み行をスキップ
+ $i = $j; // $j は </symbol> を指している (後で ++$i される)
+ }
+ if (mb_substr($svg[$i],0,7)=="</defs>") {
+ $after_defs = 1;
+ }
+ } else {
+ // </defs> 以降を探索中
+ if (preg_match('/\A *<use .*xlink:href="#([^"]+)"/', $svg[$i], $matches)) {
+ // Glyph 名を一意な物に書き換える
+ $svg[$i] = preg_replace('/ xlink:href="#[^"]+"/',
+ " xlink:href=\"#{$glyphs[$matches[1]]}\"", $svg[$i]);
+ } else
+ if (mb_substr($svg[$i],0,5)=="<path") {
+ if ($top_line==0) {
+ // 初めての <path は上部外形線
+ $top_line = $i;
+ }
+ // 最後の <path は下部外形線
+ $bottom_line = $i;
+ }
+ }
+ }
+
+ // 上部外形線から左上座標を得る
+ preg_match('/d=" *M +([^ ]+) +([^ ]+) +L +([^ ]+) +([^ ]+) *" +'.
+ 'transform *= *" *matrix\\(([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)\\) *"/',
+ $svg[$top_line], $matches);
+ list($left, $top) = transform_by_matrix(
+ intval($matches[1]), intval($matches[2]),
+ intval($matches[5]), intval($matches[6]),
+ intval($matches[7]), intval($matches[8]),
+ intval($matches[9]), intval($matches[10])
+ );
+
+ // 下部外形線から右下座標を得る
+ preg_match('/d=" *M +([^ ]+) +([^ ]+) +L +([^ ]+) +([^ ]+) *" +'.
+ 'transform *= *" *matrix\\(([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)\\) *"/',
+ $svg[$bottom_line], $matches);
+ list($right, $bottom) = transform_by_matrix(
+ intval($matches[3]), intval($matches[4]),
+ intval($matches[5]), intval($matches[6]),
+ intval($matches[7]), intval($matches[8]),
+ intval($matches[9]), intval($matches[10])
+ );
+
+ // 外形寸法を算出
+ $width = $right - $left;
+ $height = $bottom - $top;
+
+ // <?xml を上書き
+ $svg[0] = $svg[1];
+
+ // <title> を挿入
+ $svg[1] = "<title>" . htmlspecialchars($math, ENT_COMPAT | ENT_HTML401, "UTF-8") . "</title>";
+
+ // 外形線を削除
+ $svg[$bottom_line] = '';
+ $svg[$bottom_line-1] = '';
+ $svg[$top_line+1] = '';
+ $svg[$top_line] = '';
+
+ // svg 画像をトリミング
+ $svg[0] = preg_replace('/width="[^"]+"/', "width=\"{$width}pt\"", $svg[0]);
+ $svg[0] = preg_replace('/height="[^"]+"/', "height=\"{$height}pt\"", $svg[0]);
+ $svg[0] = preg_replace('/viewBox="[^"]+"/',
+ "viewBox=\"{$left} {$top} {$width} {$height}\"", $svg[0]);
+
+ return $svg;
+ }
+
+ // math から svg を作成する
+ function math2svg($math)
+ {
+ $tex_src = math2tex($math);
+
+ $tmp_dir = sys_get_temp_dir();
+ $tmp_base = $tmp_dir . "/math_edit" . getmypid();
+ $tmp_tex = $tmp_base . ".tex";
+ $tmp_dvi = $tmp_base . ".dvi";
+ $tmp_pdf = $tmp_base . ".pdf";
+ $tmp_svg = $tmp_base . ".svg";
+ $tmp_any = $tmp_base . ".*";
+
+ $out = array();
+ # create tex src file
+ $fh = fopen($tmp_tex, 'w');
+ fwrite($fh, $tex_src);
+ fclose($fh);
+ # convert tex to dvi
+ exec("platex -interaction=nonstopmode -output-directory={$tmp_dir} {$tmp_tex}", $out, $ret);
+ if ($ret) return $out;
+ # convert dvi to pdf
+ exec("dvipdfmx -o {$tmp_pdf} {$tmp_base} 2>&1", $out, $ret);
+ if ($ret) return $out;
+ # convert pdf to svg
+ exec("pdf2svg {$tmp_pdf} {$tmp_svg}", $out, $ret);
+ if ($ret) return $out;
+ # read svg
+ $svg = file( $tmp_svg );
+ # remove temporary files
+ exec("rm {$tmp_any}", $out, $ret);
+
+ // svg ソースを整形する
+ // - 外枠サイズにトリムする
+ // - 外枠を外す
+ // - Glyph に一意な名前を付ける
+ // - <title> を付加する
+ return format_svg($svg, $math);
+ }
+
+ // 同じページに配置した svg にてすでに定義済みの glyph を取り除く
+ function remove_duplicated_glyphs($svg)
+ {
+ // 定義済み Glyph を覚えておく
+ global $defined_glyphs;
+ for( $i = 0; $i < count($svg); ++$i ) {
+ if (preg_match('/\A<symbol .*id="([^"]+)"/', $svg[$i], $matches)) {
+ $glyph_id = $matches[1];
+ if(!isset($defined_glyphs)) {
+ $defined_glyphs = array();
+ }
+ if(!isset($defined_glyphs[$glyph_id])) {
+ $defined_glyphs[$glyph_id] = 1;
+ } else {
+ // 定義済みなので削除する
+ $svg[$i] = '';
+ $svg[$i+1] = '';
+ $svg[$i+2] = '';
+ }
+ }
+ if (mb_substr($svg[$i],0,7)=="</defs>") {
+ break;
+ }
+ }
+ return $svg;
+ }
+
+ // $math から html (svg) を作成する
+ // 結果をファイルにキャッシュする
+ function convert($math)
+ {
+ global $error_log_written;
+ $math = mb_convert_encoding($math, "UTF-8", "EUC");
+
+ $image_base = sha1($math);
+ $image_dir = MATH2HTML_DIR . "/{$image_base[0]}/{$image_base[1]}";
+ $image_svg = "{$image_dir}/{$image_base}.svg";
+
+ if(!file_exists($image_svg)) {
+ $svg = math2svg($math);
+
+ if (mb_substr($svg[0],0,4)=="<svg") {
+ if(!file_exists($image_dir)){
+ mkdir($image_dir, 0770, true);
+ }
+ $fh = fopen($image_svg, "w");
+ fwrite($fh, join("", $svg));
+ fclose($fh);
+ }
+ } else {
+ $svg = file($image_svg);
+ }
+
+ if (mb_substr($svg[0],0,4)=="<svg") {
+ // 同じページに配置した svg にてすでに定義済みの glyph を取り除く
+ return join("", remove_duplicated_glyphs($svg));
+ } else {
+ if (!isset($error_log_written) && !preg_match('/math_edit\/editor.html/', $_SERVER['REQUEST_URI'])) {
+ error_log ('Math2html conversion error in ' . $_SERVER['REQUEST_URI']);
+ $error_log_written = 1;
+ }
+ // エラーメッセージを表示する
+ return
+ '<span class="math-error">[Math Conversion Error]<div><br>' .
+ preg_replace("/\\n/", "<br>\n", htmlspecialchars(
+ mb_convert_encoding(join("\n", $svg), "UTF-8", "EUC"),
+ ENT_COMPAT | ENT_HTML401, "UTF-8")) .
+ '</div></span>';
+ }
+ }
+
+ } // end of namespace math2html
+
+ namespace {
+
+ function plugin_math_convert()
+ {
+ return plugin_math_inline();
+ }
+
+ function plugin_math_inline()
+ {
+ $aryargs = func_get_args();
+ $math = join(",", $aryargs);
+ $math = rtrim($math, ","); //remove extra comma at the end.
+ return math2html\convert($math);
+ }
+
+ }
+
+ ?>
+
+現状このコードで試してみています。
+
+問題がなさそうであれば、このページ全体の構成を見直して書き直す予定です。
+
+*** 不具合 [#vbeffcfc]
+
+現状では、カラーを使っているところで表示が崩れるようです?
+
+ LANG:latex
+ {\color{red}赤}\ 黒\ {\color{blue}青}
+
+の結果で COLOR{RED}{赤黒}COLOR(BLUE){青} となってしまいます。。。
+
+* カラーが崩れるところを調べてみた [#d8cbb3d0]
+
+あ、上記コードにはない
+
+ LANG:php
+ $svg = preg_replace('/<\/g>\n<g>/', "\n", $svg);
+
+何て訳分からないのを入れていたのがまずかったようでした。
+
+とりあえずこれをコメントアウトして、あとは不具合はなさそう?
+
* コメント [#od7d9c0b]
#article_kcaptcha