R Markdown to PDF: Knit .Rmd Files and Fix LaTeX Errors

November 11, 2026 · 9 min read

R Markdown to PDF: Knit .Rmd Files and Fix LaTeX Errors

Turning R Markdown to PDF takes three pieces: knitr runs your R chunks, Pandoc writes a LaTeX file, and a LaTeX engine compiles it. Install TinyTeX once, set output: pdf_document, and click Knit. This guide also covers scripted rendering, choosing an engine, and the fix behind each common error message.

Before You Knit: Pandoc, LaTeX and TinyTeX

RStudio ships with Pandoc, so the missing piece is almost always LaTeX. TinyTeX, maintained by Yihui Xie, is the lightest option for R users, and it installs without admin rights. Run this once in the R console:

install.packages("tinytex")
tinytex::install_tinytex()

The first line installs the tinytex R package. The second downloads the TinyTeX distribution itself, after which tinytex::is_tinytex() returns TRUE. If you render outside RStudio, install Pandoc separately as well, and check that R can see it with rmarkdown::pandoc_available(). For how knitr, Pandoc and LaTeX pass the file along, see what R Markdown is and how its pipeline works.

How Do You Knit an Rmd to PDF in RStudio?

Set the output format in the YAML header, then knit. Here's a minimal file, saved as report.Rmd:

---
title: "Sales Summary"
output: pdf_document
---

## Monthly totals

```{r}
summary(cars)
```

Click the small arrow next to Knit and choose Knit to PDF. RStudio runs the chunk in a fresh R session and writes report.pdf into the same folder. The PDF shows the title, the heading, the chunk's code and the summary table for the built-in cars dataset.

Every other pdf_document setting (table of contents, fonts, margins, header-includes) is covered in our R Markdown YAML header guide. This post only touches the YAML that fixes errors. For bullet styles in the PDF, see our R Markdown bullet points guide.

Rendering R Markdown to PDF from the Console or a Script

The Knit button calls rmarkdown::render() behind the scenes. You can call it yourself, which helps when you rebuild reports on a schedule:

rmarkdown::render(
  "report.Rmd",
  output_format = "pdf_document",
  output_file = "sales-2026-q3.pdf",
  output_dir = "out"
)

That writes out/sales-2026-q3.pdf. The output_format argument overrides the YAML, so the same file can still knit to HTML in RStudio.

For one PDF per group, declare a parameter such as region under params: in the YAML, read it as params$region in your chunks, and loop:

for (r in c("North", "South", "West")) {
  rmarkdown::render(
    "report.Rmd",
    output_format = "pdf_document",
    params = list(region = r),
    output_file = paste0("report-", r, ".pdf"),
    envir = new.env()
  )
}

You get three PDFs from one source. The envir = new.env() line keeps objects from one run leaking into the next. From a terminal or a CI job, skip the R prompt entirely:

Rscript -e "rmarkdown::render('report.Rmd', 'pdf_document')"

The second argument is output_format, passed by position. Add quiet = TRUE to any of these calls when you want a cleaner log, since it suppresses knitr's progress output and the Pandoc command line.

pdflatex, xelatex or lualatex: Choosing the LaTeX Engine

pdf_document uses pdflatex unless you say otherwise. It's quick and fine for plain English text. Switch to xelatex or lualatex when the text includes Greek, Cyrillic or symbols like ≥. You also need one of them to use a font installed on your system:

---
title: "Sales Summary"
mainfont: "DejaVu Serif"
output:
  pdf_document:
    latex_engine: xelatex
---

With this header, xelatex sets the body text in DejaVu Serif, provided that font is installed. The mainfont, sansfont and monofont variables only work with xelatex and lualatex, so they do nothing under pdflatex. Chinese, Japanese or Korean text needs one more variable, CJKmainfont, set to an installed CJK font.

We default to xelatex for any report that might contain names or places from outside English. It can be slower than pdflatex, but it removes a whole class of Unicode errors before they happen.

Why Does R Markdown Say LaTeX Failed to Compile?

When the engine stops, the knit ends with a line like this:

Error: LaTeX failed to compile report.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See report.log for more info.

Pandoc produced the .tex file fine; LaTeX choked on it. The actual cause is printed just above, in lines starting with !, such as ! LaTeX Error: File ... not found. Read those first. A ! Missing $ inserted. line means LaTeX met a math-only command, such as \times, outside math mode. When the culprit looks like inline R output, tinytex suggests wrapping that inline expression in dollar signs.

