<semantics>

Baseline 已广泛支持

此功能已成熟,并且在众多设备和浏览器版本上都能正常工作。自 2023 年 1 月以来,它已在各浏览器中可用。

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

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

css
semantics > :not(:first-child) {
  display: none;
}

注意:旧的 MathML 规范允许渲染器根据可用的注释决定默认渲染方式。以下确定可见子元素的规则已在某些浏览器中实现。有关 Presentation MathML 和 Content MathML 之间的区别,请参阅 MathML 4

  • 如果没有其他规则适用:默认情况下,仅渲染第一个子元素,该子元素应为 Presentation MathML。
  • 如果第一个子元素是 Presentation MathML 元素(非 <annotation><annotation-xml>),则渲染第一个子元素。
  • 如果没有找到 Presentation 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",因为它不区分 Content MathML 或 Presentation MathML。

属性

此元素的属性包括 全局 MathML 属性

示例

html
<math display="block">
  <semantics>
    <!-- The first child is the MathML expression rendered by default. -->
    <mrow>
      <msup>
        <mi>x</mi>
        <mn>2</mn>
      </msup>
      <mo>+</mo>
      <mi>y</mi>
    </mrow>

    <!--
      Annotate with content MathML, a dedicated XML dialect
      to express the meaning of mathematical formulas.
    -->
    <annotation-xml encoding="application/mathml-content+xml">
      <apply>
        <plus />
        <apply>
          <power />
          <ci>x</ci>
          <cn type="integer">2</cn>
        </apply>
        <ci>y</ci>
      </apply>
    </annotation-xml>

    <!--
      Annotate with LaTeX, a lightweight markup language
      to write mathematical formulas.
    -->
    <annotation encoding="application/x-tex">x^{2} + y</annotation>
  </semantics>
</math>

技术摘要

隐式 ARIA 角色 None

规范

规范
MathML Core
# semantics-and-presentation

浏览器兼容性