You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: inst/examples/02-components.Rmd
+74-53Lines changed: 74 additions & 53 deletions
Original file line number
Diff line number
Diff line change
@@ -230,11 +230,14 @@ Theorems\index{theorem} and proofs are commonly used in articles and books in ma
230
230
In **bookdown**, the types of theorem environments supported are in Table \@ref(tab:theorem-envs). To write a theorem, you can use the syntax below:
231
231
232
232
````markdown
233
-
`r ''````{theorem}
234
-
Here is my theorem.
235
-
```
233
+
::: {.theorem}
234
+
This is a `theorem` environment that can contain **any**
235
+
_Markdown_ syntax.
236
+
:::
236
237
````
237
238
239
+
This syntax is based on Pandoc's [fenced `Div` blocks](https://pandoc.org/MANUAL.html#divs-and-spans) and can already be used in any R Markdown document to write [custom blocks.](https://bookdown.org/yihui/rmarkdown-cookbook/custom-blocks.html)**Bookdown** only offers special handling for theorem and proof environments. Since this uses the syntax of Pandoc's Markdown, you can write any valid Markdown text inside the block.
240
+
238
241
(ref:theorem-envs) Theorem environments in **bookdown**.
To write other theorem environments, replace ```` ```{theorem}```` with other environment names in Table \@ref(tab:theorem-envs), e.g., ```` ```{lemma}````.
252
+
To write other theorem environments, replace `::: {.theorem}` with other environment names in Table \@ref(tab:theorem-envs), e.g., `::: {.lemma}`.
250
253
251
-
A theorem can have a `name`option so its name will be printed, e.g.,
254
+
A theorem can have a `name`attribute so its name will be printed. For example,
252
255
253
256
````markdown
254
-
`r ''````{theorem, name="Pythagorean theorem"}
257
+
::: {.theorem name="Pythagorean theorem"}
255
258
For a right triangle, if $c$ denotes the length of the hypotenuse
256
259
and $a$ and $b$ denote the lengths of the other two sides, we have
257
260
$$a^2 + b^2 = c^2$$
258
-
```
259
-
````
260
-
261
-
If you want to refer to a theorem, you should label it. The label can be written after ```` ```{theorem````, e.g.,
262
-
263
-
````markdown
264
-
`r ''````{theorem, label="foo"}
265
-
A labeled theorem here.
266
-
```
261
+
:::
267
262
````
268
263
269
-
The `label` option can be implicit, e.g., the following theorem has the label `bar`:
264
+
If you want to refer to a theorem, you should label it. The label can be provided as an ID to the block of the form `#label`. For example,
270
265
271
266
````markdown
272
-
`r ''````{theorem, bar}
267
+
::: {.theorem #foo}
273
268
A labeled theorem here.
274
-
```
269
+
:::
275
270
````
276
271
277
272
After you label a theorem, you can refer to it using the syntax `\@ref(prefix:label)`.\index{cross-reference} See the column `Label Prefix` in Table \@ref(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 \@ref(thm:pyth):
For a right triangle, if $c$ denotes the length of the hypotenuse
282
277
and $a$ and $b$ denote the lengths of the other two sides, we have
283
278
284
279
$$a^2 + b^2 = c^2$$
285
-
```
280
+
:::
286
281
````
287
282
288
-
```{theorem,pyth, name="Pythagorean theorem"}
283
+
::: {.theorem #pyth name="Pythagorean theorem"}
289
284
For a right triangle, if $c$ denotes the length of the hypotenuse
290
285
and $a$ and $b$ denote the lengths of the other two sides, we have
291
286
292
287
$$a^2 + b^2 = c^2$$
293
-
```
288
+
:::
294
289
295
-
The proof environments currently supported are `r knitr::combine_words(names(bookdown:::label_names_math2), before = '\x60')`. 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.
290
+
The proof environments currently supported are `r knitr::combine_words(names(bookdown:::label_names_math2), before = '\x60')`. The syntax is similar to theorem environments, and proof environments can also be named using the `name` attribute. The only difference is that since they are unnumbered, you cannot reference them, even if you provide an ID to a proof environment.
296
291
297
-
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 \@ref(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 `r knitr::combine_words(bookdown:::style_definition, before='\x60')`, and `remark` for environments `r knitr::combine_words(c('proof', bookdown:::style_remark), before='\x60')`. All other environments use the `plain` style. The style definition is done through the `\theoremstyle{}` command of the **amsthm** package.
292
+
We have tried to make all these theorem and proof environments work out of the box, no matter if your output is PDF or HTML. If you are a LaTeX or HTML expert, you may want to customize the style of these environments anyway (see Chapter \@ref(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 `r knitr::combine_words(bookdown:::style_definition, before='\x60')`, and `remark` for environments `r knitr::combine_words(c('proof', bookdown:::style_remark), before='\x60')`. All other environments use the `plain` style. The style definition is done through the `\theoremstyle{}` command of the **amsthm** package. If you do not want the default theorem definitions to be automatically added by **bookdown**, you can set `options(bookdown.theorem.preamble = FALSE)`. This can be useful, for example, to avoid conflicts in single documents (Section \@ref(a-single-document)) using the output format `bookdown::pdf_book` with a `base_format` that has already included **amsmath** definitions.
298
293
299
294
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.,
300
295
@@ -305,24 +300,16 @@ Theorems are numbered by chapters by default. If there are no chapters in your d
305
300
306
301
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.
307
302
308
-
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.,
309
-
310
-
````markdown
311
-
`r ''````{theorem, echo=TRUE}
312
-
Here is my theorem.
313
-
```
314
-
````
315
-
316
303
Below we show more examples^[Some examples are adapted from the Wikipedia page https://en.wikipedia.org/wiki/Characteristic_function_(probability_theory)] of the theorem and proof environments, so you can see the default styles in **bookdown**.
317
304
318
-
```{definition}
305
+
::: {.definition}
319
306
The characteristic function of a random variable $X$ is defined by
We derive the characteristic function of $X\sim U(0,1)$ with the probability density function $f(x)=\mathbf{1}_{x \in [0,1]}$.
327
314
328
315
\begin{equation*}
@@ -339,25 +326,25 @@ We derive the characteristic function of $X\sim U(0,1)$ with the probability den
339
326
\end{equation*}
340
327
341
328
Note that we used the fact $e^{ix}=\cos(x)+i\sin(x)$ twice.
342
-
```
329
+
:::
343
330
344
-
```{lemma,chf-pdf}
331
+
::: {.lemma #chf-pdf}
345
332
For any two random variables $X_1$, $X_2$, they both have the same probability distribution if and only if
346
333
347
334
$$\varphi _{X_1}(t)=\varphi _{X_2}(t)$$
348
-
```
335
+
:::
349
336
350
-
```{theorem,chf-sum}
337
+
::: {.theorem #chf-sum}
351
338
If $X_1$, ..., $X_n$ are independent random variables, and $a_1$, ..., $a_n$ are some constants, then the characteristic function of the linear combination $S_n=\sum_{i=1}^na_iX_i$ is
The distribution of the sum of independent Poisson random variables $X_i \sim \mathrm{Pois}(\lambda_i),\: i=1,2,\cdots,n$ is $\mathrm{Pois}(\sum_{i=1}^n\lambda_i)$.
358
-
```
345
+
:::
359
346
360
-
```{proof}
347
+
::: {.proof}
361
348
The characteristic function of $X\sim\mathrm{Pois}(\lambda)$ is $\varphi _{X}(t)=e^{\lambda (e^{it}-1)}$. Let $P_n=\sum_{i=1}^nX_i$. We know from Theorem \@ref(thm:chf-sum) that
362
349
363
350
\begin{equation*}
@@ -369,34 +356,68 @@ The characteristic function of $X\sim\mathrm{Pois}(\lambda)$ is $\varphi _{X}(t)
369
356
\end{equation*}
370
357
371
358
This is the characteristic function of a Poisson random variable with the parameter $\lambda=\sum_{i=1}^n \lambda_i$. From Lemma \@ref(lem:chf-pdf), we know the distribution of $P_n$ is $\mathrm{Pois}(\sum_{i=1}^n\lambda_i)$.
372
-
```
359
+
:::
373
360
374
-
```{remark}
361
+
::: {.remark}
375
362
In some cases, it is very convenient and easy to figure out the distribution of the sum of independent random variables using characteristic functions.
376
-
```
363
+
:::
377
364
378
-
```{corollary}
365
+
::: {.corollary}
379
366
The characteristic function of the sum of two independent random variables $X_1$ and $X_2$ is the product of characteristic functions of $X_1$ and $X_2$, i.e.,
```{exercise, name="Characteristic Function of the Sample Mean"}
371
+
::: {.exercise name="Characteristic Function of the Sample Mean"}
385
372
Let $\bar{X}=\sum_{i=1}^n \frac{1}{n} X_i$ be the sample mean of $n$ independent and identically distributed random variables, each with characteristic function $\varphi _{X}$. Compute the characteristic function of $\bar{X}$.
for complex values of $s$ and which converges when the real part of $s$ 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$.
385
+
:::
386
+
387
+
#### A note on the old syntax {#theorem-engine}
388
+
389
+
For older versions of **bookdown** (before v0.21), a `theorem` environment could be written like this:
For a right triangle, if $c$ denotes the length of the hypotenuse
394
+
and $a$ and $b$ denote the lengths of the other two sides, we have
395
+
396
+
$$a^2 + b^2 = c^2$$
397
+
```
398
+
````
399
+
400
+
This syntax still works, but we do not recommend it since the new syntax allows you to write richer content and has a cleaner implementation. However, note that the old syntax has to be used if you want the environment to work with output formats in addition to HTML and PDF, such as EPUB. The fenced `Div` syntax only works for HTML and PDF output at the moment, and we will try to improve it in the future.
401
+
402
+
This conversion between the two syntaxes is straightforward. The above theorem could be rewritten in this way:
403
+
404
+
````markdown
405
+
::: {.theorem #pyth name="Pythagorean theorem"}
406
+
For a right triangle, if $c$ denotes the length of the hypotenuse
407
+
and $a$ and $b$ denote the lengths of the other two sides, we have
408
+
409
+
$$a^2 + b^2 = c^2$$
410
+
:::
411
+
````
412
+
413
+
You can use the helper function `bookdown::fence_theorems()` to convert a whole file or a piece of text. This is a one-time operation. We have tried to do the conversion from old to new syntax safely, but we might have missed some edge cases. To make sure you do not overwrite the `input` file by accident, you can write the converted source to a new file, e.g.,
Then double check the content of `01-intro-new.Rmd`. Using `output = NULL` will print the result of conversion in the R console, and is another way to check the conversion. If you are using a control version tool, you can set `output` to be the same as `input`, as it should be safe and easy for you to revert the change if anything goes wrong.
420
+
400
421
### Special headers
401
422
402
423
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\index{part}. 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.,
0 commit comments