If the cause isn't obvious, follow the TinyTeX debugging steps in order:

  1. Update everything with update.packages(ask = FALSE, checkBuilt = TRUE), then tinytex::tlmgr_update().
  2. If tlmgr reports "Remote repository is newer than local", your TinyTeX is too old. Run tinytex::reinstall_tinytex().
  3. Put options(tinytex.verbose = TRUE) in a chunk with include=FALSE and knit again to see the full LaTeX output.

To read the LaTeX source itself, add keep_tex: true under pdf_document. The .tex file then stays next to the PDF, and the line numbers in the error point into it.

Fixing Missing .sty Files, Fonts and Unicode Characters

Three LaTeX errors come up again and again. The first is a missing package:

! LaTeX Error: File `ocgbase.sty' not found.

TinyTeX normally installs missing packages on its own mid-knit. You mostly see this error on a shared TeX Live you can't write to, on an offline machine, or with MiKTeX. With TinyTeX, hand the log or the error text to tinytex:

tinytex::parse_install("report.log")
tinytex::parse_install(text = "! LaTeX Error: File `ocgbase.sty' not found.")

Either call works out that ocgbase.sty belongs to the ocgx2 package and installs it. MiKTeX users can switch on "Always install missing packages on-the-fly" in the MiKTeX Console instead. Some table packages pull in extra LaTeX packages too; our R Markdown tables guide shows which ones work in PDF.

The second is a Unicode error from pdflatex:

! LaTeX Error: Unicode character ≥ (U+2265)
               not set up for use with LaTeX.

Older LaTeX installs word it as ! Package inputenc Error: Unicode char .... Either way, the fix is latex_engine: xelatex from the engine section. Recent tinytex versions print a hint suggesting xelatex when they spot this error.

The third appears after you switch engines and name a font:

! Package fontspec Error:
(fontspec)                The font "DejaVu Serif" cannot be found; this may
(fontspec)                be but usually is not a fontspec bug.

xelatex looks the name up among the fonts installed on the machine that knits, not the one you wrote the file on. Check the spelling against your system's font list, install the font, or pick one that ships everywhere. This one bites hardest on CI servers and colleagues' laptops.

What Do Pandoc Error 43 and Error 47 Mean?

pandoc document conversion failed with error 43 is rmarkdown passing on Pandoc's exit code. The Pandoc manual's exit code table defines both numbers:

CodePandoc nameWhat to do
43PandocPDFErrorThe LaTeX engine ran and failed. Read the ! lines, as above.
47PandocPDFProgramNotFoundErrorNo PDF engine found. Install TinyTeX or add your LaTeX bin folder to PATH, then restart RStudio.

Since rmarkdown 1.9, Pandoc only writes the .tex file for pdf_document, and R compiles it, through tinytex in current versions. These codes therefore show up mainly with very old rmarkdown versions or when you run Pandoc yourself. If you see them while knitting, update rmarkdown first.

A different message stops the render before Pandoc even starts:

Error: pandoc version 1.12.3 or higher is required and was not found (see the help page ?rmarkdown::pandoc_available).

The version number varies, but the meaning doesn't: R can't find Pandoc at all. It's common with Rscript in a terminal or a CI job, because only RStudio points R at its bundled copy. Install Pandoc so it's on your PATH, then rerun the render.

Knitting to PDF Without LaTeX Using pagedown

If LaTeX keeps fighting you, pagedown prints the HTML version of your report to PDF through Google Chrome, Microsoft Edge or Chromium. Its CRAN page lists Pandoc 2.2.3 or later as a requirement. Set the YAML to an HTML format such as html_document or pagedown::html_paged, then run:

install.packages("pagedown")
pagedown::chrome_print("report.Rmd")

chrome_print() renders the .Rmd to HTML and saves report.pdf beside it. Add knit: pagedown::chrome_print to the YAML and the Knit button produces both files. If the browser isn't found, set the PAGEDOWN_CHROME environment variable to its path, as the pagedown documentation describes.

The trade-off is typesetting. Page size, margins and running headers come from CSS, so latex_engine and LaTeX packages loaded through header-includes don't apply. Our page break guide covers forcing new pages in both routes.

Switching to Quarto doesn't remove LaTeX from the picture: quarto render report.qmd --to pdf still compiles through a TeX distribution, as our Quarto guide explains.

A document with no R chunks is just Markdown. Our Markdown to PDF converter turns that into an A4 PDF without any LaTeX install, but it doesn't run R code or read the YAML header.

Questions About Knitting .Rmd Files to PDF

Getting R Markdown to PDF working is mostly a one-time job. Install TinyTeX, switch to xelatex once your text goes beyond basic Latin, and read the lines starting with ! whenever a knit fails. If you like drafting the prose sections with a live preview, write them in our online Markdown editor, then paste them into your .Rmd and knit.