简介
Muse 借鉴了 latex2png.el 的功能,在它的源代码里面可以找到一个
muse-latex2png.el 就是用来实现这个功能的。加载这个文件之后,就可以在
Muse 文档里面使用一个 latex 标签,标签里面可以写 LaTeX 代码,最后会
转换成 png 图片插入到输出的 HTML 文档中。
默认情况下所有图片被放到发布目录里面的 ./latex/latex2png-MuseLatex2Png_242150247.pngtex 子目录下面,文件名是对公
式内容取 sxhash 的绝对值再加上可配置的前缀而得到的,这样的好处就是对于
内容相同的公式不用生成重复的图片。
其实生成图片的功能是调用 LaTeX 系统来完成的,要使用这个功能,必须保证系
统里面正确安装并配置好了 LaTeX 系统,以及 dvipng 程序。
配置好以后可以生成像这样的漂亮公式:
This is inline euqation
and there is a big one
and a even bigger one
只需要在 Muse 文档里面这样写就行了:
This is inline euqation $E=mc^2$ and there is a big one
\[
E = mc^2
\]
and a even bigger one
\[
E = mc^2
\]
配置
公式的大小
有一个变量 muse-latex2png-scale-factor 可以控制公式默认缩放的大小,但
是通常可能会需要不同大小的公式。特别是嵌入文字的公式应该和文字差不多大,
而单独显示出来的公式一般是比较大才比较漂亮。
所以我自己定义了一个函数来处理 latex 标签,先处理一下属性里面的
scale 属性再传递给 Muse 继续处理。
Inline formula
有时候需要在文本里面嵌入一些小公式,可以用
$a^2$
这种方式来书写,但是确实太麻烦了,所以我添加了一个正则匹配,可以直接在
Muse 文档里面使用 $a^2$ 这样的方式来书写公式。
公式的颜色
默认情况下公式是黑色透明背景的,不过有时候如果把输出的 HTML 页面背景设
为深色的话,这样就极不协调了。好在 LaTeX 可以方便地使用颜色,因此我们可
以设定一下 muse-latex2png-template 这个变量的值并在里面指定前景色即可
达到这个目的。
代码
这里是我的配置代码:
;;;;my extension and customization to muse-latex2png.el(require 'muse-latex2png)
;;muse-latex2png.el add `latex' tag to muse-publish-markup-tags, I;;only use this tag in html format, so I add this to;;muse-html-markup-tags and made my own customization.;;I define a new function `kid-muse-latex' in case I want to do some;;customization before pass it to `muse-publish-latex-tag' .;;;;here's my customization:;;* support for a scale attributes for latex tag(add-to-list 'muse-html-markup-tags
'("latex" t t nil kid-muse-html-latex))
;;I also want to use regexp to markup inline latex equations of the;;form `$\alpha$' because I'm too lazy to write;;$\alpha$(add-to-list 'muse-html-markup-regexps
'(1600 "\\$[^$]*\\$" 0 kid-muse-html-latex-inline))
(defvar kid-muse-html-latex-inline-default-scale "1"
"default scale for inline formular")
;;the color of the equation is defined in case that I use a;;background other than white in my HTML page.(setq muse-latex2png-template
"\\documentclass{article}
\\usepackage{fullpage}
\\usepackage{amssymb}
\\usepackage[usenames]{color}
\\usepackage{amsmath}
\\usepackage{latexsym}
\\usepackage[mathscr]{eucal}
%preamble%
\\pagestyle{empty}
\\begin{document}
{%code%}
\\end{document}\n")
(defun kid-muse-html-latex (beg end attrs)
(let* ((scale (cdr (assoc "scale" attrs)))
(muse-latex2png-scale-factor
(if scale
(string-to-number scale)
muse-latex2png-scale-factor)))
(muse-publish-latex-tag beg end attrs)))
(defun kid-muse-html-latex-inline ()
(let ((attrs `(("scale" . ,kid-muse-html-latex-inline-default-scale)
("inline" . "true"))))
(kid-muse-html-latex (match-beginning 0) (match-end 0) attrs)))
(setq muse-latex2png-scale-factor 1.5)
(require 'muse-colors)
(add-to-list 'muse-colors-tags
'("latex" t t nil muse-colors-example-tag))
修改
公式的大小
前面提到生成的图片的文件名是以公式的内容取 sxhash 而得到的,这样有一个
问题,因为我已经进行扩展,可以定制公式的大小。如果我在不同的地方出现了
相同的一个公式,那么后一次发布的文档会重新生成这个公式的图片,特别是两
个公式大小不一样的话,就会出现问题了。所以,需要修改一下
muse-latex2png.el 里面生成文件名的地方,把公式的大小也作为计算的一个因
素考虑进去就好了。这里是 patch :
---muse-latex2png.el2006-09-16 13:44:38.000000000 +0800
+++muse-latex2png.el.12006-09-16 13:46:38.000000000 +0800@@ -153,7 +153,11 @@
(temp-directory))
(t "/tmp")))
(texfile (expand-file-name-(concat prefix "_" (format "%d" (abs (sxhash code))))+(concat prefix "_" (format "%d"+(abs (sxhash+(concat code+(format "%d"+muse-latex2png-scale-factor))))))tmpdir))
(defalt-directory default-directory))
(with-temp-file (concat texfile ".tex")