目前数学公式在网络上显示的方法,在google里搜一下就会发现很多。
有用texlive把公式转为图片的如:
基于MoinMoin+ConTeXt实现包含数学内容的Wiki网站
MoinMoin数学公式环境安装与配置
等
也有用 jsmath的。
把数学公式转成图片嵌入到网页中的一个很大的麻烦是要安装texlive组合,并提供公式转成图片服务。好像有点麻烦。其实网上也有很多免费的tex公式转图片的服务。
MathJax是一个显示数学公式的js引擎,在它的网站上吹挺牛的。按安装说明先下载下来,运行自带的demo,确实不错。但用在moinmoin上有时会出现奇怪的问题,最常见的是公式中有多个^时公式不能显示出来。原因是wiki源码先被moinmoin自己解析,发到浏览器后mathjax才进行公式转换。在moinmoin解析时可能会在公式中加入一些看不见的标记或将一些tex标记当成了wiki标记来进行解析。
解决这个问题的关键是避免moinmoin自作多情解析数学公式,让数学公式源码完整地发到浏览器。
1.插加两个插件:
MathJax.py:
# -*- coding: iso-8859-1 -*- """ MoinMoin - MathJax Macro @copyright: 2012 luoboiqingcai <[email protected]> @license: GNU GPL """ Dependencies = ["pages"] #def macro_MathJax(macro,rawtex): # "return the raw tex as it is." # return macro.request.formatter.text(rawtex) def execute(macro, args): if not args: return "" else: return macro.request.formatter.text(args)
text_x_mathjax.py:
# -*- coding: iso-8859-1 -*- """ MoinMoin - tex Parser using MathJax js library @copyright: 2012 luoboiqingcai <[email protected]> @license: GNU GPL """ Dependencies = [] class Parser: """ Send plain text in a HTML <pre> element. """ ## specify extensions willing to handle ## should be a list of extensions including the leading dot ## TODO: remove the leading dot from the extension. This is stupid. #extensions = ['.txt'] ## use '*' instead of the list(!) to specify a default parser ## which is used as fallback extensions = '.tex' Dependencies = ['page'] def __init__(self, raw, request, **kw): self.raw = raw self.request = request self.form = request.form self._ = request.getText def format(self, formatter, **kw): """ Send the text. """ self.request.write(formatter.div(1)) self.request.write(formatter.text(self.raw.expandtabs()))#expandtabs is str's buildin method self.request.write(formatter.div(0))
把text_x_mathjax.py放到data/plugin/parser目录下,MathJax.py放到data/plugin/macro目录下。
2.安装mathjax:
2.1 把mathjax安装包解压并扔到apache可访问的某个目录中并配置好apache。
Alias /mysite/moin_static193/ "/usr/local/wiki/moin/mywiki/htdocs/"
2.2 在config/wikifarm/wikiname.py中加入下面这行:
html_head = '''<script type="text/javascript" src="/mysite/moin_static193/mathjax/MathJax.js?config=TeX-AMS-MML_SVG,local/local"></script>'''
2.3 修改mathjax包目录的config/local的local.js文件,加入:
MathJax.Hub.Config( { "HTML-CSS": { linebreaks: { automatic: true } }, SVG: { linebreaks: { automatic: true } }, tex2jax:{ inlineMath:[ ['$','$'] ] } } )
加入上面这些内容是让'$'成为行内公式的识别符,因为默认情况下'\['和'\]'行内公式的识别符。
3 重启服务器,比如:
/etc/init.d/apache restart
4 在moinmoin里插入数学公式:
插入行内公式:
<<MathJax($z=x^2+y^2$)>>
插入块级元素公式:
{{{#!mathjax
$$z=x^2+y^2$$
}}}
ps:当页面里有很多数学公式时mathjax的工作效率就有点低了。我在自己的电脑上架设的moinmoin,同样的页面在用了mathjax后比原来没用mathjax慢了不知道多少倍。而如果把texlive生成图片缓存起来,在第一次生成图片后以后的加载就快多了。