Chapter 4 Guide

4.1 Inline formatting

work or work

yes or yes

H2O

Ca2+

code

Small Caps

RStudio

Footnote1

If you do not want a certain heading to be numbered, you can add {-} after the heading, e.g # Preface {-}

Blockquotes are written after >, e.g.,

“Love the life you live,

Live the life you love.” — Bob Marley

Code blocks

4.2 Listings

Unordered list items start with *, -, or +, and you can nest one list within another list by indenting the sub-list by four spaces, e.g.,

  • one item
  • one item
  • one item
    • one item
    • one item

Ordered list items start with numbers (the rule for nested lists is the same as above), e.g.,

  1. the first item
  2. the second item
  3. the third item

4.4 Images

  • Chèn theo syntax trong markdown Love icon

  • Chèn từ link sử dụng thẻ HTML <img src="link.png" alt="caption" style="height: 100px; width:100px;"/>

Xem thêm https://www.markdownguide.org/hacks/#image-size

4.5 Math expressions

Ma trận dãy số

\[\begin{array}{ccc} x_{11} & x_{12} & x_{13}\\ x_{21} & x_{22} & x_{23} \end{array}\]

Ma trận

\[X = \begin{bmatrix}1 & x_{1}\\ 1 & x_{2}\\ 1 & x_{3} \end{bmatrix}\]

\[\Theta = \begin{pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]

\[\begin{vmatrix}a & b\\ c & d \end{vmatrix}=ad-bc\]

4.6 Number and reference equations

\[\begin{equation} f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k} \tag{4.1} \end{equation}\]

You may refer to it using \@ref(eq:binom), e.g., see Equation (4.1).

Công thức mô tả a normal frequency curve

\[\begin{equation} f = \frac{N}{{(\sigma \sqrt {2\pi } )}}{e^{ - {{(y - \mu )}^2}/2{\sigma ^2}}} \tag{4.2} \end{equation}\]

You may refer to it using \@ref(eq:normal), e.g., see Equation (4.2).

Nếu không đánh số công thức thì dùng equation*

\[\begin{equation*} \frac{d}{dx}\left( \int_{a}^{x} f(u)\,du\right)=f(x) \end{equation*}\]

Nếu ghi theo kiểu align thì áp dụng

\[\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 ] \tag{4.3} \end{align}\]

Sử dụng \notag để giúp không đánh số công thức ở hàng đầu tiên của (4.3)

Nếu sử dụng split thì áp dụng

\[\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.4} \end{equation}\]

4.7 Theorems and proofs

Theorem 4.1 (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\]

4.8 Text references

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.

4.9 R code

There are two types of R code in R Markdown/knitr documents: R code chunks, and inline R code. The syntax for the latter is `r R_CODE`, and it can be embedded inline with other document elements.

4.10 Figures

If we assign a figure caption to a code chunk via the chunk option fig.cap, R plots will be put into figure environments, which will be automatically labeled and numbered, and can also be cross-referenced. The label of a figure environment is generated from the label of the code chunk.

Cross reference cho Figure 4.1

par(mar = c(4, 4, .1, .1))
plot(pressure, pch = 19, type = 'b')
A figure example with the specified aspect ratio, width, and alignment.

Figure 4.1: A figure example with the specified aspect ratio, width, and alignment.

Chèn hai đồ thị 4.2

par(mar = c(4, 4, .1, .1))
plot(pressure, pch = 19, type = 'b')
plot(cars, pch = 19)
Two plots placed side by side.Two plots placed side by side.

Figure 4.2: Two plots placed side by side.

Chèn hình ảnh

knitr::include_graphics('images/michael.jpg')
Never let anyone know your mind.

Figure 4.3: Never let anyone know your mind.

Chèn lặp lại hình ảnh

knitr::include_graphics(rep('images/turtle.jpg', 3))
Finding good land.Finding good land.Finding good land.

Figure 4.4: Finding good land.

4.11 Tables

Like figures, tables with captions will also be numbered and can be referenced.

Table 4.1 is a simple example.

knitr::kable(
  head(mtcars[, 1:8], 10), booktabs = TRUE,
  caption = 'A table of the first 10 rows of the mtcars data.'
)
Table 4.1: A table of the first 10 rows of the mtcars data.
mpg cyl disp hp drat wt qsec vs
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1

