background picture with networks
🎁 Bonus #4

Dealing with PDFs

I'll be honest: I'm not a fan of PDFs 😳. No interactive charts, no fancy effects, none of the fun stuff Quarto can do. Still, PDFs are an industry standard. At some point, you’ll need to share your work in this format.

The good news? Quarto makes PDF creation painless. Let’s see how it works and a few tricks to make your life easier.

Members only
6 minutes read

🔥 Your First PDF

Creating a PDF with Quarto is quick and straightforward — let’s try it right now on your own laptop.

0
Open R Studio. Create a new Quarto file and save it.
1

In the YAML header, make sure the format attribute is set to pdf.

---
title: "test pdf"
format: pdf
---
2

Render the document!


That’s it!

Just a few seconds into this lesson, you should already have your very first PDF report like this. 🎉

⚙️ How Does It Work?

When you render a Quarto document, Quarto first runs your R code and converts the results into a Markdown (.md) file. If your script produces a graph, that graph is saved as an image and embedded in the Markdown.

Then comes the magic step: Quarto calls a tool named Pandoc, a “universal translator” that converts Markdown into many different formats, such as PDF, HTML, or Word (.docx).


Diagram showing the steps to go from a Quarto document to PDF, HTML, or Word.

How Quarto uses Pandoc to turn a .qmd file into different output formats.


For HTML and Word, Pandoc can produce the output directly. But for PDF, Pandoc needs help from a typesetting engine — a system that decides how text, images, and other elements are placed and styled on the page.

Think of typesetting as the step that handles page layout: line breaks, margins, fonts, spacing, math formulas, figure placement, and more.

Quarto lets you choose between two typesetting engines:

💪 Your First PDF with Typst

Switching to Typst is as easy as updating your document’s YAML header:

---
title: "test pdf"
format: typst
---

The output will look the same as LaTeX at first, but customizing your PDF will be much easier with Typst!

✨ Customization

Now that you know how to use typst, you probably want to actually customize your pdf report.

As for HTML, there are several levels and techniques to do so. Let's dive in:

1️⃣ YAML customization

Most of the options we described already for HTML output work for the PDF format too. And a few other ones are available, like column to split your text on columns.

---
title: "Hello Typst!"
format:
  typst:
    toc: true
    section-numbering: 1.1.a
    columns: 2
    papersize: a5
    margin:
      x: 1cm
      y: 1cm
---

2️⃣ Inline customization

You can change the style of a specific section in your document while editing directly in your .qmd file.

This is possible using the typst syntax, but Quarto offers an awesome option: using css to custom inside the PDF. For instance, this will highlight a specific piece of text using the very common background-color css property!

Here is a [span with a green background]{style="background-color:green"}.

3️⃣ external file customization

To do so we'll create a file called custom.typ in the same folder and write some customization rules in it. At the end of the day, it's a bit like writing some css code to customize an html file as explained in the css lesson.

I really like having more space between sections in my document. So here is a simple rule to add more space on top of titles:

#set heading(
  spacing: 2em,        // more space above the heading
  fill: rgb("#69b3a2") // custom color
)

Then, you need to modify the YAML header of your .qmd file to actually use this styling file:

format:
  typst:
    template: custom.typ

That’s it! Render your document again and your headings will now appear in#69b3a2 with extra breathing room above them.

4️⃣ prebuilt template

background picture with networks
Productive R Workflow

This lesson is for members only! 😔
Become a member now to access the full course with a lifetime access!

Or Login

← Previous

Parameterized Reports

Next →

Dealing with PDFs