# R Markdown To Latex

## The Bigger Picture #

In this document we learn how to create and manipulate R Markdown documents. Simply put, we are learning how to create documents, slideshows, websites and reports to produce and communicate the visualisations created earlier on. In the overall context of the workflow, this falls into the category of producing our presentations.

There are references to LinkedIn Learning videos. These are complementary but not really required as the notes below are meant to be self-contained. Some students and staff would have access for free. Do not purchase access unless you are sure you don’t have access through your organisation already.

Historically, R Markdown is an extension of the older Sweave/Latex environment. Rendering of mathematical expressions and reference management is also supported by R Markdown using embedded Latex syntax and Bibtex, respectively. Quick Start Install R Markdown install.packages (“rmarkdown”) Initialize a new R Markdown (Rmd) script. Does there exist open source LaTeX converters that produce Markdown, in the same manner tools like latex2html does?

## What is R Markdown? #

1. Turn your analyses into high quality documents, reports, presentations and dashboards with R Markdown. Use a productive notebook interface to weave together narrative text and code to produce elegantly formatted output. Use multiple languages including R, Python, and SQL. R Markdown supports a reproducible workflow for dozens of static and dynamic output formats including HTML, PDF, MS.
2. TLDR: This tutorial teaches you how to install LaTeX, R and R Markdown on Windows 10. It also guides you through creating your first R Markdown file and shows how to compile it into a PDF file using.
3. Riinu”s scripting diary.

### Markup Languages #

• R Markdown is a markup language
• Markup langages are systems for annotating documents and other media
• Some other markup languages are:
• Markdown (different from R Markdown!)
• LaTeX
• HTML

### How does R Markdown work? #

• R Markdown begins by looking like a weird R script with its own special syntax
• R Markdown documents have the special file extension .Rmd
• It includes chunks of R code, and possibly some snippets of other languages
• After we “knit” the document together (according to how we specify), it looks like a nicely rendered form of media
• The document you are currently reading was built in R Markdown!

### What can R Markdown do? #

There are three broad types of documents R Markdown can produce. Note that each have sub-categories, and other document types exist. See here for more information.

#### PDF #

• These documents always look the same
• They cannot be edited without leaving a ‘footprint’ (the edits will be noticeable!)
• They can be password protected

#### HTML Reports #

• Documents which can be put online
• They are easily viewable on mobile
• These reports can interact with htmlwidgets (moving objects, responsive to the viewer’s behaviour!)

#### HTML Presentations #

• These are HTML documents comparable to a slideshow presentation
• There are several preset styles
• Some styles are very customisable

#### Other Media #

R Markdown can also produce:

• Microsoft PowerPoint presentations
• Microsoft Word documents
• Blog-style webpages (Blogdown)
• Multi-chapter books and reference documents (Bookdown)

## LaTeX, BibTeX and TeX #

### LaTeX #

• LaTeX is a markup language like R Markdown
• It is heavily used in academia for its ability to create mathematical formulae with ease and precision
• We can call LaTeX in R Markdown if we configure RStudio correctly

### BibTeX #

• A reference management software for LaTeX
• It is required to customise the appearance and layout of all PDF documents in R Markdown

### TeX #

• A typesetting system which encompasses LaTeX
• Developed with LaTeX partly to process mathematical formulae
• LaTeX is one distribution of TeX

## Installing TeX #

This installation is needed to call upon several functions of R Markdown. Depending on your operating system, the installation for TeX will be one of these options:

macOS Linux Windows
Go to http://www.tug.org/mactex and install TeX Run sudo apt-get install texlive-full Go to https://miktex.org/download and install TeX

After the installation, RStudio will automatically be able to detect TeX on your computer.

## Installing Pandoc #

• Pandoc is a software which can convert files from one markup format to another
• This is what will convert our .Rmd (R Markdown) files to other, more useful formats

## The R Markdown Package #

R Markdown must be installed as a package through R Studio by running the following:

• R Markdown is now almost up and running
• The final step is to watch for RStudio warning that extra packages need to be installed for R Markdown to operate
• A pop-up may or may not appear, depending on the current version of rmarkdown

