tex数式画像を生成できる mimetex

mimetexは img タグに埋め込んでtexみたいに数式生成をできるスグレもの。たとえば、

 <img src="/cgi-bin/mimetex.cgi?\int_{1}^{e}\frac{1}{t}dt=1" />

まずはコンパイル。

$ gcc -DAA mimetex.c gifsave.c -lm -o mimetex.cgi

シェル上でたたくとアスキーで表示される。

$ ./mimetex.cgi '\int_{1}^{e}\frac{1}{t}dt=1'
+-----------------------------------------------------------------------+
|mimeTeX vers 1.64, Copyright(c) 2002-2006, John Forkosh Associates, Inc|
+-----------------------------------------------------------------------+
| mimeTeX is free software, licensed to you under terms of the GNU/GPL, |
|           and comes with absolutely no warranty whatsoever.           |
+-----------------------------------------------------------------------+

Ascii dump of bitmap image...
.........***............................................................
........*..*............................................................
.......*..**............................................................
.......***..............................................................
.......*................................................................
.......*...*............................................................
........***.............................................................
........................................................................
...............**.......................................................
..............*..*......................................................
.............**.**......................................................
.............*..**......................................................
.............*..........................................................
............**..........................................................
............**..........................................................
............*.........*.................................................
............*.......***.................................................
...........**.........*.................................................
...........**.........*.................................................
...........**.........*.................................................
...........*..........*.................................................
..........**..........*.................................................
..........**..........*..........**.....................................
..........**..........*...........*................................*....
..........**..........*...........*......*.......................***....
..........*.........*****........*......*..........................*....
.........**......................*......*..........................*....
.........**..................***.*...******........................*....
.........**.........*****...*...**......*.......************.......*....
.........**.................*...*......*...........................*....
.........*.................*....*......*...........................*....
........**............*....*....*......*...........................*....
........**............*....*...**.*....*..*.....************.......*....
........**............*....*...*.*....*..*.........................*....
........**..........*****...***.**.....**........................*****..
........*............*..................................................
.......**............*..................................................
.......**............*..................................................
.......**............*..................................................
.......*............*..*................................................
.......*.............**.................................................
......**................................................................
......**................................................................
......*.................................................................
..**..*.................................................................
..**.**.................................................................
..*..*..................................................................
...**...................................................................
........................................................................
.........*..............................................................
.......***..............................................................
.........*..............................................................
.........*..............................................................
.........*..............................................................
.........*..............................................................
.........*..............................................................
.........*..............................................................
.........*..............................................................
.........*..............................................................
.......*****............................................................
以下略

Webサーバから呼ばれるときを想定して環境変数QUERY_STRINGをつけてやると、HTTPヘッダ付きのバイナリが生成される。

$  QUERY_STRING="\Large f(x)=\int_{-\infty}^x e^{-t^2}dt" ./mimetex.cgi
Cache-Control: max-age=7200
Content-Length: 754
Content-type: image/gif

GIF87a 呂   ・&#63731;&#63731;ユユユハハハタタタオオオゥゥゥnnnbbbTTTFFF …

頭の4行を除くと普通のgif87aなので、これをフィルタするとリダイレクトで数式画像ファイルを生成できる。個人的な好みでさらにpngに変換。

$ QUERY_STRING="\Large f(x)=\int_{-\infty}^x e^{-t^2}dt" ./mimetex.cgi | sed -e '1,4d' | gif2png

これでお手軽に数式を書けるね。