MyST 语法速查表#

标题#

语法

示例

注释

# 一级标题
## 二级标题
### 三级标题
#### 四级标题
##### 五级标题
###### 六级标题
# MyST 速查表

1-6级标题,由#的数量表示

目标标题#

语法

示例

注释

(target_header)=
(myst_cheatsheet)=
# MyST 速查表

请参见 下文 如何引用目标标题。

引用目标标题#

可以使用 ref 内联角色 来引用目标,默认使用节标题:

{ref}`myst_cheatsheet`

你可以指定目标的文本:

{ref}`MyST 语法讲座 <myst_cheatsheet>`

另一种选择是使用 Markdown 语法:

[MyST 语法讲座](myst_cheatsheet)

引用#

语法

示例

注释

> 文本
> 这是一个引用

引用文本

主题分隔#

语法

示例

注释

---
这是一些文本的结束。

---

## 新标题

在输出中创建一条水平线

行注释#

语法

示例

注释

% 文本
一行
% 一个注释
另一行

更多信息请参见 注释

块分隔#

语法

示例

结果

+++
这是一个
+++ {"meta": "数据"}
块分隔的示例

这是一个

块分隔的示例

HTML 块#

语法

示例

结果

<标签名> 文本 <标签名>
<p> 这是一个段落 </p>

这是一个段落

链接#

语法

示例

结果

