mathml支持

mathjax使用mathml作为其数学表达式内部格式的基础,因此mathml支持是在基础级别构建到mathjax中的。有一个 MathML input jax 用于将mathml元素转换为内部格式(表示mathml元素的javascript对象),并且有一种机制可以将内部格式转换为 MathJax.startup.toMML() (如果您使用的是mathjax组件)。

而mathjax版本2包含 NativeMML 输出jax以在网页中生成mathml输出,因为mathml在chrome、edge和ie浏览器中不可用,因为safari和firefox中的mathml支持不包括mathjax所需的所有功能(例如 <mlabeledtr> 元素,并且由于safari和firefox中的结果质量并不总是与mathjax的输出相当,因此 NativeMML mathjax版本3不再提供输出jax。

但是,如果愿意,可以使用mathjax的mathml序列化特性来实现自己的本地mathml输出。下面是一个这样做的示例,用于将tex输入转换为mathml输出。

<style>
mjx-container[display="block"] {
  display: block;
  margin: 1em 0;
}
</style>
<script>
MathJax = {
  //
  //  Load only TeX input and the contextual menu
  //
  loader: {load: ['input/tex', 'ui/menu']},
  //
  //  When page is ready, render the math in the document
  //
  //
  //  When page is ready:
  //    disable the assistive-mathml menu item
  //    render the document, handling require and autoload calls
  //
  startup: {
    pageReady() {
      MathJax.startup.document.menu.menu.findID('Accessibility', 'AssistiveMml').disable();
      MathJax._.mathjax.mathjax.handleRetriesFor(() => MathJax.startup.document.render());
    }
  },
  //
  //  Override the usual typeset render action with one that generates MathML output
  //
  options: {
    renderActions: {
      assistiveMml: [],  // disable assistive mathml
      typeset: [150,
        (doc) => {for (math of doc.math) {MathJax.config.renderMathML(math, doc)}},
        (math, doc) => MathJax.config.renderMathML(math, doc)
      ]
    },
    menuOptions: {
      settings: {
        assistiveMml: false
      }
    }
  },
  //
  // The action to use for rendering MathML
  //
  renderMathML(math, doc) {
    math.typesetRoot = document.createElement('mjx-container');
    math.typesetRoot.innerHTML = MathJax.startup.toMML(math.root);
    math.display && math.typesetRoot.setAttribute('display', 'block');
  }
};
</script>
<script type="text/javascript" id="MathJax-script" async
  src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/startup.js">
</script>

此示例使用 启动 只加载 input/texcontextual menu 组件,并定义替换标准 typeset 创建mathjax容器元素并将其存储在 math.typesetRoot ,然后将内部格式转换为mathml字符串(通过 MathJax.startup.toMML() )并让浏览器将其解析为dom元素(通过 innerHTML )稍后的呈现操作将把容器及其mathml内容移动到dom的适当位置。对于显示样式的数学,容器用一个属性标记,这样css就可以用来使容器成为具有上下边距的块级元素。

该示例还采取了几个步骤来禁用assistivemathml扩展,该扩展为通常的输出呈现插入隐藏的MathML。这是不必要的,因为我们自己生成MathML作为主要输出。设置 menuOptions.settings.assistiveMml 选择权 false 关闭上下文菜单中的辅助MathML。这个 pageReady() 函数还包括一行禁用菜单中的assistivemathml项,因此用户不能意外地再次打开它。最后 assistiveMml render action被禁用,因为它永远不会被激活(也许会有点过火,但不需要白运行通常的代码)。

注解

mathjax的版本2 nativemml输出处理器解决了firefox/gecko和safari/webkit的各种限制(例如,提供对等式标签的支持),但是这种方法没有,因为它只是使用通用的mathml。