Bento

Bento MathML

<head>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <script
      type="module"
      async
      src="https://cdn.ampproject.org/bento.mjs"
    ></script>
    <script nomodule src="https://cdn.ampproject.org/bento.js"></script>
    <script
      type="module"
      async
      src="https://cdn.ampproject.org/v0/bento-mathml-1.0.mjs"
    ></script>
    <script
      nomodule
      async
      src="https://cdn.ampproject.org/v0/bento-mathml-1.0.js"
    ></script>
    <link
      rel="stylesheet"
      type="text/css"
      href="https://cdn.ampproject.org/v0/bento-mathml-1.0.css"
    />
    <style>
      body {
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
          Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji',
          'Segoe UI Symbol';
        background: #ecf1f3;
      }
    </style>
</head>

<body>

    <h2>The Quadratic Formula</h2>
    <bento-mathml
      style="height: 40px"
      data-formula="\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]"
    ></bento-mathml>
    <h2>Inline formula</h2>
    <p>
      This is an example of a formula,
      <bento-mathml
        style="height: 40px; width: 147px"
        inline
        data-formula="\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]"
      ></bento-mathml>
      placed inline in the middle of a block of text.
    </p>
</body>

在 iframe 中渲染 MathML 公式。

将 bento-mathml 用作 Web 组件或 React 函数组件

↓ Web 组件 ↓ React / Preact

Web 组件

您必须包含每个 Bento 组件必需的 CSS 库,以确保正确加载,并在添加自定义样式之前。或者使用内联提供的轻量级预升级样式。请参阅 布局和样式

通过 npm 导入

npm install @bentoproject/mathml
import {defineElement as defineBentoMathml} from '@bentoproject/mathml';
defineBentoMathml();

通过 <script> 包含

<script type="module" src="https://cdn.ampproject.org/bento.mjs" crossorigin="anonymous"></script>
<script nomodule src="https://cdn.ampproject.org/bento.js" crossorigin="anonymous"></script>
<script type="module" src="https://cdn.ampproject.org/v0/bento-mathml-1.0.mjs" crossorigin="anonymous"></script>
<script nomodule src="https://cdn.ampproject.org/v0/bento-mathml-1.0.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.ampproject.org/v0/bento-mathml-1.0.css" crossorigin="anonymous">

示例

<head>

<script
      type="module"
      async
      src="https://cdn.ampproject.org/bento.mjs"
    ></script>
    <script nomodule src="https://cdn.ampproject.org/bento.js"></script>
    <script
      type="module"
      async
      src="https://cdn.ampproject.org/v0/bento-mathml-1.0.mjs"
    ></script>
    <script
      nomodule
      async
      src="https://cdn.ampproject.org/v0/bento-mathml-1.0.js"
    ></script>
    <link
      rel="stylesheet"
      type="text/css"
      href="https://cdn.ampproject.org/v0/bento-mathml-1.0.css"
    />
</head>

<body>

<h2>The Quadratic Formula</h2>
    <bento-mathml
      style="height: 40px;"
      data-formula="\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]"
    ></bento-mathml>

    <h2>Inline formula</h2>
    <p>
      This is an example of a formula,
      <bento-mathml
        style="height: 40px; width: 147px"
        inline
        data-formula="\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]"
      ></bento-mathml>
      placed inline in the middle of a block of text.
    </p>
</body>

布局和样式

每个 Bento 组件都有一个小型 CSS 库,您必须包含该库才能确保在没有 内容偏移 的情况下正确加载。由于基于顺序的特异性,您必须手动确保在任何自定义样式之前包含样式表。

<link
rel="stylesheet"
type="text/css"
href="https://cdn.ampproject.org/v0/bento-mathml-1.0.css"
/>

或者,您还可以使轻量级预升级样式内联可用

<style>
bento-mathml {
display: block;
overflow: hidden;
position: relative;
}
</style>

属性

data-formula(必需)

指定要渲染的公式。

inline(可选)

如果指定,组件将内联渲染(在 CSS 中为 inline-block)。

title(可选)

为组件定义一个 title 属性以传播到底层 <iframe> 元素。默认值为 "MathML formula"

样式

您可以使用 bento-mathml 元素选择器自由地设置手风琴的样式。


Preact/React 组件

通过 npm 导入

npm install @bentoproject/mathml
import React from 'react';
import {BentoMathml} from '@bentoproject/mathml/react';
import '@bentoproject/mathml/styles.css';

function App() {
return (
<>
<h2>The Quadratic Formula</h2>
<BentoMathml
style={{height: 40}}
formula="\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]"
></BentoMathml>

<h2>Inline formula</h2>
<p>
This is an example of a formula,{' '}
<BentoMathml
style={{height: 40, width: 147}}
inline
formula="\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]"
></BentoMathml>
, placed inline in the middle of a block of text. This shows how the formula will fit inside a block of text and can be styled with CSS.
</p>
</>
);
}

布局和样式

容器类型

BentoMathml 组件具有已定义的布局大小类型。为了确保组件正确渲染,请务必通过所需的 CSS 布局(例如使用 heightwidthaspect-ratio 或其他此类属性定义的布局)将大小应用于组件及其直接子组件。这些可以内联应用

<BentoMathml style={{width: 300, height: 100}}>...</BentoMathml>

或通过 className

<BentoMathml className="custom-styles">...</BentoMathml>
.custom-styles {
height: 40px;
width: 147px;
}

道具

formula(必需)

指定要渲染的公式。

inline(可选)

如果指定,组件将内联渲染(在 CSS 中为 inline-block)。

title(可选)

为组件定义一个 title 属性以传播到底层 <iframe> 元素。默认值为 "MathML formula"

更多详情