Skip to content

Commit 42a240a

Browse files
authored
Document new custom environment syntax (#1173)
1 parent f02e6c5 commit 42a240a

File tree

1 file changed

+74
-53
lines changed

1 file changed

+74
-53
lines changed

inst/examples/02-components.Rmd

Lines changed: 74 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,14 @@ Theorems\index{theorem} and proofs are commonly used in articles and books in ma
230230
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:
231231

232232
````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+
:::
236237
````
237238

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+
238241
(ref:theorem-envs) Theorem environments in **bookdown**.
239242

240243
```{r theorem-envs, echo=FALSE}
@@ -246,55 +249,47 @@ knitr::kable(data.frame(
246249
), caption = '(ref:theorem-envs)', booktabs = TRUE)
247250
```
248251

249-
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}`.
250253

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,
252255

253256
````markdown
254-
`r ''````{theorem, name="Pythagorean theorem"}
257+
::: {.theorem name="Pythagorean theorem"}
255258
For a right triangle, if $c$ denotes the length of the hypotenuse
256259
and $a$ and $b$ denote the lengths of the other two sides, we have
257260
$$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+
:::
267262
````
268263

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,
270265

271266
````markdown
272-
`r ''````{theorem, bar}
267+
::: {.theorem #foo}
273268
A labeled theorem here.
274-
```
269+
:::
275270
````
276271

277272
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):
278273

279274
````markdown
280-
`r ''````{theorem, pyth, name="Pythagorean theorem"}
275+
::: {.theorem #pyth name="Pythagorean theorem"}
281276
For a right triangle, if $c$ denotes the length of the hypotenuse
282277
and $a$ and $b$ denote the lengths of the other two sides, we have
283278

284279
$$a^2 + b^2 = c^2$$
285-
```
280+
:::
286281
````
287282

288-
```{theorem, pyth, name="Pythagorean theorem"}
283+
::: {.theorem #pyth name="Pythagorean theorem"}
289284
For a right triangle, if $c$ denotes the length of the hypotenuse
290285
and $a$ and $b$ denote the lengths of the other two sides, we have
291286

292287
$$a^2 + b^2 = c^2$$
293-
```
288+
:::
294289

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.
296291

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.
298293

299294
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.,
300295

@@ -305,24 +300,16 @@ Theorems are numbered by chapters by default. If there are no chapters in your d
305300

306301
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.
307302

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-
316303
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**.
317304

318-
```{definition}
305+
::: {.definition}
319306
The characteristic function of a random variable $X$ is defined by
320307

321308
$$\varphi _{X}(t)=\operatorname {E} \left[e^{itX}\right], \; t\in\mathcal{R}$$
322-
```
309+
:::
323310

324311

325-
```{example}
312+
::: {.example}
326313
We derive the characteristic function of $X\sim U(0,1)$ with the probability density function $f(x)=\mathbf{1}_{x \in [0,1]}$.
327314

328315
\begin{equation*}
@@ -339,25 +326,25 @@ We derive the characteristic function of $X\sim U(0,1)$ with the probability den
339326
\end{equation*}
340327

341328
Note that we used the fact $e^{ix}=\cos(x)+i\sin(x)$ twice.
342-
```
329+
:::
343330

344-
```{lemma, chf-pdf}
331+
::: {.lemma #chf-pdf}
345332
For any two random variables $X_1$, $X_2$, they both have the same probability distribution if and only if
346333

347334
$$\varphi _{X_1}(t)=\varphi _{X_2}(t)$$
348-
```
335+
:::
349336

350-
```{theorem, chf-sum}
337+
::: {.theorem #chf-sum}
351338
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
352339

353340
$$\varphi _{S_{n}}(t)=\prod_{i=1}^n\varphi _{X_i}(a_{i}t)=\varphi _{X_{1}}(a_{1}t)\cdots \varphi _{X_{n}}(a_{n}t)$$
354-
```
341+
:::
355342

356-
```{proposition}
343+
::: {.proposition}
357344
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+
:::
359346

360-
```{proof}
347+
::: {.proof}
361348
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
362349

363350
\begin{equation*}
@@ -369,34 +356,68 @@ The characteristic function of $X\sim\mathrm{Pois}(\lambda)$ is $\varphi _{X}(t)
369356
\end{equation*}
370357

371358
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+
:::
373360

374-
```{remark}
361+
::: {.remark}
375362
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+
:::
377364

378-
```{corollary}
365+
::: {.corollary}
379366
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.,
380367

381368
$$\varphi _{X_1+X_2}(t)=\varphi _{X_1}(t) \varphi _{X_2}(t)$$
382-
```
369+
:::
383370

384-
```{exercise, name="Characteristic Function of the Sample Mean"}
371+
::: {.exercise name="Characteristic Function of the Sample Mean"}
385372
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}$.
386-
```
373+
:::
387374

388-
```{solution}
375+
::: {.solution}
389376
Applying Theorem \@ref(thm:chf-sum), we have
390377

391378
$$\varphi _{\bar{X}}(t)=\prod_{i=1}^n \varphi _{X_i}\left(\frac{t}{n}\right)=\left[\varphi _{X}\left(\frac{t}{n}\right)\right]^n.$$
392-
```
379+
:::
393380

394-
```{hypothesis, name="Riemann hypothesis"}
381+
::: {.hypothesis name="Riemann hypothesis"}
395382
The Riemann Zeta-function is defined as
396383
$$\zeta(s) = \sum_{n=1}^{\infty} \frac{1}{n^s}$$
397384
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:
390+
391+
````markdown
392+
`r ''````{theorem pyth, name="Pythagorean theorem"}
393+
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.,
414+
415+
```r
416+
bookdown::fence_theorems("01-intro.Rmd", output = "01-intro-new.Rmd")
398417
```
399418

419+
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+
400421
### Special headers
401422

402423
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy