Creating accessible & reproducible teaching materials with minimum scaffolding

Clement Lee (Newcastle University)

2024-06-14

Outline

  • From old-fashioned teaching materials
    • verbatim R code & output in .tex
    • auxiliary files: .pdf, .png, …
    • R scripts for generating the auxiliary files
    • by products: .out, .log, .aux, …
  • To accessible & reproducible materials
    • separate R scripts
    • {html, pdf} \(\times\) {solutions, questions/gaps}
  • Aggregation of marginal gains (Kaizen)
  • Manual but efficient workflow
    • Focus on future savings
    • Script-based, good for version control
    • As little scaffolding as possible
  • What works and what doesn’t
  • R Markdown tutorial

Starting point

 

The output (.pdf)

MATH9999 Practical 1 Questions
2024-06-14

  1. Plot the cars dataset in R.
  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.
  3. Show that \(e^{i\pi}+1=0\).

A separate solution file

 

The output (.pdf)

MATH9999 Practical 1 Solutions
2024-06-14

  1. Plot the cars dataset in R.

  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.

    ## [1] 6.2832
  3. \(~~~\)Show that \(e^{i\pi}+1=0\). \[\begin{align*} e^{i\pi}+1&=\cos\pi+i\sin\pi+1\qquad\qquad\qquad\qquad\\ &=-1+i\times0+1=0 \end{align*}\]

1. Reproducibility / dynamic generation

 

The output (.pdf)

MATH9999 Practical 1 Solutions
2024-06-14

  1. Plot the cars dataset in R.

  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.

    ## [1] 6.2832
  3. \(~~~\)Show that \(e^{i\pi}+1=0\). \[\begin{align*} e^{i\pi}+1&=\cos\pi+i\sin\pi+1\qquad\qquad\qquad\qquad\\ &=-1+i\times0+1=0 \end{align*}\]

2. R Sweave to R Markdown

 

The output (.pdf)

MATH9999 Practical 1 Solutions
2024-06-14

  1. Plot the cars dataset in R.

  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.

    ## [1] 6.2832
  3. \(~~~\)Show that \(e^{i\pi}+1=0\). \[\begin{align*} e^{i\pi}+1&=\cos\pi+i\sin\pi+1\qquad\qquad\qquad\qquad\\ &=-1+i\times0+1=0 \end{align*}\]

2. (cont’d)

 

The output (.html)

MATH9999 Practical 1 Solutions
2024-06-14

  1. Plot the cars dataset in R.

  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.

    ## [1] 6.2832
  3. \(~~~\)Show that \(e^{i\pi}+1=0\). \[\begin{align*} e^{i\pi}+1&=\cos\pi+i\sin\pi+1\qquad\qquad\qquad\qquad\\ &=-1+i\times0+1=0 \end{align*}\]

3. Toggle for questions & solutions

 

The output (.html)

MATH9999 Practical 1 Solutions
2024-06-14

  1. Plot the cars dataset in R.

  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.

    ## [1] 6.2832
  3. \(~~~\)Show that \(e^{i\pi}+1=0\). \[\begin{align*} e^{i\pi}+1&=\cos\pi+i\sin\pi+1\qquad\qquad\qquad\qquad\\ &=-1+i\times0+1=0 \end{align*}\]

3. (cont’d)

 

The output (.html)

MATH9999 Practical 1 Solutions
2024-06-14

  1. Plot the cars dataset in R.

  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.

  3. \(~~~\)Show that \(e^{i\pi}+1=0\). \[\begin{align*} e^{i\pi}+1&=\cos\pi+i\sin\pi+1\qquad\qquad\qquad\qquad\\ &=-1+i\times0+1=0 \end{align*}\]

4. Parametrise the toggle

 

The output (.html)

MATH9999 Practical 1 Questions
2024-06-14

  1. Plot the cars dataset in R.

  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.

  3. \(~~~\)Show that \(e^{i\pi}+1=0\). \[\begin{align*} e^{i\pi}+1&=\cos\pi+i\sin\pi+1\qquad\qquad\qquad\qquad\\ &=-1+i\times0+1=0 \end{align*}\]

4. (cont’d)

 

The output (.html)

MATH9999 Practical 1 Solutions
2024-06-14

  1. Plot the cars dataset in R.

  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.

    ## [1] 6.2832
  3. \(~~~\)Show that \(e^{i\pi}+1=0\). \[\begin{align*} e^{i\pi}+1&=\cos\pi+i\sin\pi+1\qquad\qquad\qquad\qquad\\ &=-1+i\times0+1=0 \end{align*}\]

