作者:Yihui Xie
译者:郑宝童
日期:2021.??.??
2.2 Markdown extensions by bookdown
2.2 bookdown对Markdown的拓展
Although Pandoc’s Markdown is much richer than the original Markdown syntax, it still lacks a number of things that we may need for academic writing. For example, it supports math equations, but you cannot number and reference equations in multi-page HTML or EPUB output. We have provided a few Markdown extensions in bookdown to fill the gaps.
尽管Pandoc的Markdown比原生的Markdown语法要丰富得多,但它仍然缺少一些我们在学术写作中可能需要的东西。例如,它支持数学方程,但不能在多页HTML或EPUB输出中对方程进行编号和引用。我们在bookdown中提供了一些Markdown扩展来填补这些空白。
2.2.1 Number and reference equations
To number and refer to equations, put them in the equation environments and assign labels to them using the syntax
(\#eq:label)
, e.g.,
2.2.1数值和参考方程
要给方程式编号和引用方程式,请将方程式置于方程式环境中,并使用语法(#eq:label)为它们分配标签,例如:
\begin{equation}
f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
(\#eq:binom)
\end{equation}
It renders the equation below:
You may refer to it using \@ref(eq:binom)
, e.g., see Equation (2.1).
Equation labels must start with the prefix eq:
in bookdown. All labels in bookdown must only contain alphanumeric characters, :
, -
, and/or /
. Equation references work best for LaTeX/PDF output, and they are not well supported in Word output or e-books. For HTML output, bookdown can only number the equations with labels. Please make sure equations without labels are not numbered by either using the equation*
environment or adding \nonumber
or \notag
to your equations. The same rules apply to other math environments, such as eqnarray
, gather
, align
, and so on (e.g., you can use the align*
environment).
We demonstrate a few more math equation environments below. Here is an unnumbered equation using the equation*
environment:
你可以使用 \@ref(eq:binom)
, 例如: 方程式(2.1).
方程式在bookdown必须要以前缀开头 eq:
. 所有在bookdown的label必须包含字符 :
, -
, and/or /
. Equation references work best for LaTeX/PDF output, and they are not well supported in Word output or e-books. 对于 HTML 的输出, bookdown 可以只对方程式打上数字标签. Please make sure equations without labels are not numbered by either using the equation*
environment or adding \nonumber
or \notag
to your equations. The same rules apply to other math environments, such as eqnarray
, gather
, align
, and so on (e.g., you can use the align*
environment).
We demonstrate a few more math equation environments below. Here is an unnumbered equation using the equation*
environment:
\begin{equation*}
\frac{d}{dx}\left( \int_{a}^{x} f(u)\,du\right)=f(x)
\end{equation*}
Below is an align
environment (2.2):
\begin{align}
g(X_{n}) &= g(\theta)+g'({\tilde{\theta}})(X_{n}-\theta) \notag \\
\sqrt{n}[g(X_{n})-g(\theta)] &= g'\left({\tilde{\theta}}\right)
\sqrt{n}[X_{n}-\theta ] (\#eq:align)
\end{align}
You can use the split
environment inside equation
so that all lines share the same number (2.3). By default, each line in the align
environment will be assigned an equation number. We suppressed the number of the first line in the previous example using \notag
. In this example, the whole split
environment was assigned a single number.
\begin{equation}
\begin{split}
\mathrm{Var}(\hat{\beta}) & =\mathrm{Var}((X'X)^{-1}X'y)\\
& =(X'X)^{-1}X'\mathrm{Var}(y)((X'X)^{-1}X')'\\
& =(X'X)^{-1}X'\mathrm{Var}(y)X(X'X)^{-1}\\
& =(X'X)^{-1}X'\sigma^{2}IX(X'X)^{-1}\\
& =(X'X)^{-1}\sigma^{2}
\end{split}
(\#eq:var-beta)
\end{equation}
2.2.2 Theorems and proofs
Theorems and proofs are commonly used in articles and books in mathematics. However, please do not be misled by the names: a “theorem” is just a numbered/labeled environment, and it does not have to be a mathematical theorem (e.g., it can be an example irrelevant to mathematics). Similarly, a “proof” is an unnumbered environment. In this section, we always use the general meanings of a “theorem” and “proof” unless explicitly stated.
In bookdown, the types of theorem environments supported are in Table 2.1. To write a theorem, you can use the syntax below:
```{theorem}
Here is my theorem.
TABLE 2.1: Theorem environments in **bookdown**.
| Environment | Printed Name | Label Prefix |
| --- | --- | --- |
| theorem | Theorem | thm |
| lemma | Lemma | lem |
| corollary | Corollary | cor |
| proposition | Proposition | prp |
| conjecture | Conjecture | cnj |
| definition | Definition | def |
| example | Example | exm |
| exercise | Exercise | exr |
| hypothesis | Hypothesis | hyp |
To write other theorem environments, replace ````{theorem}` with other environment names in Table [2.1](https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#tab:theorem-envs), e.g., ````{lemma}`.<br />A theorem can have a `name` option so its name will be printed, e.g.,
```{theorem, name=”Pythagorean theorem”} For a right triangle, if $c$ denotes the length of the hypotenuse and $a$ and $b$ denote the lengths of the other two sides, we have a^2 + b^2 = c^2
```
If you want to refer to a theorem, you should label it. The label can be written after ````{theorem`, e.g.,
```{theorem, label=”foo”} A labeled theorem here.
```
The `label` option can be implicit, e.g., the following theorem has the label `bar`:
```{theorem, bar} A labeled theorem here.
```
After you label a theorem, you can refer to it using the syntax `\@ref(prefix:label)`. See the column `Label Prefix` in Table [2.1](https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#tab:theorem-envs) for the value of `prefix` for each environment. For example, we have a labeled and named theorem below, and `\@ref(thm:pyth)` gives us its theorem number [2.1](https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#thm:pyth):
```{theorem, pyth, name=”Pythagorean theorem”} For a right triangle, if $c$ denotes the length of the hypotenuse and $a$ and $b$ denote the lengths of the other two sides, we have a^2 + b^2 = c^2
```
**Theorem 2.1 (Pythagorean theorem) **For a right triangle, if cc denotes the length of the hypotenuse and aa and bb denote the lengths of the other two sides, we have<br />![](https://cdn.nlark.com/yuque/__latex/d459699ab031562a6c4fbb577db204bc.svg#card=math&code=a%5E2%20%2B%20b%5E2%20%3D%20c%5E2%0A&height=20&id=vHXoX)<br />The proof environments currently supported are `proof`, `remark`, and `solution`. The syntax is similar to theorem environments, and proof environments can also be named. The only difference is that since they are unnumbered, you cannot reference them.<br />We have tried to make all these theorem and proof environments work out of the box, no matter if your output is PDF, HTML, or EPUB. If you are a LaTeX or HTML expert, you may want to customize the style of these environments anyway (see Chapter [4](https://bookdown.org/yihui/bookdown/customization.html#customization)). Customization in HTML is easy with CSS, and each environment is enclosed in `<div></div>` with the CSS class being the environment name, e.g., `<div class="lemma"></div>`. For LaTeX output, we have predefined the style to be `definition` for environments `definition`, `example`, `exercise`, and `hypothesis`, and `remark` for environments `proof` and `remark`. All other environments use the `plain` style. The style definition is done through the `\theoremstyle{}` command of the **amsthm** package.<br />Theorems are numbered by chapters by default. If there are no chapters in your document, they are numbered by sections instead. If the whole document is unnumbered (the output format option `number_sections = FALSE`), all theorems are numbered sequentially from 1, 2, …, N. LaTeX supports numbering one theorem environment after another, e.g., let theorems and lemmas share the same counter. This is not supported for HTML/EPUB output in **bookdown**. You can change the numbering scheme in the LaTeX preamble by defining your own theorem environments, e.g.,
\newtheorem{theorem}{Theorem} \newtheorem{lemma}[theorem]{Lemma}
When **bookdown** detects `\newtheorem{theorem}` in your LaTeX preamble, it will not write out its default theorem definitions, which means you have to define all theorem environments by yourself. For the sake of simplicity and consistency, we do not recommend that you do this. It can be confusing when your Theorem 18 in PDF becomes Theorem 2.4 in HTML.<br />Theorem and proof environments will be hidden if the chunk option `echo` is set to `FALSE`. To make sure they are always shown, you may add the chunk option `echo=TRUE`, e.g.,
```{theorem, echo=TRUE} Here is my theorem.
```
Below we show more examples[4](https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#fn4) of the theorem and proof environments, so you can see the default styles in **bookdown**.<br />**Definition 2.1 **The characteristic function of a random variable XX is defined by<br />![](https://cdn.nlark.com/yuque/__latex/d12c96a3ba59c57425793751c42b24fd.svg#card=math&code=%5Cvarphi%20_%7BX%7D%28t%29%3D%5Coperatorname%20%7BE%7D%20%5Cleft%5Be%5E%7BitX%7D%5Cright%5D%2C%20%5C%3B%20t%5Cin%5Cmathcal%7BR%7D%0A&height=24&id=rT5a7)<br />**Example 2.1 **We derive the characteristic function of X∼U(0,1)X∼U(0,1) with the probability density function ![](https://cdn.nlark.com/yuque/__latex/b9e8c2432a8a531b7d07573e5ba9f7f0.svg#card=math&code=f%28x%29%3D%5Cmathbf%7B1%7D_%7Bx%20%5Cin%20%5B0%2C1%5D%7D&height=23&id=LAA3C).<br />![](https://cdn.nlark.com/yuque/__latex/738c4d694f956daec9037e013abbc428.svg#card=math&code=%5Cbegin%7Bequation%2A%7D%0A%5Cbegin%7Bsplit%7D%0A%5Cvarphi%20_%7BX%7D%28t%29%20%26%3D%20%5Coperatorname%20%7BE%7D%20%5Cleft%5Be%5E%7BitX%7D%5Cright%5D%5C%5C%0A%20%26%20%3D%5Cint%20e%5E%7Bitx%7Df%28x%29dx%5C%5C%0A%20%26%20%3D%5Cint_%7B0%7D%5E%7B1%7De%5E%7Bitx%7Ddx%5C%5C%0A%20%26%20%3D%5Cint_%7B0%7D%5E%7B1%7D%5Cleft%28%5Ccos%28tx%29%2Bi%5Csin%28tx%29%5Cright%29dx%5C%5C%0A%20%26%20%3D%5Cleft.%5Cleft%28%5Cfrac%7B%5Csin%28tx%29%7D%7Bt%7D-i%5Cfrac%7B%5Ccos%28tx%29%7D%7Bt%7D%5Cright%29%5Cright%7C_%7B0%7D%5E%7B1%7D%5C%5C%0A%20%26%20%3D%5Cfrac%7B%5Csin%28t%29%7D%7Bt%7D-i%5Cleft%28%5Cfrac%7B%5Ccos%28t%29-1%7D%7Bt%7D%5Cright%29%5C%5C%0A%20%26%20%3D%5Cfrac%7Bi%5Csin%28t%29%7D%7Bit%7D%2B%5Cfrac%7B%5Ccos%28t%29-1%7D%7Bit%7D%5C%5C%0A%20%26%20%3D%5Cfrac%7Be%5E%7Bit%7D-1%7D%7Bit%7D%0A%5Cend%7Bsplit%7D%0A%5Cend%7Bequation%2A%7D&height=344&id=XIA7B)<br />Note that we used the fact ![](https://cdn.nlark.com/yuque/__latex/359226ec3f3c776085fe397602f67ff6.svg#card=math&code=e%5E%7Bix%7D%3D%5Ccos%28x%29%2Bi%5Csin%28x%29%0A&height=23&id=MuSMS)twice.<br />**Lemma 2.1 **For any two random variables X1, X2, they both have the same probability distribution if and only if<br />![](https://cdn.nlark.com/yuque/__latex/e1c693caea33ddca5af5dc138fe53e04.svg#card=math&code=%5Cvarphi%20_%7BX_1%7D%28t%29%3D%5Cvarphi%20_%7BX_2%7D%28t%29&height=21&id=oKzNM)<br />**Theorem 2.2 **If X1, …, Xn are independent random variables, and a1a1, …, anan are some constants, then the characteristic function of the linear combination ![](https://cdn.nlark.com/yuque/__latex/2e4a645cd196c4d7603c98ef1846bd75.svg#card=math&code=S_n%3D%5Csum_%7Bi%3D1%7D%5Ena_iX_i&height=49&id=blYKy)<br />![](https://cdn.nlark.com/yuque/__latex/d4c26028488a48fd19a13ef88ea68559.svg#card=math&code=%5Cvarphi%20_%7BS_%7Bn%7D%7D%28t%29%3D%5Cprod_%7Bi%3D1%7D%5En%5Cvarphi%20_%7BX_i%7D%28a_%7Bi%7Dt%29%3D%5Cvarphi%20_%7BX_%7B1%7D%7D%28a_%7B1%7Dt%29%5Ccdots%20%5Cvarphi%20_%7BX_%7Bn%7D%7D%28a_%7Bn%7Dt%29%0A&height=49&id=cwU6I)<br />**Proposition 2.1 **The distribution of the sum of independent Poisson random variables <br />![](https://cdn.nlark.com/yuque/__latex/4b9fd163a1500a18f4003db28ffde4d7.svg#card=math&code=X_i%20%5Csim%20%5Cmathrm%7BPois%7D%28%5Clambda_i%29%2C%5C%3A%20i%3D1%2C2%2C%5Ccdots%2Cn%0A&height=20&id=jquio)is ![](https://cdn.nlark.com/yuque/__latex/0779837a2a63a14c0051cfb97da8b60b.svg#card=math&code=%5Cmathrm%7BPois%7D%28%5Csum_%7Bi%3D1%7D%5En%5Clambda_i%29%0A&height=49&id=C6Pgd)<br />_Proof. _The characteristic function of![](https://cdn.nlark.com/yuque/__latex/f691689103e5fe685fb45ae0e3dca50a.svg#card=math&code=X%5Csim%5Cmathrm%7BPois%7D%28%5Clambda%29%0A&height=20&id=HatmP) is ![](https://cdn.nlark.com/yuque/__latex/92d9853b82ee437b3c6f15ab3c7640bc.svg#card=math&code=%5Cvarphi%20_%7BX%7D%28t%29%3De%5E%7B%5Clambda%20%28e%5E%7Bit%7D-1%29%7D%0A&height=25&id=b0lDZ) Let![](https://cdn.nlark.com/yuque/__latex/75a5227f4961c2a460287835448890d9.svg#card=math&code=P_n%3D%5Csum_%7Bi%3D1%7D%5EnX_i&height=49&id=tIHef). We know from Theorem [2.2](https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#thm:chf-sum) that<br />![](https://cdn.nlark.com/yuque/__latex/875f19049c9c887e57da3180b65f73e4.svg#card=math&code=%5Cbegin%7Bequation%2A%7D%0A%5Cbegin%7Bsplit%7D%0A%5Cvarphi%20_%7BP_%7Bn%7D%7D%28t%29%20%26%20%3D%5Cprod_%7Bi%3D1%7D%5En%5Cvarphi%20_%7BX_i%7D%28t%29%20%5C%5C%0A%26%20%3D%5Cprod_%7Bi%3D1%7D%5En%20e%5E%7B%5Clambda_i%20%28e%5E%7Bit%7D-1%29%7D%20%5C%5C%0A%26%20%3D%20e%5E%7B%5Csum_%7Bi%3D1%7D%5En%20%5Clambda_i%20%28e%5E%7Bit%7D-1%29%7D%0A%5Cend%7Bsplit%7D%0A%5Cend%7Bequation%2A%7D%0A&height=127&id=AOaoD)<br />This is the characteristic function of a Poisson random variable with the paramete![](https://cdn.nlark.com/yuque/__latex/bd0da827380e8448a68be3bed2c15512.svg#card=math&code=%5Clambda%3D%5Csum_%7Bi%3D1%7D%5En%20%5Clambda_i%0A&height=49&id=CjN1c) From Lemma [2.1](https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#lem:chf-pdf), we know the distribution of Pn is![](https://cdn.nlark.com/yuque/__latex/0779837a2a63a14c0051cfb97da8b60b.svg#card=math&code=%5Cmathrm%7BPois%7D%28%5Csum_%7Bi%3D1%7D%5En%5Clambda_i%29%0A&height=49&id=J8Hmx)<br />_Remark. _In some cases, it is very convenient and easy to figure out the distribution of the sum of independent random variables using characteristic functions.<br />**Corollary 2.1 **The characteristic function of the sum of two independent random variables X1X1 and X2X2 is the product of characteristic functions of X1and X2, i.e.,<br />![](https://cdn.nlark.com/yuque/__latex/13474313a46386d9e6d5da42be806065.svg#card=math&code=%5Cvarphi%20_%7BX_1%2BX_2%7D%28t%29%3D%5Cvarphi%20_%7BX_1%7D%28t%29%20%5Cvarphi%20_%7BX_2%7D%28t%29&height=21&id=CpOkq)<br />**Exercise 2.1 (Characteristic Function of the Sample Mean) **Let![](https://cdn.nlark.com/yuque/__latex/f4b895c1a827eb4514dcc7b6a4cadefd.svg#card=math&code=%5Cbar%7BX%7D%3D%5Csum_%7Bi%3D1%7D%5En%20%5Cfrac%7B1%7D%7Bn%7D%20X_i%0A&height=49&id=bOHW9) be the sample mean of nn independent and identically distributed random variables, each with characteristic function φX. Compute the characteristic function of ![](https://cdn.nlark.com/yuque/__latex/58c3d0d0ebe063b8eb10b3dcf9c10b0c.svg#card=math&code=%5Cbar%7BX%7D%0A&height=18&id=DTta6).<br />_Solution. _Applying Theorem [2.2](https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#thm:chf-sum), we have<br />![](https://cdn.nlark.com/yuque/__latex/291546ef88fd03b8e08e045fe1f033c7.svg#card=math&code=%5Czeta%28s%29%20%3D%20%5Csum_%7Bn%3D1%7D%5E%7B%5Cinfty%7D%20%5Cfrac%7B1%7D%7Bn%5Es%7D%0A&height=49&id=M2hxZ)<br />**Hypothesis 2.1 (Riemann hypothesis) **The Riemann Zeta-function is defined as ![](https://cdn.nlark.com/yuque/__latex/291546ef88fd03b8e08e045fe1f033c7.svg#card=math&code=%5Czeta%28s%29%20%3D%20%5Csum_%7Bn%3D1%7D%5E%7B%5Cinfty%7D%20%5Cfrac%7B1%7D%7Bn%5Es%7D%0A&height=49&id=vnppe)<br />for complex values of ss and which converges when the real part of ss is greater than 1. The Riemann hypothesis is that the Riemann zeta function has its zeros only at the negative even integers and complex numbers with real part 1/2.
<a name="elE5g"></a>
### 2.2.3 Special headers
There are a few special types of first-level headers that will be processed differently in **bookdown**. The first type is an unnumbered header that starts with the token `(PART)`. This kind of headers are translated to part titles. If you are familiar with LaTeX, this basically means `\part{}`. When your book has a large number of chapters, you may want to organize them into parts, e.g.,
(PART) Part I {-}
Chapter One
Chapter Two
(PART) Part II {-}
Chapter Three
A part title should be written right before the first chapter title in this part, both title in the same document. You can use `(PART\*)` (the backslash before `*` is required) instead of `(PART)` if a part title should not be numbered.<br />The second type is an unnumbered header that starts with `(APPENDIX)`, indicating that all chapters after this header are appendices, e.g.,
Chapter One
Chapter Two
(APPENDIX) Appendix {-}
Appendix A
Appendix B
The numbering style of appendices will be automatically changed in LaTeX/PDF and HTML output (usually in the form A, A.1, A.2, B, B.1, …). This feature is not available to e-books or Word output.
> 2.2.4 Text references
<a name="Evn74"></a>
### 2.2.4 文字参考
You can assign some text to a label and reference the text using the label elsewhere in your document. This can be particularly useful for long figure/table captions (Section [2.4](https://bookdown.org/yihui/bookdown/figures.html#figures) and [2.5](https://bookdown.org/yihui/bookdown/tables.html#tables)), in which case you normally will have to write the whole character string in the chunk header (e.g., `fig.cap = "A long long figure caption."`) or your R code (e.g., `kable(caption = "A long long table caption.")`). It is also useful when these captions contain special HTML or LaTeX characters, e.g., if the figure caption contains an underscore, it works in the HTML output but may not work in LaTeX output because the underscore must be escaped in LaTeX.
The syntax for a text reference is `(ref:label) text`, where `label` is a unique label[5](https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#fn5) throughout the document for `text`. It must be in a separate paragraph with empty lines above and below it. The paragraph must not be wrapped into multiple lines, and should not end with a white space. For example,
(ref:foo) Define a text reference here.
Then you can use `(ref:foo)` in your figure/table captions. The text can contain anything that Markdown supports, as long as it is one single paragraph. Here is a complete example:
A normal paragraph.
(ref:foo) A scatterplot of the data cars
using base R graphics.
{r foo, fig.cap='(ref:foo)'}
plot(cars) # a scatterplot
```
Text references can be used anywhere in the document (not limited to figure captions). It can also be useful if you want to reuse a fragment of text in multiple places.
- Some examples are adapted from the Wikipedia page https://en.wikipedia.org/wiki/Characteristicfunction(probability_theory))↩︎
- You may consider using the code chunk labels.↩︎