[文本](目标)
[Jupyter Book](https://jupyterbook.org)

Jupyter Book

[文本](相对路径)
[PDF 文档](../advanced/pdf)

PDF 文档

[文本](目标 "标题")
[Jupyter Book](https://jupyterbook.org "JB 主页")

Jupyter Book

<目标>
<https://jupyterbook.org>

https://jupyterbook.org

[文本][]
[Jupyter Book][intro_page]

[intro_page]: https://jupyterbook.org

Jupyter Book

列表#

有序列表#

示例

结果

1. 第一个项目
2. 第二个项目
    1. 第一个子项目
  1. 第一个项目

  2. 第二个项目

    1. 第一个子项目

1. 第一个项目
2. 第二个项目
    * 第一个子项目
  1. 第一个项目

  2. 第二个项目

    • 第一个子项目

无序列表#

示例

结果

* 第一个项目
* 第二个项目
  * 第一个子项目
  • 第一个项目

  • 第二个项目

    • 第一个子项目

* 第一个项目
  1. 第一个子项目
  2. 第二个子项目
  • 第一个项目

    1. 第一个子项目

    2. 第二个子项目

表格#

语法

示例

结果

| a    | b    |
| :--- | ---: |
| c    | d    |
|    Training   |   Validation   |
| :------------ | -------------: |
|        0      |        5       |
|     13720     |      2744      |

Training

Validation

0

5

13720

2744

```{list-table}
:header-rows: 1

* - Col1
  - Col2
* - Row1 under Col1
  - Row1 under Col2
* - Row2 under Col1
  - Row2 under Col2
```
```{list-table}
:header-rows: 1

* - Training
  - Validation
* - 0
  - 5
* - 13720
  - 2744
```

Training

Validation

0

5

13720

2744

```{list-table} 表格标题
:header-rows: 1
:name: 引用标签

* - Col1
  - Col2
* - Row1 under Col1
  - Row1 under Col2
* - Row2 under Col1
  - Row2 under Col2
```
```{list-table} 这个表格标题
:header-rows: 1
:name: 示例表格

* - Training
  - Validation
* - 0
  - 5
* - 13720
  - 2744
```
Table 2 这个表格标题#

Training

Validation

0

5

13720

2744

引用表格#

Note

为了引用一个表格,你必须为其添加一个标签。 要为你的表格添加标签,只需在表格中包含一个 :name: 参数,后面跟着你的表格标签。 为了添加一个编号引用,你还必须包含一个表格标题。参见上面的示例。

语法

示例

结果

{numref}`label`
{numref}`example-table` 是一个示例。

example-table 是一个示例。

{ref}`text <label>`
这个 {ref}`表格 <example-table>` 是一个示例。

这个 表格 是一个示例。

{numref}`text %s <label>`
{numref}`表 %s <example-table>` 是一个示例。

%s 是一个示例。

警示框#

语法

示例

结果

```{admonition} 标题
文本
```
```{admonition} 这是一个标题
这是一个带有标题的警示框示例。
```

这是一个标题

这是一个带有标题的警示框示例。

```{note}
文本
```

```{note} 文本
更多文本...
```
```{note} 笔记不需要**任何**参数,
因此内容可以从此处开始。
```

Note

笔记不需要任何参数, 因此内容可以从此处开始。

```{warning} 文本
更多文本...
```
```{warning} 这是一个示例
这是一个警告指令的示例。
```

Warning

这是一个示例 这是一个警告指令的示例。

```{tip} 文本
更多文本...
```
```{tip} 这是一个示例
这是一个提示指令的示例。
```

Tip

这是一个示例 这是一个提示指令的示例。

```{caution} 文本
更多文本...
```
```{caution} 这是一个示例
这是一个注意指令的示例。
```

Caution

这是一个示例 这是一个注意指令的示例。

```{attention} 文本
更多文本...
```
```{attention} 这是一个示例
这是一个注意指令的示例。
```

Attention

这是一个示例 这是一个注意指令的示例。

```{danger} 文本
更多文本...
```
```{danger} 这是一个示例
这是一个危险指令的示例。
```

Danger

这是一个示例 这是一个危险指令的示例。

```{error} 文本
更多文本...
```
```{error} 这是一个示例
这是一个错误指令的示例。
```

Error

这是一个示例 这是一个错误指令的示例。

```{hint} 文本
更多文本...
```
```{hint} 这是一个示例
这是一个提示指令的示例。
```

Hint

这是一个示例 这是一个提示指令的示例。

```{important} 文本
更多文本...
```
```{important} 这是一个示例
这是一个重要指令的示例。
```

Important

这是一个示例 这是一个重要指令的示例。

```{admonition} 标题
:class: warning
文本
```
```{admonition} 这是一个标题
:class: warning
这是一个带有标题和警告样式的警示框示例。
```

这是一个标题

这是一个带有标题和警告样式的警示框示例。

图片和图表#

语法

示例

结果

```{figure} ./path/to/figure.jpg
:name: label

标题
```
```{figure} ../images/C-3PO_droid.png
:height: 150px
:name: figure-example

这是我的图片标题!
```
../_images/C-3PO_droid.png

Fig. 12 这是我的图片标题!#

```{image} ./path/to/figure.jpg
:name: label
```
```{image} ../images/C-3PO_droid.png
:height: 150px
:name: image-example
```
../_images/C-3PO_droid.png

更多信息请参见 图片和图表Markdown 文件

引用图片#

语法

示例

结果

{numref}`label`
{numref}`figure-example`是一个
图表示例。

Fig. 12是一个 图表示例。

{numref}`text %s <label>`
{numref}`图 %s <figure-example>`
是一个示例。

图 12 是一个示例。

{ref}`text <label>`
这个{ref}`图 <figure-example>`
是一个示例。

这个 是一个示例。

引用图像#

语法

示例

结果

{ref}`文本 <标签>`
This {ref}`image <image-example>`
is an example.

This image is an example.

数学#

语法

示例

结果

行内

This is an example of an
inline equation $z=\sqrt{x^2+y^2}$.

This is an example of an inline equation \(z=\sqrt{x^2+y^2}\).

数学块

This is an example of a
math block

$$
z=\sqrt{x^2+y^2}
$$

This is an example of a math block

\[ z=\sqrt{x^2+y^2} \]

带标签的数学块

This is an example of a
math block with a label

$$
z=\sqrt{x^2+y^2}
$$ (mylabel)

This is an example of a math block with a label

(6)#\[ z=\sqrt{x^2+y^2} \]

数学指令

This is an example of a
math directive with a
label
```{math}
:label: eq-label

z=\sqrt{x^2+y^2}
```

This is an example of a math directive with a label

(7)#\[z=\sqrt{x^2+y^2}\]

更多信息请参见 数学与方程

引用数学指令#

语法

示例

结果

{eq}`标签`
Check out equation {eq}`eq-label`.

Check out equation (7).

代码#

行内代码#

示例:

Wrap in-line code blocks in backticks: `boolean example = true;`.

结果:

Wrap in-line code blocks in backticks: boolean example = true;.

代码和语法高亮#

示例:

```python
note = "Python syntax highlighting"
print(node)
```

```none
No syntax highlighting.
```

结果:

note = "Python syntax highlighting"
print(node)

No syntax highlighting.

可执行代码#

Warning

确保在 .ipynb.md 文件的开头包含此 front-matter YAML 块。

---
jupytext:
  formats: md:myst
  text_representation:
    extension: .md
    format_name: myst
kernelspec:
  display_name: Python 3
  language: python
  name: python3
---

示例:

```{code-cell} ipython3
note = "Python syntax highlighting"
print(note)
```

结果:

note = "Python syntax highlighting"
print(note)
Python syntax highlighting

更多信息请参见 完全用 Markdown 编写的笔记本

标签#

可以通过引入以下 tags 作为选项来应用于代码单元格:

标签选项

描述

示例

"full-width"

单元格占用所有水平空间

```{code-cell} ipython3
:tags: ["full-width"]
print("This is a test.")
```

"scroll-output"

使输出单元格可滚动

```{code-cell} ipython3
:tags: ["scroll-output"]
for ii in range(100):
  print("This is a test.")
```

"margin"

将代码单元格移到右侧边距

```{code-cell} ipython3
:tags: ["margin"]
print("This is a test.")
```

"hide-input"

隐藏单元格但显示输出

```{code-cell} ipython3
:tags: ["hide-input"]
print("This is a test.")
```

"hide-output"

隐藏单元格的输出

```{code-cell} ipython3
:tags: ["hide-output"]
print("This is a test.")
```

"hide-cell"

隐藏代码单元格的输入和输出

```{code-cell} ipython3
:tags: ["hide-cell"]
print("This is a test.")
```

"remove-input"

移除单元格的输入

```{code-cell} ipython3
:tags: ["remove-input"]
print("This is a test.")
```

"remove-output"

移除单元格的输出

```{code-cell} ipython3
:tags: ["remove-output"]
print("This is a test.")
```

"remove-cell"

移除整个代码单元格

```{code-cell} ipython3
:tags: ["remove-cell"]
print("This is a test.")
```

"raises-exception"

标记单元格为“预期错误”

```{code-cell} ipython3
:tags: ["raises-exception"]
while True print('Hello world')
```

粘贴变量#

示例:

```{code-cell} ipython3
from myst_nb import glue
my_variable = "here is some text!"
glue("glued_text", my_variable)
```

Here is an example of how to glue text: {glue:}`glued_text`

结果:

from myst_nb import glue
my_variable = "这是一些文本!"
glue("glued_text", my_variable)
'这是一些文本!'

以下是如何粘贴文本的示例:’这是一些文本!’

更多信息请参阅 在您的笔记本中粘贴变量

粘贴数字#

示例:

```{code-cell} ipython3
from myst_nb import glue
import numpy as np
import pandas as pd

ss = pd.Series(np.random.randn(4))
ns = pd.Series(np.random.randn(100))

glue("ss_mean", ss.mean())
glue("ns_mean", ns.mean(), display=False)
```

以下是如何粘贴数字的示例:{glue:}`ss_mean` 和 {glue:}`ns_mean`。

结果:

from myst_nb import glue
import numpy as np
import pandas as pd

ss = pd.Series(np.random.randn(4))
ns = pd.Series(np.random.randn(100))

glue("ss_mean", ss.mean())
glue("ns_mean", ns.mean(), display=False)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[3], line 2
      1 from myst_nb import glue
----> 2 import numpy as np
      3 import pandas as pd
      5 ss = pd.Series(np.random.randn(4))

ModuleNotFoundError: No module named 'numpy'

以下是如何粘贴数字的示例: 和 。

更多信息请参阅 在您的笔记本中粘贴变量

粘贴可视化#

示例:

```{code-cell} ipython3
from myst_nb import glue
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 200)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y, 'b-', linewidth=2)

glue("glued_fig", fig, display=False)
```

这是一个内联粘贴图形示例:{glue:}`glued_fig`。
这是一个将粘贴输出粘贴为块的示例:
```{glue:} glued_fig
```

结果:

from myst_nb import glue
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 200)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y, 'b-', linewidth=2)

