---
title: "CS&SS 569 Visualizing Data and Models"
subtitle: "Lab 2: Intro to \\LaTeX{} with \\texttt{R Markdown} and \\texttt{Overleaf}"
author: "Ramses Llobet"
editor_options:
  chunk_output_type: console
output:
  beamer_presentation:
    colortheme: seagull
    fonttheme: structurebold
    theme: Szeged
    highlight: tango
linkcolor: blue
fontsize: 11pt
header-includes:
 \usepackage{float}
 \usepackage{xcolor}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


# Agenda


1. `R Markdown` and HW1

2. \LaTeX{} and `Overleaf`


# Homework Submission

- **Use Canvas** not email


# `R Markdown` and HW1

- Problem 1: Attach files (PDF/picture), and offer your critique and remedy

- Problem 2: Read data and reproduce the plot
  - *Do not spend more than two hours* 


# `R Markdown`

- `R Markdown` file (`.Rmd`) offers an integrated framework
  - To contain both narrative text, code chunks and outputs such as graphs
  - To render ("knit") high quality, reproducible outputs
      - `HTML`, `PDF`, `Word`, `Beamer`, etc.
      - I write my slides using `R Markdown` (in `Beamer`: a \LaTeX{} class of presentation slides)
      - Great way to submit your homework
  - \LaTeX{} code is supported


# \texttt{R-Markdown}

