【问题标题】:Rendering MathML on svg using d3.js使用 d3.js 在 svg 上渲染 MathML
【发布时间】:2015-01-08 12:57:11
【问题描述】:

我正在尝试使用 d3.js 在 svg 上渲染 MathML 方程。谁能帮我得到一个关于svg的二次方程。我试过用异物做,但没有成功。

【问题讨论】:

  • 你能告诉我们一些你尝试实现的代码吗?
  • 我从一个示例开始。代码在jsfiddle.net/p3wuu2pw

标签: svg d3.js mathml


【解决方案1】:

我花了很长时间试图让它在 JSFiddle 中运行,但没有成功,但它在我的 PC 上运行良好。 JSFiddle here。您介意尝试以下方法并告诉我它是否也适用于您吗?

步骤 1. 加载 MathJax

<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

第 2 步。使用此代码附加一个 foreignObject

var svg = d3.select("body").append("svg").attr("width",400).attr("height",400)
var text = svg.append("foreignObject").attr("width",100).attr("height",100)
text.text("$$ x = \\sum_{i \\in A} i^{2} $$")
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);

但是,如果您仍然更喜欢 MathML,那么您可以使用以下方法:

text.html("<math display=\"block\"><mrow><mi>x</mi><mo>=</mo><mfrac><mrow><mo>−</mo><mi>b</mi><mo>±</mo><msqrt><mrow><msup><mi>b</mi><mn>2</mn></msup><mo>−</mo><mn>4</mn><mi>a</mi><mi>c</mi></mrow></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mrow></math>")

我知道我正在添加更多脚本供您加载,但我的理解是,MathML 已不再被广泛使用。

希望对你有帮助。

编辑 最后是一个 JSFiddle:link

谢谢

【讨论】:

  • 这很好,但我们将输入作为正确的 MathML,需要在 SVG 上呈现
  • 没问题。我认为使用 MathJax 渲染 MathML 会更好。另外,我不确定是否真的不再支持 MathML。 Chrome 没有显示它(正如@RobertLongson 提到的)
  • 又是同样的问题。是否可以将 MathML 转换为 MathJax 语法。
  • 未来注意事项:cdn.mathjax.org 即将结束生命周期,请查看mathjax.org/cdn-shutting-down 了解迁移提示。
【解决方案2】:

你有两个错误

  • foreignObject 必须具有宽度/高度属性
  • mathml 元素必须在 mathml 命名空间中创建

在此修复这些结果...

d3.ns.prefix.mathml = "http://www.w3.org/1998/Math/MathML";

var foreignObject = d3.select("body")
    .append("svg")
var x = foreignObject.append("foreignObject")
    .attr("requiredExtensions", "http://www.w3.org/1999/xhtml")
    .attr("width", "100")
    .attr("height", "100")
var text = x.append("mathml:mo")
var row = x.append("mathml:mrow")
row.append("mathml:mi").text("a")
row.append("mathml:mo").text('\u2062')
var msup = row.append("msup")
msup.append("mathml:mi").text("x")
msup.append("mathml:mi").text("2")
row.append("mathml:mo").text("+")
row.append("mathml:mi").text("b")
row.append("mathml:mo").text('\u2062')
row.append("mathml:mi").text('x')
row.append("mathml:mo").text('+')
row.append("mathml:mi").text('c')

or as a fiddle

【讨论】:

  • 我试过 ax square + bx + c。但同样没有成功 JSFiddle:jsfiddle.net/p3wuu2pw/3
  • 感谢您的努力。这很奇怪。它在 Chrome 中不起作用。有什么我想念的吗?
  • 是Chrome在SVG上不支持MathML还是根本不支持MathML
  • 后者。根本没有数学。
  • 哎哟。需要为此找到另一种解决方案。是否可以将 MathML 转换为 MathJax 然后加载它。上面给出的其他解决方案适用于 chrome。所以只要大声思考 id MathML 就可以转换为 MathJax 语法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-08
  • 1970-01-01
  • 2016-11-15
  • 2013-09-29
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多