If you want to put multiple tables in a single table environment, wrap the data objects (usually data frames in R) into a list. See Table 4.2 for an example. Please note that this feature is only available in HTML and PDF output.

knitr::kable(
  list(
    head(iris[, 1:2], 3),
    head(mtcars[, 1:3], 5)
  ),
  caption = 'A Tale of Two Tables.', booktabs = TRUE
)
Table 4.2: A Tale of Two Tables.
Sepal.Length Sepal.Width
5.1 3.5
4.9 3.0
4.7 3.2
mpg cyl disp
Mazda RX4 21.0 6 160
Mazda RX4 Wag 21.0 6 160
Datsun 710 22.8 4 108
Hornet 4 Drive 21.4 6 258
Hornet Sportabout 18.7 8 360

Longtable. Nếu dùng trong HTML thì áp dụng đoạn code. Còn nếu dùng trong PDF thì add \usepackage{longtable} vào phần LaTeX preamble.

knitr::kable(
  iris[1:55, ], longtable = TRUE, booktabs = TRUE,
  caption = 'A table generated by the longtable package.'
)
Table 4.3: A table generated by the longtable package.
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
4.6 3.4 1.4 0.3 setosa
5.0 3.4 1.5 0.2 setosa
4.4 2.9 1.4 0.2 setosa
4.9 3.1 1.5 0.1 setosa
5.4 3.7 1.5 0.2 setosa
4.8 3.4 1.6 0.2 setosa
4.8 3.0 1.4 0.1 setosa
4.3 3.0 1.1 0.1 setosa
5.8 4.0 1.2 0.2 setosa
5.7 4.4 1.5 0.4 setosa
5.4 3.9 1.3 0.4 setosa
5.1 3.5 1.4 0.3 setosa
5.7 3.8 1.7 0.3 setosa
5.1 3.8 1.5 0.3 setosa
5.4 3.4 1.7 0.2 setosa
5.1 3.7 1.5 0.4 setosa
4.6 3.6 1.0 0.2 setosa
5.1 3.3 1.7 0.5 setosa
4.8 3.4 1.9 0.2 setosa
5.0 3.0 1.6 0.2 setosa
5.0 3.4 1.6 0.4 setosa
5.2 3.5 1.5 0.2 setosa
5.2 3.4 1.4 0.2 setosa
4.7 3.2 1.6 0.2 setosa
4.8 3.1 1.6 0.2 setosa
5.4 3.4 1.5 0.4 setosa
5.2 4.1 1.5 0.1 setosa
5.5 4.2 1.4 0.2 setosa
4.9 3.1 1.5 0.2 setosa
5.0 3.2 1.2 0.2 setosa
5.5 3.5 1.3 0.2 setosa
4.9 3.6 1.4 0.1 setosa
4.4 3.0 1.3 0.2 setosa
5.1 3.4 1.5 0.2 setosa
5.0 3.5 1.3 0.3 setosa
4.5 2.3 1.3 0.3 setosa
4.4 3.2 1.3 0.2 setosa
5.0 3.5 1.6 0.6 setosa
5.1 3.8 1.9 0.4 setosa
4.8 3.0 1.4 0.3 setosa
5.1 3.8 1.6 0.2 setosa
4.6 3.2 1.4 0.2 setosa
5.3 3.7 1.5 0.2 setosa
5.0 3.3 1.4 0.2 setosa
7.0 3.2 4.7 1.4 versicolor
6.4 3.2 4.5 1.5 versicolor
6.9 3.1 4.9 1.5 versicolor
5.5 2.3 4.0 1.3 versicolor
6.5 2.8 4.6 1.5 versicolor

Tạo table thủ công. Table 4.4

Table 4.4: A simple table in Markdown.
Sepal.Length Sepal.Width Petal.Length Petal.Width
5.1 3.5 1.4 0.2
4.9 3.0 1.4 0.2
4.7 3.2 1.3 0.2
4.6 3.1 1.5 0.2
5.0 3.6 1.4 0.2
5.4 3.9 1.7 0.4

  1. footnote↩︎