2.1 Markdown 语法

在本节中,我们将非常简要地介绍 Pandoc 风格的 Markdown。熟悉 Markdown 的读者可以跳过这一节。Pandoc 风格的 Markdown 的全部语法可以在 Pandoc 网站 http://pandoc.org 上找到。

2.1.1 内联格式

要将文本变为 斜体 (italic),可以用下划线或星号将它围起来,例如 _text_*text*。对于 粗体 (bold) 文本,可以使用双下划线 (__text__) 或双星号 (**text**)。被 ~ 围住的文本将会被转换为下标(例如 H~2~SO~4~ 呈现为 H2SO4)。类似地,两个脱字符 (^) 能够产生上标(例如 Fe^2+^ 渲染为 Fe2+)。为了把文本标注为 内联代码 (inline code),使用一堆反引号,例如 `code`3小型大写字母 (Small caps) 能够通过 HTML 标签 span 呈现出来,例如 <span style="font-variant:small-caps;">Small Caps</span> 呈现为 Small Caps。链接是使用 [text](link) 呈现的,例如 [RStudio](https://www.rstudio.com),图片的语法也类似:在前面加一个感叹号即可,例如 ![alt text or image title](path/to/image)。脚注放进脱字符 (^) 后面的方括号内 ^[],例如 ^[This is a footnote.]。我们将在第 2.8 节内讨论引文 (citations)。

2.1.2 块级元素

小节标题可以在若干 # 号之后写入,例如:

# First-level header

## Second-level header

### Third-level header

如果你不想对某个标题进行编号,可以在标题后面添加 {-},例如:

# Preface {-}

无序列表以 *-+ 开头,并且你可以通过缩进四个空格将另一个列表嵌套进一个列表中,例如

- one item
- one item
- one item
    - one item
    - one item

输出为:

  • one item
  • one item
  • one item
    • one item
    • one item

嵌套列表以数字开头(嵌套列表的书写规则同上),例如:

1. the first item
2. the second item
3. the third item

输出结果与 Markdown 源代码并没有太多不同:

  1. the first item
  2. the second item
  3. the third item

块引用 (blockquotes) 写在 > 之后,例如:

> "I thoroughly disapprove of duels. If a man should challenge me,
  I would take him kindly and forgivingly by the hand and lead him
  to a quiet place and kill him."
>
> --- Mark Twain

实际输出为(我们在本书中为块引用定制了样式):

“I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him.”

— Mark Twain

纯文本代码块可以在三个或更多的反引号后写入,也可以将块缩进四个空格,例如:

```
This text is displayed verbatim / preformatted
```

Or indent by four spaces:

    This text is displayed verbatim / preformatted

2.1.3 数学表达式

内联 LaTeX 方程 可以使用 LaTeX 语法写在一对美元符号 ($) 内,例如:$f(k) = {n \choose k} p^{k} (1-p)^{n-k}$(实际输出为:\(f(k)=\binom{n}{k}p^{k}(1-p)^{n-k}\));展示样式的数学表达式可以用一对双美元符号表示,例如: $$f(k) = \binom{n}{k} p^{k} (1-p)^{n-k}$$,其输出看起来像这样:

\[f\left(k\right)=\binom{n}{k}p^k\left(1-p\right)^{n-k}\]

你也能够在 $ $$$ $$ 中使用数学环境,例如:

$$\begin{array}{ccc}
x_{11} & x_{12} & x_{13}\\
x_{21} & x_{22} & x_{23}
\end{array}$$

\[\begin{array}{ccc} x_{11} & x_{12} & x_{13}\\ x_{21} & x_{22} & x_{23} \end{array}\]

$$X = \begin{bmatrix}1 & x_{1}\\
1 & x_{2}\\
1 & x_{3}
\end{bmatrix}$$

\[X = \begin{bmatrix}1 & x_{1}\\ 1 & x_{2}\\ 1 & x_{3} \end{bmatrix}\]

$$\Theta = \begin{pmatrix}\alpha & \beta\\
\gamma & \delta
\end{pmatrix}$$

\[\Theta = \begin{pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]

$$\begin{vmatrix}a & b\\
c & d
\end{vmatrix}=ad-bc$$

\[\begin{vmatrix}a & b\\ c & d \end{vmatrix}=ad-bc\]


  1. 为了呈现文本性的反引号,需要在外部使用更多的反引号,例如你可以使用两个反引号使内部的一个反引号能够呈现出来:`` `code` ``↩︎