跳至主要内容

编写数学表达式

在 GitHub 上使用 Markdown 显示数学表达式。

谁可以使用此功能?

Markdown 可在 GitHub 网页界面中使用。

关于编写数学表达式

为了实现对数学表达式的清晰交流,GitHub 在 Markdown 中支持 LaTeX 格式的数学。更多信息请参见 Wikibooks 中的 LaTeX/数学

GitHub 的数学渲染功能使用 MathJax——一个开源的基于 JavaScript 的显示引擎。MathJax 支持广泛的 LaTeX 宏以及若干实用的可访问性扩展。更多信息请参见 MathJax 文档MathJax 可访问性扩展文档

数学表达式的渲染可在 GitHub Issues、GitHub Discussions、Pull Request、Wiki 和 Markdown 文件中使用。

编写行内表达式

在文本中内联数学表达式有两种分隔方式。您可以使用美元符号($)将表达式包围,或使用 $` 开始、`$ 结束的方式。当表达式本身包含与 Markdown 语法冲突的字符时,后者尤其有用。更多信息请参见 基础写作和格式化语法

This sentence uses `$` delimiters to show math inline: $\sqrt{3x-1}+(1+x)^2$

Screenshot of rendered Markdown showing an inline mathematical expression: the square root of 3x minus 1 plus (1 plus x) squared.

This sentence uses $\` and \`$ delimiters to show math inline: $`\sqrt{3x-1}+(1+x)^2`$

Screenshot of rendered Markdown showing an inline mathematical expression with backtick syntax: the square root of 3x minus 1 plus (1 plus x) squared.

将表达式写成块

要将数学表达式作为块显示,请另起一行并使用两个美元符号 $$ 将其包围。

提示

如果您在 .md 文件中编写,需要使用特定的换行方式,例如在行尾加入反斜杠(\),如下例所示。有关 Markdown 中换行的更多信息,请参见 基础写作和格式化语法

**The Cauchy-Schwarz Inequality**\
$$\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)$$

Screenshot of rendered Markdown showing a complex equation. Bold text reads "The Cauchy-Schwarz Inequality" above the formula for the inequality.

另外,您也可以使用 ```math 代码块语法来将数学表达式显示为块。使用该语法时,无需使用 $$ 分隔符。下面的示例将得到与上述相同的渲染效果

**The Cauchy-Schwarz Inequality**

```math
\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)
```

在数学表达式的行内及内部书写美元符号

若要在与数学表达式同一行中显示美元符号作为普通字符,需要对非分隔用的 $ 进行转义,以确保行能够正确渲染。

  • 在数学表达式内部,在显式的 $ 前添加一个 \ 符号。

    This expression uses `\$` to display a dollar sign: $`\sqrt{\$4}`$
    

    Screenshot of rendered Markdown showing how a backslash before a dollar sign displays the sign as part of a mathematical expression.

  • 在数学表达式外部但同一行时,用 <span> 标签将显式的 $ 包裹起来。

    To split <span>$</span>100 in half, we calculate $100/2$
    

    Screenshot of rendered Markdown showing how span tags around a dollar sign display the sign as inline text not as part of a mathematical equation.

延伸阅读

© . This site is unofficial and not affiliated with GitHub, Inc.