glue("glued_fig", fig, display=False)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[4], line 2
      1 from myst_nb import glue
----> 2 import matplotlib.pyplot as plt
      3 import numpy as np
      5 x = np.linspace(0, 10, 200)

ModuleNotFoundError: No module named 'matplotlib'

这是一个内联粘贴图形示例:。 这是一个将粘贴输出粘贴为块的示例:

更多信息请参阅 在您的笔记本中粘贴变量

粘贴数学公式#

示例:

```{code-cell} ipython3
import sympy as sym
x, y = sym.symbols('x y')
z = sym.Function('z')
z = sym.sqrt(x**2+y**2)
glue("example_eq", z, display=False)
```

要粘贴数学公式,请尝试
```{glue:math} example_eq
:label: glue-eq-example
```

结果:

import sympy as sym
x, y = sym.symbols('x y')
z = sym.Function('z')
z = sym.sqrt(x**2+y**2)
glue("example_eq", z, display=False)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[5], line 1
----> 1 import sympy as sym
      2 x, y = sym.symbols('x y')
      3 z = sym.Function('z')

ModuleNotFoundError: No module named 'sympy'

要粘贴数学公式,请尝试:

更多信息请参阅 content:executable:output-insert

参考文档#

语法

示例

结果

{doc}`路径/到/文档`
更多信息请参阅 {doc}`../content/citations`。

更多信息请参阅 引用和参考文献

{doc}`文本 <路径/到/文档>`
更多信息请参阅 {doc}`这里 <../content/citations>`。

更多信息请参阅 这里

脚注#

语法

示例

结果

[^ref]

[^ref]: 脚注文本
这是一个脚注示例。[^footnote1]

[^footnote1]: 引用脚注的定义通常放在文档底部。

这是一个脚注引用。[1]

更多信息请参阅 脚注

引用#

Note

确保你有一个参考 bibtex 文件。你可以通过运行 touch references.bib 来创建一个,或者查看一个 references.bib 示例。

语法

示例

结果

{cite}`mybibtexcitation`
此示例生成以下引用 {cite}`perez2011python`。

此示例生成以下引用 [Perez et al., 2011]

要在文档中包含提及的引用列表,请引入 bibliography 指令

```{bibliography}
:filter: docname in docnames
```

更多信息请参阅 引用和参考文献