1 Markdown highlighting

1.1 Bold and Italic

Formatting Code
bold **bold**
bold __bold__
italic *italic*
italic _italic_

1.2 Text coloring

To add color you can use CSS or R code like this:

color_text <- function(x, color){
  if(knitr::is_latex_output())
    paste("\\textcolor{",color,"}{",x,"}",sep="")
  else if(knitr::is_html_output())
    paste("<font color='",color,"'>",x,"</font>",sep="")
  else
    x
}

red <- function(x){
  if(knitr::is_latex_output())
    paste("\\textcolor{",'red',"}{",x,"}",sep="")
  else if(knitr::is_html_output())
    paste("<font color='red'>",x,"</font>",sep="")
  else
    x
}

The string This is colored with the red function comes from:

`r red("This is colored with the red function")`

The string This is colored with the color_text function set to mauve comes from:

`r color_text("This is colored with the color_text function set to mauve", "#E0B0FF")`

1.3 Formatting Text

Appearance Code
Three xfun::n2w(3, cap = TRUE)
14.5% scales::percent(0.1447, accuracy= .1)
p=0.145 scales::pvalue(0.1447, accuracy= .001, add_p =TRUE)

1.4 Text Font

To tweak the appearance of words use these formats:

Formatting Looks like Code
plain text \(\text{text Pr}\) \text{text Pr}
bold Greek symbol \(\boldsymbol{\epsilon}\) \boldsymbol{\epsilon}
typewriter \(x\ \tt{sentence}\ x\) \tt{sentence}
teletype \(x\ \texttt{blah}\ x\) \texttt{blah}
slide font \(\sf{blah}\) \sf{blah}
bold \(\mathbf{x}\) \mathbf{x}
plain \(\mathrm{text Pr}\) \mathrm{text Pr}
cursive \(\mathcal{S}\) \mathcal{S}
Blackboard bold \(\mathbb{R}\) \mathbb{R}

2 References

2.1 Section references

