【发布时间】:2012-10-21 09:26:19
【问题描述】:
我有一些 mathjax 格式的 HTML 文本:
text = "an inline \\( f(x) = \frac{a}{b} \\) equation, a display equation \\[ F = m a \\] \n and another inline \\(y = x\\)"
(注意:方程式由单斜杠分隔,例如\(,而不是\\(,额外的\ 只是为ruby 文本转义第一个)。
我想获得将其替换为的输出,例如由 latex.codecogs 创建的图像,例如
desired_output = "an inline <img src="http://latex.codecogs.com/png.latex?f(x) = \frac{a}{b}\inline"/> equation, a display equation <img src="http://latex.codecogs.com/png.latex?F = m a"/> \n and another inline <img src="http://latex.codecogs.com/png.latex?y = x\inline"/> "
使用红宝石。我试试:
desired = text.gsub("(\\[)(.*?)(\\])", "<img src=\"http://latex.codecogs.com/png.latex?\2\" />")
desired = desired.gsub("(\\()(.*?)(\\))", "<img src=\"http://latex.codecogs.com/png.latex?\2\\inline\")
desired
但这并不成功,只返回原始输入。我错过了什么?如何适当地构建此查询?
【问题讨论】:
-
如果有人正在寻找它,我编写的用图像替换 mathjax 方程的 Jekyll 插件在这里:github.com/cboettig/labnotebook/blob/… 感谢@justin-ko 的帮助!