- If any of you is looking for an general introduction for `RMarkdown`, I suggest you to check [Chapter 27](https://r4ds.had.co.nz/r-markdown.html) from Wickham and Grolemund ([2017](https://r4ds.had.co.nz/index.html)) - **R for Data Science**.

- If you want a more comprehensive guide, then check Xie et al. ([2021](https://bookdown.org/yihui/rmarkdown/)) - **R Markdown: The Definitive Guide**.

- Another, more applied, resource is Xie et al. ([2022](https://bookdown.org/yihui/rmarkdown-cookbook/)) - **R Markdown Cookbook**.




# \texttt{R-Markdown}


- `RMarkdown` is a document format that allows you to integrate `R` **code** and **output** into a single document. 

- Besides `R` code and output, it can also include **text**, **images**, and other **multimedia elements**, allowing for rich and informative documents.  


- *Pandoc* is a free and open-source **document converter** that can convert documents from one markup language to another.  


  - In the context of `Rmarkdown`, pandoc is the underlying document converter (sfotware) that converts the R-markdown file into a final output format, such as **HTML**, **PDF**, or **Word**.




# \texttt{R-Markdown}

- The output format of the final document can be customized using options in the **YAML header** or external templates.


```{r, echo=FALSE, out.width="60%", fig.align='center'}
knitr::include_graphics("images/YAML.PNG")
```


- The YAML header in RMarkdown is a block of configuration settings at the beginning of the document enclosed by three hyphens (`---`).


- It is used to specify document metadata and other settings such as the document title, author, output format, and more.

# \texttt{R-Markdown}

- **Code chunks** are sections of `R` code that can be executed and embedded within an `RMarkdown` document.

```{r, echo=FALSE, out.width="100%", fig.align='center'}
knitr::include_graphics("images/chunk.PNG")
```



  - Code chunks can be inserted using the syntax `{r}` and closed with ```. 
  
    - Short cut in Windows: \texttt{Ctrl + Alt + I}
    - Short cut in macOS: \texttt{Cmd + Option + I}



- Code chunks can be customized with various **chunk options**.




# \texttt{R-Markdown}


- **Note**: set the function `knitr::opts_chunk$set()` with any general setting without repeating it in every code chunk.

- Recommendation chunck options for Homework


```{r, echo=FALSE, out.width="80%", fig.align='center'}
knitr::include_graphics("images/chunk_settings.png")
```



# \texttt{R-Markdown}


- In `RMarkdown`, **rendering** a document means converting the source `RMarkdown` file into its final output format (using pandoc).


- To render a document, we need to \texttt{Knit}, knitting is the process of taking the RMarkdown file and converting it into a single, cohesive document that can be rendered into different formats (HTML, PDF, etc).


- To compile a `R Markdown` document to `PDF`, you need to install \LaTeX{}
  - If you haven't installed any previous \LaTeX{} distribution, I recommend `TinyTeX`



# `R Markdown` and `TinyTeX`

- "`TinyTeX` is a lightweight, portable, cross-platform, and easy-to-maintain LaTeX distribution":  

```{r, eval = FALSE}
install.packages('tinytex')
tinytex::install_tinytex()
```



# `Knitting`

- To knit:

```{r, echo=FALSE, out.width="80%", fig.align='center'}
knitr::include_graphics("images/knitting.PNG")
```

- Auxiliary window for output preview:

```{r, echo=FALSE, out.width="60%", fig.align='center'}
knitr::include_graphics("images/knit.PNG")
```


# `R Markdown` in practices: two examples 

- `lab2_RMarkdownSample.Rmd`: cover most of the basic functionalities in R Markdown; good for future reference

- `CSSS569HW1Start.Rmd`: a template to get started with HW 1!



# Intro to \LaTeX with `Overleaf`

- Alternatively, we have `Overleaf`: <https://www.overleaf.com/> 
  - An online \LaTeX editor  
    - Integrated PDF preview panel  
    - Quality of life features: auto-complete commands, auto-close brackets, keyboard shortcuts, etc.  
    - Numerous templates: journal articles, books, CVs, slides, posters, etc.  
    - Easy collaboration (But not free)  
    - Integrated with `Zotero` and `Mendeley` for bibliography management  
    - Integrated with `Git` for version control
    
# Intro to \LaTeX with `Overleaf`
- Before we dive in, useful resources.
  - [The Not So Short Introduction to \LaTeXe](https://tobi.oetiker.ch/lshort/lshort.pdf) (Oetiker et al., 2023).
    - Learn \LaTeX in 280 pages / minutes.
  - [`Overleaf` documentation](https://www.overleaf.com/learn/).
    - Contains intro to basic \LaTeX, `Overleaf`, and many practical guides.
  - [\TeX at StackExchange](https://tex.stackexchange.com).
  -  General: [Mathematics](https://en.wikibooks.org/wiki/LaTeX/Mathematics) and [Tables](https://en.wikibooks.org/wiki/LaTeX/Tables) and [TikZ](https://sites.google.com/site/kochiuyu/Tikz).
  -  Beamer Theme: [here](https://hartwork.org/beamer-theme-matrix/).
  -  Bibliography: [natbib](http://merkel.texture.rocks/Latex/natbib.php), [doi2bib](https://www.doi2bib.org/), [text2bib](https://text2bib.economics.utoronto.ca/)
  - Other: [here](https://gking.harvard.edu/pages/student-materials).

  

  
# Intro to \LaTeX{} with `Overleaf`
- Some useful templates:
  - Thesis: [here](https://www.overleaf.com/gallery/tagged/thesis).
  - Homework: my sample with appendix for R code [here](https://www.overleaf.com/read/bznsdyxrmmyg#f7b3f2), another [here](https://www.overleaf.com/3192366323hndhdggvqczq#400023).
  - Working paper: [Kenya's sample](https://www.overleaf.com/4935746599hvsbzbxjfcyx#444679) and [Chris's sample](https://www.overleaf.com/project/5e42e4a9ae15f200014a4919) (*not for beginners*).
  - Academic journal: [here](https://www.overleaf.com/latex/templates/tagged/academic-journal).
  - Presentation slides (Beamer): [here](https://www.overleaf.com/latex/templates/tagged/beamer).
  - Poster presentations: [here](https://www.overleaf.com/gallery/tagged/poster)
  - CVs and résumés: [here](https://www.overleaf.com/gallery/tagged/cv).
  - Graphs, trees, diagrams (TikZ): tutorial [here](https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ%3A_A_Tutorial_for_Beginners_(Part_1)%E2%80%94Basic_Drawing) and gallery [here](https://www.overleaf.com/gallery/tagged/tikz).



# 

\Huge

\centerline{F I N}
