第 4 章 語法 Cheatsheet
下文許多內容直接自 Bookdown 擷取。想完整了解者,請閱讀 Bookdown 中的內容。
4.1 章節連結
4.1.1 錨點定義
在內文中,可以使用特殊語法建立前往其它章節的連結,但首先每個章節需要有錨點。若章節標題由英數組成,例如 ## Experimental Design
, 則錨點會自動被定義成 experimental-design
。若標題含有中文,例如 ## 實驗設計
則需自行定義錨點:## 實驗設計 {#exp-design}
。
4.1.2 內文連結
要在內文建立連至其它章節的連結,需使用 \@ref(anchor)
的語法,例如,
- 連結至
## Experimental Design
\@ref(experimental-design)
- 連結至
## 實驗設計 {#exp-design}
\@ref(exp-design)
4.2 數學
4.2.1 Unnumbered Equations
\begin{equation*}
\frac{d}{dx}\left( \int_{a}^{x} f(u)\,du\right)=f(x)
\end{equation*}
\[\begin{equation*} \frac{d}{dx}\left( \int_{a}^{x} f(u)\,du\right)=f(x) \end{equation*}\]
4.2.2 Numbered Equations
\begin{equation}
f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
(\#eq:bino)
\end{equation}
\@ref(eq:bino) 式
\[\begin{equation} f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k} \tag{4.1} \end{equation}\]
式 (4.1)
4.2.3 Multi-line Aligned Equations
\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}
詳見公式 \@ref(eq:var-beta)
\[\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} \tag{4.2} \end{equation}\]
詳見公式 (4.2)
4.2.4 定理與證明
```{theorem, thm-label, 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$$
```
詳見定理 \@ref(thm:thm-label)
詳見定理 4.1
4.2.5 環境
除了 theorem
之外,還可以使用其它 bookdown 提供的環境。例如,將 code chunk 中 theorem
換成 proof
, remark
, solution
1。
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 |
4.3 Figure Referencing
```{r iris, fig.cap= "The iris data."}
library(ggplot2)
pl_iris <- ggplot(data = iris) +
geom_point(aes(x = Sepal.Length,
y = Sepal.Width,
color = Species)
)
pl_iris
```
見圖 \@ref(fig:iris)
產生:見圖 4.1
4.4 Table Referencing
`r Sys.time()`.
(ref:mtcars) Dynamic variable in caption:
```{r mtcars, echo=FALSE}
knitr::kable(
head(mtcars[, 1:8], 5), booktabs = TRUE,
caption = '(ref:mtcars)'
)
```
### Link Table in Later Sections {-}
見表 \@ref(tab:mtcars)
mpg | cyl | disp | hp | drat | wt | qsec | vs | |
---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 |
Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 |
Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 |
Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 |
Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 |
Link Table in Later Sections
見表 4.2
4.5 總整理
\@ref(section-anchor)
(ref:mtcar) Text refereces
\@ref(fig:fig-label)
fig-label
is code chunk label
\@ref(thm:thm-label)
thm-label
is code chunk label
\@ref(tab:tab-label)
tab-label
is code chunk label. Use text reference for complicated captions.
\@ref(eq:eq-label)
- Defined as
(\#eq:eq-label)
in LaTeX equation environment
- Defined as
參考資料
Kassin, S., Fein, S., & Markus, H. R. (2017). Social Psychology (10th ed.). Boston: Cengage Learning.
theorem
、proof
、remark
這三者無法被 cross-reference。↩︎