5. Hack the non-coding questions

 

The output (.html)

MATH9999 Practical 1 Solutions
2024-06-14

  1. Plot the cars dataset in R.

  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.

    ## [1] 6.2832
  3. \(~~~\)Show that \(e^{i\pi}+1=0\). \[\begin{align*} e^{i\pi}+1&=\cos\pi+i\sin\pi+1\qquad\qquad\qquad\qquad\\ &=-1+i\times0+1=0 \end{align*}\]

5. (cont’d)

 

The output (.html)

MATH9999 Practical 1 Questions
2024-06-14

  1. Plot the cars dataset in R.

  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.

  3. Show that \(e^{i\pi}+1=0\).

\[~\] \[~\] \[~\]

6. Externalise the parameters

 

The R command

\[~\] \[~\] \[\Downarrow\Downarrow\Downarrow\] \[~\] \[~\]

The output (Practical1_questions.html)

MATH9999 Practical 1 Questions
2024-06-14

  1. Plot the cars dataset in R.

  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.

  3. \(~~~\)Show that \(e^{i\pi}+1=0\).

6. (cont’d)

 

The R command

\[~\] \[~\] \[\Downarrow\Downarrow\Downarrow\] \[~\] \[~\]

The output (Practical1_questions.pdf)

MATH9999 Practical 1 Questions
2024-06-14

  1. Plot the cars dataset in R.

  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.

  3. \(~~~\)Show that \(e^{i\pi}+1=0\).

7. Bringing it all together

 

Variant A: Language agnostic(-ish)

 

The output (.html)

MATH9999 Practical 1 Solutions
2024-06-14

  1. Plot the cars dataset in R.

  2. Find the numerical value of \(2\pi\), to \(4\) decimal places.

    ## 6.2832
  3. \(~~~\)Show that \(e^{i\pi}+1=0\). \[\begin{align*} e^{i\pi}+1&=\cos\pi+i\sin\pi+1\qquad\qquad\qquad\qquad\\ &=-1+i\times0+1=0 \end{align*}\]

Variant B: Slides

 

Upgrade: whole set of notes

 

What about LaTeX environments & commands?

Some work

  • equation
  • align
  • aligned
  • eqnarray
  • array
  • (and their * versions)
  • $$ $$
  • \[ \]

 

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 lists
  • itemize: (R) Markdown can do lists
  • quote: (R) Markdown can do quotes
  • solution: The hack using strings in R
  • verbatim: Backticks ``
  • example: Environment extended by bookdown
  • exercise: Environment extended by bookdown
  • figure: Function knitr::include_graphics()

We’ve come a long way

 

Tasks when converting

  1. Edit the preamble
  2. Move \label{} outside (sub)(sub)section headings
  3. Replacements with regular expressions (regex)
  4. Replacements with strings
  5. Remove some LaTeX commands: \textcolor, \medskip, \bigskip, \hfill, \newpage, \intertext
  6. Edit section & equation labels & \eqref{}
  7. Edit “other” environments in conjunction with their labels
  8. Edit itemize & enumerate environments
  9. Convert environments to R code chunks:
    • solution: the string hack
    • figure: knitr::include_graphics() (& convert .pdf to .png)
    • verbatim: actual R code without hardcoded results
    • tabular: knitr::kable() (or markdown syntax for tables)
  10. Fix code chunk labels ("_" \(\rightarrow\) “-”) & options: dev, out.width
  11. Add space in solution code chunks for PDF version, in conjunction with figure positions

Replacements (in this order)

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 \&

Scaffolding

  1. R(Studio)
  2. Package rmarkdown (all lowercase)
    • For standalone documents & whole set of notes
  3. Package bookdown
    • Only required for whole set of notes
  4. Packages specific for your module
    • Most likely in the R script
  5. Package tinytex (recommended)
    • For below
  6. LaTeX distribution & TeX packages
    • installed within R via tinytex::install_tinytex()
    • tinytex::tlmgr_install() useful
  7. Package reticulate & Python (3)
    • For Python content
  • YMMV: Even though they are meant for reproducibility
  • E.g. Labelling & referencing that works in bookdown doesn’t automatically work in R Markdown

 

Summary

Benefits?

Thank you!