LaTeX and Technical Documents

LaTeX is a typesetting system that turns plain-text source into beautifully formatted documents, and it is the de facto standard for anything math-heavy. If you write equations, you will get cleaner, more consistent results with LaTeX than with a word processor, and the same math syntax works inside R Markdown, Quarto, and Jupyter.

One plain-text math source, many outputs. The same LaTeX notation written once — inline as $...$ or in an .Rmd / .qmd source — renders through the same toolchain (LaTeX/tinytex for print, KaTeX or MathJax for the web) into a paper PDF, an HTML page, slides, and a notebook, so the equation you type is portable across every target.
Figure 1. One plain-text math source, many outputs. The same LaTeX notation written once — inline as $...$ or in an .Rmd / .qmd source — renders through the same toolchain (LaTeX/tinytex for print, KaTeX or MathJax for the web) into a paper PDF, an HTML page, slides, and a notebook, so the equation you type is portable across every target.

Why LaTeX for Math?#

Inline vs. Display Math#

Wrap math in dollar signs. A single $...$ is inline (flows within a sentence); double $$...$$ is display (centered on its own line).

Latex
The estimator $\hat{\beta}$ is unbiased when the errors have mean zero.

$$
\hat{\beta} = (X^\top X)^{-1} X^\top y
$$

Do use inline math for symbols mentioned in prose. Don’t put large multi-line equations inline, they crowd the text; use display math.

Common Constructs#

SourceMeaning
\frac{a}{b}fraction ab\frac{a}{b}
\sum_{i=1}^{n} x_isum from 1 to n
\alpha, \beta, \gamma, \mu, \sigmaGreek letters
x_i / x^2subscript / superscript
X \sim N(\mu, \sigma^2)“distributed as” (\sim)
\bar{x}, \hat{\theta}bar and hat accents
\begin{bmatrix} a & b \\ c & d \end{bmatrix}a matrix

An example combining several of these:

Latex
$$
\bar{x} = \frac{1}{n} \sum_{i=1}^{n} x_i,
\qquad
X_i \sim N(\mu, \sigma^2)
$$

A Tiny Compilable Document#

A complete, minimal .tex file you can compile to PDF:

Latex
\documentclass{article}
\begin{document}

The sample mean is $\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i$.

\begin{equation}
\hat{\sigma}^2 = \frac{1}{n-1}\sum_{i=1}^{n}(x_i - \bar{x})^2
\end{equation}

\end{document}

Using LaTeX from R#

You do not need a system-wide LaTeX install. The tinytex R package installs a lightweight, self-contained distribution:

R
install.packages("tinytex")
tinytex::install_tinytex()   # one-time: sets up LaTeX for PDF rendering

With that in place, R Markdown and Quarto can render PDFs. Math goes directly in the document using the same $...$ syntax:

Markdown
---
title: "My Analysis"
output: pdf_document      # R Markdown; use `format: pdf` for Quarto
---

The mean is $\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i$, and we assume
$X_i \sim N(\mu, \sigma^2)$.

```{r}
mean(c(1, 2, 3, 4))
```

Render from the R console or the command line:

Shell
Rscript -e 'rmarkdown::render("analysis.Rmd")'
quarto render analysis.qmd --to pdf

LaTeX from Python, Jupyter, and Julia#

Journal Templates#

For submitting to journals, the rticles R package provides ready-made LaTeX templates for many publishers (PLOS, Elsevier, IEEE, and more). You write in R Markdown and get a correctly formatted PDF, no need to reverse-engineer a journal’s style file.

R
install.packages("rticles")
# In RStudio: New R Markdown -> From Template -> pick a journal