Section 99.4 comes from Section [99.4](#x99.4)

2.2 Footnotes

1 comes from ^[A footnote]

2.3 Figures

See Figure 2.1.

The cars data.

Figure 2.1: The cars data.

Name a figure chunk with a name like cars-plot and include fig.cap = "The cars data." then reference it like this: \@ref(fig:cars-plot)

3 Displaying Formula

3.1 Symbols

Symbols Code
\(\stackrel{\text{def}}{=}\) \stackrel{\text{def}}{=}
\(\nabla\) \nabla
\(\partial\) \partial
\(\vert\) or use \(\lvert a \rvert\) \vert \text{ or use } \lvert a \rvert \
\(\Vert\) \Vert
\(\mid\) in set notation; \given is missing \mid

3.2 Brackets and Parentheses

Looks like Code
\(\big( \Big( \bigg( \Bigg(\) \big( \Big( \bigg( \Bigg(
\(\big] \Big] \bigg] \Bigg]\) \big] \Big] \bigg] \Bigg]

3.3 Notation

Based on: https://www.calvin.edu/~rpruim/courses/s341/S17/from-class/MathinRmd.html

Math Code
\(x = y\) $x = y$
\(x \approx y\) $x \approx y$
\(f_k(X) \equiv Pr(X|Y = k)\) f_k(X) \equiv Pr(X|Y = k)
\(x \lt y\) $x < y$
\(x \gt y\) $x > y$
\(x \le y\) $x \le y$
\(x \ge y\) $x \ge y$
\(x \times y\) $x \times y$
\(x^{n}\) $x^{n}$
\(x_{n}\) $x_{n}$
\(x_1, x_2, \dots, x_n\) $x_1, x_2, \dots, x_n$
\(x_1 + x_2 + \cdots + x_n\) $x_1 + x_2 + \cdots + x_n$
\(\overline{x}\) $\overline{x}$
\(\hat{x}\) $\hat{x}$
\(\widehat{SE}\) $\widehat{SE}$
\(\tilde{x}\) $\tilde{x}$
\(\frac{a}{b}\) $\frac{a}{b}$
\(\displaystyle \frac{a}{b}\) $\displaystyle \frac{a}{b}$
\(\binom{n}{k}\) $\binom{n}{k}$
\(x_{1} + x_{2} + \cdots + x_{n}\) $x_{1} + x_{2} + \cdots + x_{n}$
\(x_{1}, x_{2}, \dots, x_{n}\) $x_{1}, x_{2}, \dots, x_{n}$
\(\mathbf{x} = \langle x_{1}, x_{2}, \dots, x_{n}\rangle\) $\mathbf{x} = \langle x_{1}, x_{2}, \dots, x_{n}\rangle$
\(x \in A\) $x \in A$
\(\lvert A \rvert\) $\lvert A \rvert$
\(\big \langle x_i, x_{i'}\big \rangle\) \big \langle x_i, x_{i'}\big \rangle
\(x \in A\) $x \in A$
\(x \subset B\) $x \subset B$
\(x \subseteq B\) $x \subseteq B$
\(A \cup B\) $A \cup B$
\(A \cap B\) $A \cap B$
\(X \sim {\sf Binom}(n, \pi)\) X \sim {\sf Binom}(n, \pi)$
\(\mathrm{P}(X \le x) = {\tt pbinom}(x, n, \pi)\) $\mathrm{P}(X \le x) = {\tt pbinom}(x, n, \pi)$
\(P(A \mid B)\) $P(A \mid B)$
\(\mathrm{P}(A \mid B)\) $\mathrm{P}(A \mid B)$
\(\{1, 2, 3\}\) $\{1, 2, 3\}$
\(\sin(x)\) $\sin(x)$
\(\log(x)\) $\log(x)$
\(\exp(x)\) $\exp(x)$
\(\exp{\big(\sum_{i=1}^p x_i\big)}\) $\exp{\big(\sum_{i=1}^p x_i\big)}$
\(\int_{a}^{b}\) $\int_{a}^{b}$
\(\left(\int_{a}^{b} f(x) \; dx\right)\) $\left(\int_{a}^{b} f(x) \; dx\right)$
\(\left[\int_{-\infty}^{\infty} f(x) \; dx\right]\) $\left[\int_{\-infty}^{\infty} f(x) \; dx\right]$
\(\left. F(x) \right|_{a}^{b}\) $\left. F(x) \right|_{a}^{b}$
\(\sum_{x = a}^{b} f(x)\) $\sum_{x = a}^{b} f(x)$
\(\prod_{x = a}^{b} f(x)\) $\prod_{x = a}^{b} f(x)$
\(\lim_{x \to \infty} f(x)\) $\lim_{x \to \infty} f(x)$
\(\displaystyle \lim_{x \to \infty} f(x)\) $\displaystyle \lim_{x \to \infty} f(x)$
\(RMSE = \sqrt{\frac{1}{n}\sum_{i=1}^{n} (Y_n - \hat{Y}_i)^2}\) $RMSE = \sqrt{\frac{1}{n}\sum_{i=1}^{n} (Y_n - \hat{Y}_i)^2}$
\((\texttt{pop} - \overline{ \texttt{pop}})\) $(\texttt{pop} - \overline{\texttt{pop}})$
\(\hat{f}(x) \leftarrow \hat{f}(x) + \lambda\hat{f}^b(x)\) \hat{f}(x) \leftarrow \hat{f}(x) + \lambda\hat{f}^b(x)

3.4 Greek letters

Based on: https://www.calvin.edu/~rpruim/courses/s341/S17/from-class/MathinRmd.html

letters code
\(\alpha A\) $\alpha A$
\(\beta B\) $\beta B$
\(\gamma \Gamma\) $\gamma \Gamma$
\(\delta \Delta\) $\delta \Delta$
\(\epsilon \varepsilon E\) $\epsilon \varepsilon E$
\(\zeta Z \sigma\) $\zeta Z \sigma
\(\eta H\) $\eta H$
\(\theta \vartheta \Theta\) $\theta \vartheta \Theta$
\(\iota I\) $\iota I$
\(\kappa K\) $\kappa K$
\(\lambda \Lambda\) $\lambda \Lambda$
\(\mu M\) $\mu M$
\(\nu N\) $\nu N$
\(\xi\Xi\) $\xi\Xi$
\(o O\) $o O$ (omicron)
\(\pi \Pi\) $\pi \Pi$
\(\rho\varrho P\) $\rho\varrho P$
\(\sigma \Sigma\) \sigma \Sigma$
\(\tau T\) $\tau T$
\(\upsilon \Upsilon\) $\upsilon \Upsilon$
\(\phi \varphi \Phi\) $\phi \varphi \Phi$
\(\chi X\) $\chi X$
\(\psi \Psi\) $\psi \Psi$
\(\omega \Omega\) $\omega \Omega$

3.5 Calligraphic Letters

Thank you Layla: https://gist.github.com/labouz/d2e012f638864b9af061afe648ce441b

Letters Code Meaning
\(\mathcal{B}\) $\mathcal{B}$ Basis
\(\mathcal{O}\) $\mathcal{O}$ Used for Big-O notation
\(\mathcal{P}\) $\mathcal{P}$ Power set, probability function
\(\mathcal{S}\) $\mathcal{S}$ A set
\((\Omega, \mathcal{F}, \mathcal{P})\) $(\Omega, \mathcal{F}, \mathcal{P})$ Probability space/triple

3.5.1 Stacked text (for finding maximum):

To get text stacked below a word for optimization formulas use \mathop with $$

Math Code
\[\mathop{\text{max}}_{k}(\hat{p}_{mk})\] $$\mathop{\text{max}}_{k}(\hat{p}_{mk})$$

3.5.2 Adjust position of limits:

To have limits appear next to sigma or pi (not above):

Math Code
\(\sum\nolimits_{i=1}^{n}X_i.\) \sum\nolimits_{i=1}^{n}X_i.

3.6 Matrices

\[\begin{equation} A = \begin{bmatrix} 1 & \cdots & 3\\ \vdots & \ddots & \vdots\\ 7 & \cdots & 9 \end{bmatrix} \end{equation}\]

Comes from this code:

\begin{equation}
A = 
  \begin{bmatrix}
    1 & \cdots & 3\\
    \vdots & \ddots & \vdots\\
    7 & \cdots & 9
  \end{bmatrix}
\end{equation}

Options to surround the matrix:

Type Code
Nothing \begin{matrix}
Parentheses \begin{pmatrix}
Square Brackets \begin{bmatrix}
Curly brackets \begin{Bmatrix}
Pipes \begin{vmatrix}
Double Pipes \begin{Vmatrix}

3.7 Equations

These are formulas that appear with an equation number.

3.7.1 Basic Equation

The names of equations can not include . or _ but it can include -

\begin{equation} 
  1 + 1 = 2 
  (\#eq:eq99-1)
\end{equation} 

Which appears as:

\[\begin{equation} 1 + 1 = 2 \tag{3.1} \end{equation}\]

The reference to the equation is (3.1) which comes from this code \@ref(eq:eq99-1)

3.7.2 Case-When Equation (Large Curly Brace)

Based on: https://tex.stackexchange.com/questions/9065/large-braces-for-specifying-values-of-variables-by-condition

Case when formula:

\[\begin{equation} y = \begin{cases} 0, & \text{if}\ a=1 \\ 1, & \text{otherwise} \end{cases} \tag{3.2} \end{equation} \]

Which comes from this code:

\begin{equation} 
  y =
  \begin{cases}
    0, & \text{if}\ a=1 \\
    1, & \text{otherwise}
  \end{cases}
  (\#eq:eq99-2)
\end{equation} 

The reference to equation is (3.2) which comes from this code \@ref(eq:eq99-2)

3.7.3 Alligned with Underbars

\[\begin{equation} \begin{aligned} \mathrm{E}(Y-\hat{Y})^2 & = \mathrm{E}[f(X) + \epsilon -\hat{f}(X)]^2 \\ & = \underbrace{[f(X) -\hat{f}(X)]^2}_{\mathrm{Reducible}} + \underbrace{\mathrm{var}(\epsilon)}_{\mathrm{Irreducible}} \\ \end{aligned} \tag{3.3} \end{equation}\]

Comes from this code:

\begin{equation}
\begin{aligned}
\mathrm{E}(Y-\hat{Y})^2 & = \mathrm{E}[f(X) + \epsilon -\hat{f}(X)]^2 \\
                        & = \underbrace{[f(X) -\hat{f}(X)]^2}_{\mathrm{Reducible}} + \underbrace{\mathrm{var}(\epsilon)}_{\mathrm{Irreducible}} \\
\end{aligned}
(\#eq:eq99-3)
\end{equation}

3.8 Convert models to equations

Use equatiomatic package (Anderson, Heiss, and Sumners 2022) to show the equations corresponding to models fitted in R

fit <- lm(mpg ~ cyl + disp, mtcars)

Theoretical model:

equatiomatic::extract_eq(fit)

\[ \operatorname{mpg} = \alpha + \beta_{1}(\operatorname{cyl}) + \beta_{2}(\operatorname{disp}) + \epsilon \]

Actual coefficients

equatiomatic::extract_eq(fit, use_coefs = TRUE)

\[ \operatorname{\widehat{mpg}} = 34.66 - 1.59(\operatorname{cyl}) - 0.02(\operatorname{disp}) \]

Based on: https://bookdown.org/yihui/rmarkdown-cookbook/equatiomatic.html

4 Tham khảo

Nguồn: Ray’s Formatting Notes, version 2021-02-15 https://gist.github.com/RaymondBalise/ee4b7da0a70087317dc52bf479a4e2b6


  1. A footnote↩︎