6  Markdown and LaTeX

6.1 The Power of Plain Text

Scientific communication requires more than just words—we need formatted text, mathematical equations, code, figures, and tables. Traditionally, researchers used word processors for this purpose, but word processors have significant limitations for technical writing. They obscure the structure of documents behind visual formatting. They make collaboration difficult because different versions become hard to reconcile. They separate code from the documents that describe it, making it easy for analyses and their descriptions to get out of sync.

Markup languages offer a different approach. You write in plain text, adding simple annotations that specify how the document should be formatted. A processor then converts this annotated text into beautifully formatted output. Because the source is plain text, it can be version-controlled, compared across versions, and edited with any text editor.

6.2 What is Markdown?

Markdown is a lightweight markup language designed to be easy to read and write. Created by John Gruber in 2004, it uses intuitive syntax that looks reasonable even before processing. A document written in Markdown can be rendered into HTML, PDF, Word documents, presentations, websites, and more.

The basic philosophy is that common formatting should be quick to type and not interrupt the flow of writing. Bold text is wrapped in double asterisks. Italics use single asterisks. Headers are indicated by hash marks at the start of a line.

Text Formatting

To make text italic, wrap it in single asterisks or underscores:

*italic text* or _italic text_

For bold text, use double asterisks or underscores:

**bold text** or __bold text__

You can combine them for bold italic:

***bold and italic***

Headers

Headers structure your document into sections. Use hash marks at the start of a line, with more hashes indicating lower-level headers:

# First-Level Header
## Second-Level Header
### Third-Level Header

Lists

Create bullet lists by starting lines with dashes, asterisks, or plus signs:

- First item
- Second item
    - Sub-item (indent with spaces or tabs)
    - Another sub-item
- Third item

For numbered lists, use numbers followed by periods:

1. First step
2. Second step
3. Third step

Block Quotes

Block quotes are useful for highlighting important passages or attributing quotes:

> "You know the greatest danger facing us is ourselves, 
> an irrational fear of the unknown. But there's no such 
> thing as the unknown — only things temporarily hidden, 
> temporarily not understood."
>
> --- Captain James T. Kirk

This renders as:

“You know the greatest danger facing us is ourselves, an irrational fear of the unknown. But there’s no such thing as the unknown — only things temporarily hidden, temporarily not understood.”

— Captain James T. Kirk

Code

Inline code is wrapped in single backticks: `code`. Code blocks use triple backticks, optionally specifying the language for syntax highlighting:

```r
x <- rnorm(100)
mean(x)
```

6.3 What is LaTeX?

LaTeX (pronounced “LAH-tek” or “LAY-tek”) is a document preparation system for high-quality typesetting, particularly of technical and scientific documents. Originally created by Leslie Lamport in the 1980s, LaTeX builds on the TeX typesetting system developed by Donald Knuth.

LaTeX excels at mathematical notation. Complex equations that would be tedious or impossible to create in a word processor can be expressed elegantly in LaTeX. The system handles numbering, cross-references, bibliographies, and other scholarly apparatus automatically.

Most importantly for our purposes, LaTeX can be embedded directly in Markdown documents. This gives us the best of both worlds: the simplicity of Markdown for prose and the power of LaTeX for mathematics.

6.4 Mathematical Notation with LaTeX

Inline and Display Math

Mathematical expressions can appear inline with text or as centered display equations. Inline math is wrapped in single dollar signs: $x^2$ produces \(x^2\). Display equations use double dollar signs:

$$E = mc^2$$

Produces:

\[E = mc^2\]

Greek Letters and Symbols

Greek letters are typed as their names preceded by a backslash:

$$\alpha, \beta, \gamma, \delta, \epsilon$$
$$\mu, \sigma, \pi, \theta, \lambda$$

\[\alpha, \beta, \gamma, \delta, \epsilon\] \[\mu, \sigma, \pi, \theta, \lambda\]

Common mathematical symbols:

$$\neq, \approx, \leq, \geq, \pm, \times, \div$$

\[\neq, \approx, \leq, \geq, \pm, \times, \div\]

Superscripts and Subscripts

Superscripts use the caret ^ and subscripts use the underscore _:

$$x^2, x_i, x_i^2, x_{ij}$$

\[x^2, x_i, x_i^2, x_{ij}\]

For multi-character superscripts or subscripts, use curly braces:

$$x^{n+1}, x_{i,j}$$

\[x^{n+1}, x_{i,j}\]

Fractions

Fractions use the \frac{numerator}{denominator} command:

$$\frac{a}{b}, \frac{x^2 + 1}{x - 1}$$

\[\frac{a}{b}, \frac{x^2 + 1}{x - 1}\]

Square Roots

Square roots and nth roots:

$$\sqrt{x}, \sqrt[3]{x}, \sqrt[n]{x}$$

\[\sqrt{x}, \sqrt[3]{x}, \sqrt[n]{x}\]

Summation and Products

Sums and products with limits:

$$\sum_{i=1}^{n} x_i, \prod_{i=1}^{n} x_i$$

\[\sum_{i=1}^{n} x_i, \prod_{i=1}^{n} x_i\]

Integrals

Integrals with limits:

$$\int_{a}^{b} f(x) \, dx$$
$$\iint f(x,y) \, dx \, dy$$

\[\int_{a}^{b} f(x) \, dx\] \[\iint f(x,y) \, dx \, dy\]

Statistical Formulas

Here are some common statistical formulas in LaTeX:

The sample mean:

$$\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i$$

\[\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i\]

The sample variance:

$$s^2 = \frac{1}{n-1}\sum_{i=1}^{n} (x_i - \bar{x})^2$$

\[s^2 = \frac{1}{n-1}\sum_{i=1}^{n} (x_i - \bar{x})^2\]

The binomial probability formula:

$$P(X=k) = \binom{n}{k} p^{k} (1-p)^{n-k}$$

\[P(X=k) = \binom{n}{k} p^{k} (1-p)^{n-k}\]

The Poisson probability formula:

$$P(Y=r) = \frac{e^{-\mu}\mu^r}{r!}$$

\[P(Y=r) = \frac{e^{-\mu}\mu^r}{r!}\]

The normal distribution density:

$$f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}$$

\[f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}\]

Matrices

Matrices are created with the matrix environment:

$$\begin{matrix}
a & b \\
c & d
\end{matrix}$$

\[\begin{matrix} a & b \\ c & d \end{matrix}\]

For brackets around the matrix, use pmatrix (parentheses) or bmatrix (square brackets):

$$\begin{pmatrix}
a & b \\
c & d
\end{pmatrix}$$

\[\begin{pmatrix} a & b \\ c & d \end{pmatrix}\]

6.5 Quarto and R Markdown

Quarto and R Markdown extend Markdown by allowing you to embed executable code chunks. When the document is rendered, the code runs and its output—whether text, tables, or figures—is automatically included in the final document.

Document Structure

A Quarto document has three main parts:

  1. YAML Header: Document metadata and settings (enclosed by ---)
  2. Markdown Content: Text and formatting
  3. Code Chunks: Executable code blocks

YAML Header

The YAML header appears at the top of the document and configures how it will be rendered:

---
title: "My Analysis Report"
author: "Your Name"
date: today
format: html
---

Common YAML options include:

Option Description
title Document title
author Author name(s)
date Publication date (today for current date)
format Output format: html, pdf, docx
toc Include table of contents (true/false)
code-fold Collapse code by default
execute Global code execution options

A more complete header might look like:

---
title: "Genomic Analysis Report"
author: "Jane Scientist"
date: today
format:
  html:
    theme: cosmo
    toc: true
    code-fold: true
execute:
  echo: true
  warning: false
---

Code Chunks

A code chunk in Quarto looks like this:

```{r}
x <- rnorm(100)
mean(x)
```

When rendered, this shows both the code and its output:

Code
x <- rnorm(100)
mean(x)
[1] -0.001136062

Chunk Options

Chunk options control how code is displayed and executed. In Quarto, options are specified with #| comments at the start of the chunk:

```{r}
#| label: my-analysis
#| echo: false
#| fig-width: 6
#| fig-height: 4
hist(rnorm(1000))
```

Essential chunk options:

Option Description Default
echo Show code in output true
eval Execute the code true
include Include chunk in output true
warning Show warnings true
message Show messages true
fig-cap Figure caption none
fig-width Figure width (inches) 7
fig-height Figure height (inches) 5

Callout Blocks

Quarto provides special callout blocks for highlighting information:

::: {.callout-note}
This is a note with additional information.
:::

::: {.callout-warning}
This warns readers about potential issues.
:::

::: {.callout-tip}
This provides helpful tips.
:::

These render as visually distinct boxes that draw attention to important content.

Note

This is an example of a callout note—useful for supplementary information.

Warning

This is a warning callout—use for potential pitfalls or important cautions.

Cross-References

Quarto supports cross-references to figures, tables, and sections. Label items and reference them with @:

```{r}
#| label: fig-scatter
#| fig-cap: "Relationship between x and y"
plot(x, y)
```

See @fig-scatter for the scatter plot.

For sections, add a label after the header:

## Methods {#sec-methods}

As described in @sec-methods, we used...

Rendering Documents

To render a Quarto document:

  • In RStudio: Click the “Render” button or press Ctrl+Shift+K (Windows/Linux) or Cmd+Shift+K (Mac)
  • Command line: Run quarto render document.qmd

You can render to multiple formats:

quarto render document.qmd --to html
quarto render document.qmd --to pdf
quarto render document.qmd --to docx

6.6 Why This Matters

The combination of Markdown, LaTeX, and executable code enables truly reproducible research. Your analysis code lives in the same document as your prose. When data change, you re-render the document and everything updates automatically. Collaborators can see exactly what you did. Future you can understand past you’s work.

These tools have become standard in data science. Learning them now will pay dividends throughout your career, whether you are writing homework assignments, thesis chapters, journal articles, or technical reports.

6.7 Practice Exercises

Exercise M.1: R Markdown Basics

Create a new R Markdown document and practice:

  1. Creating headers at different levels using #, ##, and ###
  2. Making text italic and bold
  3. Creating ordered and unordered lists
  4. Inserting a hyperlink
  5. Creating a code chunk that generates output
  6. Knitting the document to HTML
Exercise M.2: Code Chunk Options

Experiment with code chunk options:

  1. Create a code chunk with echo=TRUE and eval=TRUE (default behavior)
  2. Create the same code chunk with echo=FALSE—what happens?
  3. Try eval=FALSE—what happens?
  4. Use fig.width and fig.height to control plot dimensions
Exercise M.3: LaTeX Mathematical Notation

Practice writing equations in LaTeX:

  1. Write the equation for the mean: \(\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i\)
  2. Write the equation for variance
  3. Write the normal distribution probability density function
  4. Use both inline math ($...$) and display math ($$...$$)
Exercise M.4: Tables in Markdown

Create tables using:

  1. Basic Markdown table syntax
  2. The kable() function from the knitr package
Code
library(knitr)
kable(head(iris, 5))