-
Notifications
You must be signed in to change notification settings - Fork 0
/
90-bookdown.Rmd
207 lines (114 loc) · 7.73 KB
/
90-bookdown.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Bookdown {#bookdown}
## About
This is a _sample_ book written in **Markdown**. You can use anything that Pandoc's Markdown supports; for example, a math equation $a^2 + b^2 = c^2$.
### Usage
Each **bookdown** chapter is an .Rmd file, and each .Rmd file can contain one (and only one) chapter. A chapter *must* start with a first-level heading: `# A good chapter`, and can contain one (and only one) first-level heading.
Use second-level and higher headings within chapters like: `## A short section` or `### An even shorter section`.
The `index.Rmd` file is required, and is also your first book chapter. It will be the homepage when you render the book.
### Render book
You can render the HTML version of this example book without changing anything:
1. Find the **Build** pane in the RStudio IDE, and
1. Click on **Build Book**, then select your output format, or select "All formats" if you'd like to use multiple formats from the same book source files.
Or build the book from the R console:
```{r, eval=FALSE}
bookdown::render_book()
```
To render this example to PDF as a `bookdown::pdf_book`, you'll need to install XeLaTeX. You are recommended to install TinyTeX (which includes XeLaTeX): <https://yihui.org/tinytex/>.
### Preview book
As you work, you may start a local server to live preview this HTML book. This preview will update as you edit the book when you save individual .Rmd files. You can start the server in a work session by using the RStudio add-in "Preview book", or from the R console:
```{r eval=FALSE}
bookdown::serve_book()
```
```{r include=FALSE}
# automatically create a bib database for R packages
knitr::write_bib(c(
.packages(), 'bookdown', 'knitr', 'rmarkdown'
), 'packages.bib')
```
## Hello bookdown
All chapters start with a first-level heading followed by your chapter title, like the line above. There should be only one first-level heading (`#`) per .Rmd file.
### A section
All chapter sections start with a second-level (`##`) or higher heading followed by your section title, like the sections above and below here. You can have as many as you want within a chapter.
#### An unnumbered section {-}
Chapters and sections are numbered by default. To un-number a heading, add a `{.unnumbered}` or the shorter `{-}` at the end of the heading, like in this section.
## Cross-references {#cross}
Cross-references make it easier for your readers to find and link to elements in your book.
### Chapters and sub-chapters
There are two steps to cross-reference any heading:
1. Label the heading: `# Hello world {#nice-label}`.
- Leave the label off if you like the automated heading generated based on your heading title: for example, `# Hello world` = `# Hello world {#hello-world}`.
- To label an un-numbered heading, use: `# Hello world {-#nice-label}` or `{# Hello world .unnumbered}`.
1. Next, reference the labeled heading anywhere in the text using `\@ref(nice-label)`; for example, please see Chapter \@ref(cross).
- If you prefer text as the link instead of a numbered reference use: [any text you want can go here](#cross).
### Captioned figures and tables
Figures and tables *with captions* can also be cross-referenced from elsewhere in your book using `\@ref(fig:chunk-label)` and `\@ref(tab:chunk-label)`, respectively.
See Figure \@ref(fig:nice-fig).
```{r nice-fig, fig.cap='Here is a nice figure!', out.width='80%', fig.asp=.75, fig.align='center', fig.alt='Plot with connected points showing that vapor pressure of mercury increases exponentially as temperature increases.'}
par(mar = c(4, 4, .1, .1))
plot(pressure, type = 'b', pch = 19)
```
Don't miss Table \@ref(tab:nice-tab).
```{r nice-tab, tidy=FALSE}
knitr::kable(
head(pressure, 10), caption = 'Here is a nice table!',
booktabs = TRUE
)
```
## Parts
You can add parts to organize one or more book chapters together. Parts can be inserted at the top of an .Rmd file, before the first-level chapter heading in that same file.
Add a numbered part: `# (PART) Act one {-}` (followed by `# A chapter`)
Add an unnumbered part: `# (PART\*) Act one {-}` (followed by `# A chapter`)
Add an appendix as a special kind of un-numbered part: `# (APPENDIX) Other stuff {-}` (followed by `# A chapter`). Chapters in an appendix are prepended with letters instead of numbers.
## Footnotes and citations
### Footnotes
Footnotes are put inside the square brackets after a caret `^[]`. Like this one ^[This is a footnote.].
### Citations
Reference items in your bibliography file(s) using `@key`.
For example, we are using the **bookdown** package [@R-bookdown] (check out the last code chunk in index.Rmd to see how this citation key was added) in this sample book, which was built on top of R Markdown and **knitr** [@xie2015] (this citation was added manually in an external file book.bib).
Note that the `.bib` files need to be listed in the index.Rmd with the YAML `bibliography` key.
The `bs4_book` theme makes footnotes appear inline when you click on them. In this example book, we added `csl: chicago-fullnote-bibliography.csl` to the `index.Rmd` YAML, and include the `.csl` file. To download a new style, we recommend: https://www.zotero.org/styles/
The RStudio Visual Markdown Editor can also make it easier to insert citations: <https://rstudio.github.io/visual-markdown-editing/#/citations>
## Blocks
### Equations
Here is an equation.
\begin{equation}
f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
(\#eq:binom)
\end{equation}
You may refer to using `\@ref(eq:binom)`, like see Equation \@ref(eq:binom).
### Theorems and proofs
Labeled theorems can be referenced in text using `\@ref(thm:tri)`, for example, check out this smart theorem \@ref(thm:tri).
::: {.theorem #tri}
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$$
:::
Read more here <https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html>.
### Callout blocks
The `bs4_book` theme also includes special callout blocks, like this `.rmdnote`.
::: {.rmdnote}
You can use **markdown** inside a block.
```{r collapse=TRUE}
head(beaver1, n = 5)
```
:::
It is up to the user to define the appearance of these blocks for LaTeX output.
You may also use: `.rmdcaution`, `.rmdimportant`, `.rmdtip`, or `.rmdwarning` as the block name.
The R Markdown Cookbook provides more help on how to use custom blocks to design your own callouts: https://bookdown.org/yihui/rmarkdown-cookbook/custom-blocks.html
## Sharing your book
### Publishing
HTML books can be published online, see: https://bookdown.org/yihui/bookdown/publishing.html
### 404 pages
By default, users will be directed to a 404 page if they try to access a webpage that cannot be found. If you'd like to customize your 404 page instead of using the default, you may add either a `_404.Rmd` or `_404.md` file to your project root and use code and/or Markdown syntax.
### Metadata for sharing
Bookdown HTML books will provide HTML metadata for social sharing on platforms like Twitter, Facebook, and LinkedIn, using information you provide in the `index.Rmd` YAML. To setup, set the `url` for your book and the path to your `cover-image` file. Your book's `title` and `description` are also used.
This `bs4_book` provides enhanced metadata for social sharing, so that each chapter shared will have a unique description, auto-generated based on the content.
Specify your book's source repository on GitHub as the `repo` in the `_output.yml` file, which allows users to view each chapter's source file or suggest an edit. Read more about the features of this output format here:
https://pkgs.rstudio.com/bookdown/reference/bs4_book.html
Or use:
```{r eval=FALSE}
?bookdown::bs4_book
```
`r if (knitr::is_html_output()) '
## References {-}
'`