Once these are installed, R Markdown is ready for use!

## The R Markdown interface #

Reminder: R Markdown files are of the type .Rmd

• When we’re in RStudio, we create a new R Markdown document
• It is recommended to do this in a new (or existing) R Project
• Navigate to File()New File()Rmarkdown..
• We are immediately forced to make a choice, however, no selection we make is permanent
• By default, we will begin by working in an HTML document
• When we select OK, we have an R Markdown document in front of us
• If we save this document, it will appear in our files tab
• The document currently appears to look like a script
• We use the “Knit” button to compile this script into the document type we chose
• We are also able to freely modify the text in the .Rmd file
• This is our first step to creating our own reports

• YAML is a standard for configuration files
• By modifying the “YAML header,” we configure our R Markdown file
• The YAML header is everything between the two ---
• It is automatically generated when we create a document
• In this format, indentation matters, so when we modify our headers we must be careful

### Basic options: #

• The title, author and date options are self-explanatory
• The output option dictates the document we produce when we Knit (by default it is set to whatever we selected when creating the document)
• Some output styles (which we will cover) are:
• html_document and bookdown::html_document2
• pdf_document and bookdown::pdf_document2
• slidy_presentation
• ioslides_presentation
• beamer_presentation

• There are very many different fields we can add to the YAML header
• Some are simple to include and require only one line, such as the subtitle field added below
• Others are more complex and must be formatted by several layers of indentation, such as the toc field
• We will introduce a few more fields later on, but here are lists of useful PDF-relevant fields and useful HTML-relevant fields

## Code Chunks #

R Markdown is enormously useful for its ability to display and run code.

### Creating Code Chunks #

• “Code chunks” are small, dedicated sections of the R Markdown document which come with their own options for displaying or running code
• We begin a code chunk with three backticks (s) and end it with three backticks
• After the three backticks we have a section of curly brackets ({ })
• In these backticks, we state the language we are coding in, then type a space, then name the code chunk
• Warning: never give two code chunks in the same document the same name, or the document will not compile
• The above code runs and produces the code shown above

### Code Chunk options #

#### Global Chunk options #

• When creating chunks, all options are set to a “global” default which spans the entire document
• These default options are specified in the chunk called setup, which is automatically generated at the beginning of our .Rmd file

### LaTeX Formatting #

To call upon (LaTeX) formatting, we have two options.

• If we wish to call on a formula in a line, we surround the line with ( and )

(X_{t} = X_{t-1} + Z_t + Z_{t-1})

• We may also frame a section of LaTeX code with $$ to render entire sections of formatting$$quadbegin{pmatrix}12 & 4 & 1
4/3 & 0.9 & e
pi & 0 & 8
end{pmatrix}quad

## Calling on HTML in R Markdown #

Note that this is only possible in HTML document formats.

Some HTML will run fine and without the need for formatting after being pasted into R Markdown. If this is not the case, code can be framed with special “html_preserve” framing.

## Pubishing HTML Documents to the Web #

There are two main ways to publish:

• RPubs is a free platform provided by RStudio
• GitHub Pages is a free-to-use platform, but requires knowledge of Git

## Using RPubs #

1. Connect to the RPubs website and register an account

2. Click the “Publish” button from within R Studio. This can be done either from the .Rmd file or from the knitted document

3. Select “Publish”

4. Set a title, description, and choose a slug (the slug is the last part of the URL)

Your document is now public at the available URL!

### Markdown Vs Latex

1. Mann, H. (2019) ↩︎

LaTeX is a document prepration system that is very powerful but can sometimes also be a bit confusing. Hence, some useful details are provided that should help R/exams users getting LaTeX installed and used effectively for preparing exams.

## What?

LaTeX is a software system for document preparation, see the corresponding Wikipedia page for an overview. The system encompasses various elements that are important to distinguish:

• It uses a markup language that can be written in any simple text editor with commands structuring a document (title, sections, etc.), formatting text (italics, bold face, etc.), and providing a powerful mathematical notation.
• Subsequently, a TeX engine can be used to process such a text file and produce an output document that can be viewed, printed, etc. The probably most commonly-used TeX engine is pdfTeX rendering LaTeX files into PDF documents.
• TeX distributions ship collections of such TeX engines along with further utilities, macros, and fonts for processing LaTeX files. The most widely-used TeX distributions are TeX Live available on all major operating systems and MikTeX for Windows.

