Clement Lee
2023-12-08
Usual file structure
R
code & output in .tex\includegraphics
in .texR
-related files:
\(~\)
A single set of source scripts
This talk
The source file (.tex)
\begin{itemize}
\item $\pi=3.1415927\dots$ is \textit{irrational} \& \textbf{transcendental}
\item Type \texttt{pi} in \verb'R' console for numerical value
\end{itemize}
The output (.pdf)
pi
in R
console for numerical value\(\quad\)
## [1] 6.283185
\(\quad\)
Question: Show that \(e^{i\pi}+1=0\).
Solution: \[\begin{align*}
e^{i\pi}+1&=\cos\pi+i\sin\pi+1\qquad\qquad\qquad\qquad\\
&=-1+i\times0+1=0
\end{align*}\]
The source file (.Rnw)
\begin{itemize}
\item $\pi=\Sexpr{pi}\dots$ is \textit{irrational} \& \textbf{transcendental}
\item Type \texttt{pi} in \verb'R' console for numerical value
\end{itemize}
The output (.pdf)
pi
in R
console for numerical value\(\quad\)
## [1] 6.283185
\(\quad\)
Question: Show that \(e^{i\pi}+1=0\).
Solution: \[\begin{align*}
e^{i\pi}+1&=\cos\pi+i\sin\pi+1\qquad\qquad\qquad\qquad\\
&=-1+i\times0+1=0
\end{align*}\]
The source file (.Rmd)
* $\pi=`r pi`\dots$ is *irrational* & **transcendental**
* Type `pi` in `R` console for numerical value
The output (.pdf / .html)
pi
in R
console for numerical value\(\quad\)
## [1] 6.283185
\(\quad\)
Question: Show that \(e^{i\pi}+1=0\).
Solution: \[\begin{align*}
e^{i\pi}+1&=\cos\pi+i\sin\pi+1\qquad\qquad\qquad\qquad\\
&=-1+i\times0+1=0
\end{align*}\]
For displaying / hiding code / plots
The output (.pdf / .html)
Don’t evaluate
Also useful when showing code that doesn’t work
The output (.pdf / .html)
Hide the plots (but still evaluate)
The output (.pdf / .html)
Hide the code & results (but still evaluate)
The output (.pdf / .html)
Question version
The output (.pdf / .html)
Plot the cars
dataset in R
.
Find the numerical value of \(2\pi\), to 4 decimal places.
Solution version
The output (.pdf / .html)
cars
dataset in R
.## [1] 6.2832
Between the triple dashes: YAML similar to LaTeX preamble
---
title: "MAS9999 Practical 1"
params:
solution: ""
---
1. Plot the `cars` dataset in `R`.
```{r, out.width = "70%", include = params$solution}
plot(cars)
```
2. Find the numerical value of $2\pi$, to 4 decimal places.
```{r, include = params$solution}
round(2 * pi, 4)
```
Call this file Practical1.Rmd
\(~\)
Run the commands (interactively) in R
library(rmarkdown)
render("Practical1.Rmd", params = list(solution = FALSE),
output_format = "html_document",
output_file = "Practical1_questions.html")
render("Practical1.Rmd", params = list(solution = FALSE),
output_format = "pdf_document",
output_file = "Practical1_questions.pdf")
render("Practical1.Rmd", params = list(solution = TRUE),
output_format = "html_document",
output_file = "Practical1_solutions.html")
render("Practical1.Rmd", params = list(solution = TRUE),
output_format = "pdf_document",
output_file = "Practical1_solutions.pdf")
\(~\)
Originally
Question: Show that $e^{i\pi}+1=0$.
Solution:
\begin{align*}
e^{i\pi}+1&=\cos\pi+i\sin\pi+1\\
&=-1+i\times0+1=0
\end{align*}
No native solution environment AFAIK
The output (.pdf / .html)
Question: Show that \(e^{i\pi}+1=0\).
Solution: \[\begin{align*} e^{i\pi}+1&=\cos\pi+i\sin\pi+1\\ &=-1+i\times0+1=0 \end{align*}\]
Making solution text a string in R
Question: Show that $e^{i\pi}+1=0$.
```{r, results = "asis", echo = FALSE, include = TRUE}
cat("Solution:
\\begin{align*}
e^{i\\pi}+1&=\\cos\\pi+i\\sin\\pi+1\\\\
&=-1+i\\times0+1=0
\\end{align*}", "\n")
```
Double the backslashes
The output (.pdf / .html)
Question: Show that \(e^{i\pi}+1=0\).
Solution: \[\begin{align*} e^{i\pi}+1&=\cos\pi+i\sin\pi+1\\ &=-1+i\times0+1=0 \end{align*}\]
The question version
The output (.pdf / .html)
Question: Show that \(e^{i\pi}+1=0\).
Practical1.Rmd
---
title: "MAS9999 Practical 1"
params:
solution: ""
---
1. Write `R` code to plot the `cars` dataset.
```{r, include = params$solution, fig.show = "hide"}
plot(cars)
```
2. Find the numerical value of $2\pi$, to 4 decimal places.
```{r, include = params$solution}
round(2 * pi, 4)
```
3. Show that $e^{i\pi}+1=0$.
```{r, results = "asis", echo = FALSE, include = params$solution}
cat("Solution:
\\begin{align*}
e^{i\\pi}+1&=\\cos\\pi+i\\sin\\pi+1\\\\
&=-1+i\\times0+1=0
\\end{align*}", "\n")
```
The output (Practical1_questions.html)
\(\qquad\)
\(\qquad\)
\(\qquad\qquad\) MAS9999 Practical 1
Write R
code to plot the cars
dataset.
Find the numerical value of \(2\pi\), to 4 decimal places.
Show that \(e^{i\pi}+1=0\).
Practical1.Rmd
---
title: "MAS9999 Practical 1"
params:
solution: ""
---
1. Write `R` code to plot the `cars` dataset.
```{r, include = params$solution, fig.show = "hide"}
plot(cars)
```
2. Find the numerical value of $2\pi$, to 4 decimal places.
```{r, include = params$solution}
round(2 * pi, 4)
```
3. Show that $e^{i\pi}+1=0$.
```{r, results = "asis", echo = FALSE, include = params$solution}
cat("Solution:
\\begin{align*}
e^{i\\pi}+1&=\\cos\\pi+i\\sin\\pi+1\\\\
&=-1+i\\times0+1=0
\\end{align*}", "\n")
```
The output (Practical1_solutions.html)
\(\qquad\)
\(\qquad\)
\(\qquad\qquad\) MAS9999 Practical 1
R
code to plot the cars
dataset.## [1] 6.2832
R, Python and maths questions
---
title: "MAS9999 Practical 1"
params:
solution: ""
---
1. Write `R` code to plot the `cars` dataset.
```{r, include = params$solution, fig.show = "hide"}
plot(cars)
```
2. Find the numerical value of $2\pi$, to 4 decimal places.
```{python, include = params$solution}
import math
round(2 * math.pi, 4)
```
3. Show that $e^{i\pi}+1=0$.
```{r, results = "asis", echo = FALSE, include = params$solution}
cat("Solution:
\\begin{align*}
e^{i\\pi}+1&=\\cos\\pi+i\\sin\\pi+1\\\\
&=-1+i\\times0+1=0
\\end{align*}", "\n")
```
The output (Practical1_solutions.html)
\(\qquad\)
\(\qquad\)
\(\qquad\qquad\) MAS9999 Practical 1
R
code to plot the cars
dataset.## 6.2832
Practical1.qmd
---
title: "MAS9999 Practical 1"
params:
solution: ""
---
1. Write `R` code to plot the `cars` dataset.
```{r}
#| include: !expr params$solution
#| fig.show: "hide"
plot(cars)
```
2. Find the numerical value of $2\pi$, to 4 decimal places.
```{r}
#| include: !expr params$solution
round(2 * pi, 4)
```
3. Show that $e^{i\pi}+1=0$.
```{r}
#| results: "asis"
#| echo: FALSE
#| include: !expr params$solution
cat("Solution:
\\begin{align*}
e^{i\\pi}+1&=\\cos\\pi+i\\sin\\pi+1\\\\
&=-1+i\\times0+1=0
\\end{align*}", "\n")
```
The output (Practical1_solutions.html)
\(\qquad\)
\(\qquad\)
\(\qquad\qquad\) MAS9999 Practical 1
R
code to plot the cars
dataset.## [1] 6.2832
A collection of .Rmd files
index.Rmd
The parameter solution (again)
solution = FALSE
: version with gapssolution = TRUE
: version with solutions
The output format
bookdown::pdf_book
: A single PDF with all chaptersbookdown::gitbook
: Multiple HTML pagesEquations
\begin{equation}
e^{i\pi}+1=0
(\#eq:euler)
\end{equation}
Equation \@ref(eq:euler) is the Euler's identity.
(\#eq:euler)
instead of \label{eq:euler}
\@ref(eq:euler)
instead of \eqref(eq:euler)
eq:
suffices
The output
\[\begin{equation} e^{i\pi}+1=0\qquad\qquad\qquad (1) \end{equation}\] Equation (1) is the Euler’s identity.
Figures
```{r plot-cars, out.width = "70%", fig.cap = "Cars dataset".}
plot(cars)
```
A positive correlation between `speed` and `dist`
can be seen in Figure \@ref(fig:plot-cars).
fig:
when labellingplot_cars
Theorems & proofs
::: {.theorem name="Euler's identity" #euler-identity}
$e^{i\pi}+1=0$
:::
The proof of Theorem \@ref(thm:euler-identity) is as below:
::: {.proof}
\begin{align*}
e^{i\pi}+1&=\cos\pi+i\sin\pi+1\\
&=-1+i\times0+1=0
\end{align*}
:::
thm:
when labelling
The output
Theorem 1 (Euler’s identity) \(e^{i\pi}+1=0\)
The proof of Theorem 1 is as below:
Proof. \[\begin{align*} e^{i\pi}+1&=\cos\pi+i\sin\pi+1\\ &=-1+i\times0+1=0 \end{align*}\]
Other environments
::: {.lemma name="Lemon" #two}
:::
::: {.corollary name="Coriander" #three}
:::
::: {.proposition name="Pepper" #four}
:::
::: {.conjecture name="Confit" #five}
:::
::: {.definition name="Daffodil" #six}
:::
::: {.example name="Exotic" #seven}
:::
::: {.exercise name="Extract" #eight}
:::
::: {.hypothesis name="Hazelnut" #nine}
:::
Lemma \@ref(lem:two), Corollary \@ref(cor:three),
Proposition \@ref(prp:four), Conjecture \@ref(cnj:five),
Definition \@ref(def:six), Example \@ref(exm:seven),
Exercise \@ref(exr:eight), Hypothesis \@ref(hyp:nine)
The output
Lemma 2 (Lemon)
Corollary 3 (Coriander)
Proposition 4 (Pepper)
Conjecture 5 (Confit)
Definition 6 (Daffodil)
Example 7 (Exotic)
Exercise 8 (Extract)
Hypothesis 9 (Hazelnut)
Lemma 2, Corollary 3, Proposition 4, Conjecture 5, Definition 6, Example 7, Exercise 8, Hypothesis 9
Some work
equation
align
aligned
eqnarray
array
$$ $$
\[ \]
Some don’t (for HTML)
center
tabular
displaymath
\intertext
\textcolor
\medskip
\bigskip
\hfill
\newpage
Some have been taken care of
enumerate
: (R)markdown can do listsitemize
: (R)markdown can do listsquote
: (R)markdown can do quotessolution
: The hack using strings in Rverbatim
: Backticks ``example
: Environment extended by bookdownexercise
: Environment extended by bookdownfigure
Cons
Pros
\label{}
outside (sub)(sub)section headings\textcolor
, \medskip
, \bigskip
, \hfill
, \newpage
, \intertext
\eqref{}
itemize
& enumerate
environmentsknitr::include_graphics()
(& convert .pdf to .png)knitr::kable()
(or markdown syntax for tables)dev
, out.width
R Sweave
\section{Properties of $\pi$} \label{sect:pi_properties}
\begin{itemize}
\item $\pi=\Sexpr{pi}\dots$ is \textit{irrational} \& \textbf{transcendental}
\item Type \texttt{pi} in \verb'R' console for numerical value
\end{itemize}
<<>>=
2 * pi
@
<<plot_digits, fig.cap = "Digits of $\\pi$ after the decimal place.">>=
plot(strsplit(sprintf("%.16f", pi), "")[[1]][-(1:2)])
@
The pattern in Figure~\ref{fig:plot_digits}
seems random\footnote{in some sense}.
% A comment that won't appear in the output.
R Markdown
## Properties of $\pi$ {#sect:pi-properties}
* $\pi=`r pi`\dots$ is *irrational* & **transcendental**
* Type `pi` in `R` console for numerical value
```{r}
2 * pi
```
```{r, plot-digits, fig.cap = "Digits of $\\pi$ after the decimal place."}
plot(strsplit(sprintf("%.16f", pi), "")[[1]][-(1:2)])
```
The pattern in Figure \@ref(fig:plot-digits)
seems random^[in some sense].
<!-- A comment that won't appear in the output. -->
Before | After | Remarks |
---|---|---|
\label{([^}]+)_([^}]+)_([^}]+)} |
{#\1-\2-\3} |
Similar for those with fewer "_" |
\ref{([^}]+)_([^}]+)_([^}]+)} |
\@ref(\1-\2-\3) |
Ditto |
\section{([^}]+)} |
## \1 |
One # fewer for single document |
\section*{([^}]+)} |
## \1 {-} |
Ditto; similar for (sub)(sub)sections |
\textit{([^}]+)} |
*\1* |
Same for \emph{([^}]+)} |
\textbf{([^}]+)} |
**\1** |
|
\texttt{([^}]+)} |
`\1` |
Same for \verb |
\Sexpr{([^}]+)} |
`r \1` |
|
\footnote{([^}]+)} |
^[\1] |
|
` `(.*)'' |
"\1" |
Before below replacments with ` |
^<<>>=$ |
```{r} |
|
^<<(.*)>>= |
```{r, \1} |
|
^@[\s]? |
``` |
|
^%(.*) |
<!-- \1 --> |
After fixing environments commented out |
(self-defined LaTeX commands) | (Markdown replacements) | (or just original LaTeX commands) |
Figure~ | Figure
|
Not regex; with space at the end; |
same for Chapter, Section, etc. | ||
\_ |
_ |
Not regex; same for \$ , \% and \& |
rmarkdown
(all lowercase)quarto
rmarkdown
& bookdown
tinytex
\(\rightarrow\) tinytex::install_tinytex()
tinytex::tlmgr_install()
reticulate
YMMV
One R Markdown script to render them all
Bookdown notes
Conversion considerations