<semantics>

<semantics> MathML 元素将注释与一个 MathML 表达式关联起来,例如其文本源作为轻量级标记语言或以特殊的 XML 语言表示的数学含义。通常,它的结构是:

  • 第一个子元素是要注释的 MathML 表达式。
  • 后续的 <annotation><annotation-xml> 元素,后者为 XML 格式(例如 OpenMath)而保留。

默认情况下,<semantics> 元素的第一个子元素会被渲染,而其他子元素的 display 属性会被设置为 none

备注: 旧版 MathML 规范允许渲染器根据可用的注释来决定默认呈现方式。以下规则用于确定可见子元素,且已在某些浏览器中实现。请参阅 MathML 4 以了解表现(Presentation)MathML 和内容(Content)MathML 之间的区别。

  • 如果没有其他规则适用:默认情况下只渲染第一个子元素,该元素应该是 表现 MathML。
  • 如果第一个子元素是除了 <annotation><annotation-xml> 之外的表现 MathML 元素,则渲染第一个子元素。
  • 如果未找到表现 MathML,则渲染 <semantics> 的第一个不带 src 属性的 <annotation><annotation-xml> 子元素。对于 <annotation-xml> 元素,encoding 属性必须等于以下值之一:
    • "application/mathml-presentation+xml"
    • "MathML-Presentation"
    • "SVG1.1"
    • "text/html"
    • "image/svg+xml"
    • "application/xml"

请注意,此处未提及 "application/mathml+xml",因为它区分内容 MathML 和表现 MathML。

属性

<semantics><annotation><annotation-xml> 元素接受全局 MathML 属性。此外,可以在 <annotation><annotation-xml> 元素上设置以下属性:

encoding

注释中语义信息的编码(例如 "MathML-Content""MathML-Presentation""application/openmath+xml""image/png"

src 已弃用

语义信息的外部源位置。

示例

html
<math display="block">
  <semantics>
    <!-- 第一个子元素是默认渲染的 MathML 表达式 -->
    <mrow>
      <msup>
        <mi>x</mi>
        <mn>2</mn>
      </msup>
      <mo>+</mo>
      <mi>y</mi>
    </mrow>

    <!-- 用公式的 PNG 图像注释 -->
    <annotation-xml encoding="MathML-Content">
      <apply>
        <plus />
        <apply>
          <power />
          <ci>x</ci>
          <cn type="integer">2</cn>
        </apply>
        <ci>y</ci>
      </apply>
    </annotation-xml>

    <!-- 用公式的 PNG 图像注释 -->
    <annotation encoding="image/png" src="some/path/formula.png" />

    <!-- 用 LaTeX 注释,这是一种用于编写数学公式的轻量标记语言 -->
    <annotation encoding="application/x-tex"> x^{2} + y </annotation>
  </semantics>
</math>

规范

Specification
MathML Core
# semantics-and-presentation

浏览器兼容性

BCD tables only load in the browser