## Why?

In general LaTeX it is very useful for professional typesetting, especially of scientific documents, and is the most widely-used markup language for mathematical notation. However, the complexity of the LaTeX markup language can also be confusing, especially for newcomers, and hence it can be easier to use other markup languages such as Markdown for many documents. But even when using Markdown as the main markup for a document, LaTeX may still be involved in producing an output document: either only for mathematical notation (e.g., in HTML output) and/or for rendering PDF output. In short, due to its power and flexibility LaTeX is used either explicitly or implicitly in the preparation of a lot of output documents.

Consequently, R/exams leverages LaTeX, mainly for three purposes:

• As the main markup language for authoring exercises and formatting text in R/LaTeX (Rnw) format.
However, this is optional and the R/Markdown (Rmd) format for authoring exercises is probably more widely used. See the First Steps tutorial for a side-by-side comparison.
• As the markup language for mathematical notation.
This is used in both Rnw and Rmd exercises and can be displayed in various ways. See the Mathematical Notation tutorial for some guidance, especially when rendering online exams.
• As the engine for producing PDF output.
This is also used for both Rnw and Rmd exercises, especially when using exams2pdf() for flexible, customizable PDF output and exams2nops() for standardized PDF output that can be automatically scanned and evaluated.

## How?

If you use R/exams for rendering exercises without mathematical notation into online exams then you likely do not need LaTeX at all (and can stop reading here).

If you only use mathematical notation but still only generate online HTML-based exams, especially for e-learning materials, then you need a bit of the markup language but do not actually need to install a TeX engine as part of a TeX distribution.

However, when you produce PDF output, be it from Rnw or Rmd exercises, then you need to install a TeX distribution with suitable TeX engines. In case of R/Markdown exercises in Rmd format, R/exams converts the Markdown markup to LaTeX behind the scenes (using the powerful pandoc document converter), before using a TeX engine to render the PDF output. The following comments should help you choose an appropriate way to install and interface a TeX distribution when working with R/exams.

The TeX distribution providing the TeX engine(s) can either be installed “as usual” on the system, e.g., by downloading MikTeX for Windows or installing TeX Live from a standard repository under Linux or OS X. Alternatively, the R package tinytex offers the possibility for installing a lightweight TeX distribution called “TinyTeX”, based on TeX Live. The latter option is particularly attractive for R/exams users that do not actually use LaTeX for other projects. This is due to a couple of reasons: Everything is handled through R; no special super user rights are needed for the installation; and the installation is minimal with additional style files etc. being installed automatically only if needed. To install the R package tinytex you just need

or alternatively use the graphical user interface in RStudio etc. To install the TeX distribution “TinyTeX” you just need

Finally, even when you have installed a TeX distribution (other than TinyTeX) on your system (e.g., TeX Live or MikTeX), the R package tinytex may (or may not) still be useful for producing PDF exams. Three cases need to be distinguished.

1. System LaTeX via tools::texi2dvi()
When: The R package tinytex is not installed or when setting options(exams_tex = "tools").
Why: For R/exams users that also use LaTeX for other projects and do not need any further assistance and hence do not need to introduce another dependency in their code.

2. System LaTeX via tinytex::latexmk()
When: The R package tinytex is installed but not the TinyTeX distribution (and the exams_tex option is not set to "tools").
Why: This tries to resolve some dependencies automatically (e.g., additional style files). Also, it not only supports the "pdflatex" engine but also "xelatex" or "lualatex" etc., which may be useful/necessary for handling certain fonts.

3. TinyTeX via tinytex::latexmk()
When: The R package tinytex is installed and tinytex::install_tinytex() was used to install the TinyTeX distribution (and the exams_tex option is not set to "tools"`).
Why: For users that do not use LaTeX outside of R as it is more lightweight and very easy to install/maintain.

### Export R Markdown To Latex

Achim ZeileisTUTORIALS
LaTeXMathMLMathJax