Skip to main content

15  Options

There’s a lot going on when you set a Quarto option. You need to:

In this chapter, you’ll learn more about setting options in Quarto so you can nail each of those steps.

You’ll start by learning how to find out what options are available, and where to put them. Then, you’ll learn more about YAML syntax, and how to find the values an option takes. Finally, you’ll see some ways things can go wrong, along with hints for fixing the issue.

TodoTO-DO

[CVW] Add an icon for error examples in the PDF version, since the red border doesn’t carry over.

Some examples in this chapter will result in an error from quarto render—we’ve highlighted these examples with a red border, like this:

bad-yaml.qmd
---
title: Bad YAML : (
---

The error messages shown in the text are current as of Quarto 1.11.9.

TodoTO-DO

Remove the callout above once we build the whole book, and examples, with a single Quarto version.

15.1 Finding an option

The best place to find out what options are available is the Reference section of the Quarto documentation. These pages are automatically generated from the YAML schema defined in the source code of the Quarto CLI.

In this section, you’ll learn how to use the Reference to find the name of an option, and where it can be set.

15.1.2 The Reference tells you where options can go

The scopes that define the sections of the Reference also map to where you write the option: execution options go inside a code cell, format options go in the document header, and project options go in _quarto.yml. This means the page that lists an option also describes where you can write it. For example, if you find an option listed on a format page, you can write it in the document header.

However, there are two reasons an option listed in one scope can sometimes be written in another:

  1. Some options are allowed in more than one scope, and consequently appear on more than one page in the Reference. For example, code-fold appears in both the Code Output section of Code Cells: Knitr and the Code section of HTML Options. That means you can write it inside a code cell or in the document header (assuming you are using the knitr engine and targeting HTML).

  2. You can increase the scope of any format option by moving it to _quarto.yml. Format options set in _quarto.yml apply to every document in the project. For example, you can also write code-fold in _quarto.yml and it will apply to every document in your project.

In practice, you write:

  • Project options in _quarto.yml.

  • Format options in the document header or _quarto.yml.

  • Execution options in code cells, or in the document header or _quarto.yml if the option is also listed on the Reference page for the format(s) you are targeting.

You’ll learn more about choosing the right location in Section 15.2.

NoteWebsites are a bit different

Most options in a website project follow the conventions described in this section: project options are listed on Website Options and format options on HTML Options. However, there are two exceptions:

  • Some options listed on the Website Options page under Projects are actually format options, specifically for HTML pages in a website. For example, listing and about go in the document header of the page they apply to, not in _quarto.yml (see Section 14.7 and Section 14.6).

  • Some project options have a format option counterpart that isn’t listed on the HTML Options page. For example, you set up navbar and sidebar under website in _quarto.yml, but you can hide them on a single page with navbar: false or sidebar: false in its document header (see Section 14.4.2 and Section 14.4.3).

15.1.3 Completing option names in your editor

If you are using RStudio, Positron, or VS Code with the Quarto extension, you can also get help with options from YAML intelligence. YAML intelligence includes two tools to help you get your YAML right: YAML completion and YAML validation.

YAML completion suggests code completions when you are editing YAML in your Quarto documents and projects. You can hit a shortcut to see suggested completions of option names and values (see Section 2.2.16 for Positron and Section 2.3.14 for RStudio).

Use completion:

  • At the top level without typing anything, to remind yourself of available options.
  • When you have started typing an option name, to avoid misspelling it. If the option doesn’t appear as a completion, check that you have it nested correctly.

You’ll use completion again to find values in Section 15.4, and meet YAML validation in Section 15.5.4.

15.2 Choosing where to put an option

The Reference tells you where an option can be written, but you still have some decisions to make. In this section, you’ll focus on format options first. Format options can be tricky because you can write them in several places. For example, the toc option might appear:

  • at the top level of a document header:

    document.qmd
    ---
    title: My document
    format: html
    toc: true
    ---
  • nested under a format in the document header:

    document.qmd
    ---
    title: My document
    format:
      html:
        toc: true
    ---
  • or in _quarto.yml:

    _quarto.yml
    toc: true

All three set a table of contents, but they differ in which formats and which documents they apply to. You’ll learn how to make the two key decisions behind these choices:

  • Whether to nest a format option under the format key (Section 15.2.1).
  • Whether to move a format option from the document header to _quarto.yml (Section 15.2.2).

Then, in Section 15.2.3, you’ll see that execution options follow the same rules once you move them out of a code cell.

15.2.1 Targeting formats

In Chapter 5, you saw “under a format key” as one of three places to set options. To understand what that means, and when to do it, you’ll dive into the following principles:

  • Everything is a format option
  • Top-level options are applied to all formats

15.2.1.1 Everything is a format option

Consider the following document header:

document.qmd
---
title: My document
toc: true
format: html
---

From Quarto’s perspective, this is equivalent to the header:

document.qmd
---
format:
  html:
    title: My document
    toc: true
---

That is, the options title and toc are HTML format options. It’s convenient to be able to put things at the top level, but internally, Quarto moves those options under each format. This is why the Reference pages in the Quarto documentation are laid out by format—options are supported, or not, depending on the output format.

15.2.1.2 Top-level options are applied to all formats

Quarto assumes any options you specify at the top level should be applied to all formats. For example, if you extend the document header to add the pdf format:

document.qmd
---
title: My document
toc: true
format:
  html: default
  pdf: default
---

Quarto interprets this as:

format:
  html:
    title: My document
    toc: true
  pdf:
    title: My document
    toc: true

This is a good example of why this behavior exists—you’ll want many options to be the same across formats, and being able to specify them at the top level saves a lot of typing.

Option values nested under a format will override any specified at the top level. For example, you might want toc: true for most formats, but override it for revealjs:

document.qmd
---
title: My document
toc: true
format:
  html: default
  pdf: default
  revealjs:
    toc: false
---

There are a couple of consequences of this behavior to be aware of:

  • Options are silently ignored by formats that don’t support them. While this is true regardless of where you set the option, it can be harder to remember when you set options at the top level. For example, you can set code-fold: true at the top level, and it will work great for format: html, but it isn’t supported by format: pdf.

  • YAML completion will suggest options at the top level that are supported by at least one format, but that might not be the format you are targeting. For example, the completion fig-pos is suggested at the top level, but it only applies to format: pdf.

Both are reminders that it’s up to you to check the Reference to see whether a format supports an option.

15.2.1.3 Takeaways

These principles describe how Quarto interprets your options, but don’t provide much guidance on when to nest options under format.

If you are only targeting one format, whether you nest options under format has no practical consequence; it’s a matter of style. A common convention is to keep document metadata (e.g., title, author, and date) at the top level and nest everything else under format.

When you are targeting more than one format, the decision is more clear-cut: put options that apply to all formats at the top level, and nest options that apply to a single format under that format.

15.2.2 Moving options to _quarto.yml

Project options aren’t the only thing you might find in _quarto.yml. You can move any options you’ve set in the document header to _quarto.yml and they’ll apply to all documents in your project. You won’t find format options listed on the Projects pages of the Reference, but they can always go in _quarto.yml.

For example, if you want all documents in the project to have a table of contents, you could include toc: true in your _quarto.yml file:

_quarto.yml
toc: true

Options specified in _quarto.yml follow the same principles as those in the document header. If you set toc: true at the top level, it applies to all formats. Alternatively, you could set it for a specific format, e.g., pdf, by nesting it under the format:

_quarto.yml
format:
  pdf:
    toc: true

As discussed in Section 13.5.2, you can override any options in _quarto.yml at the document level by setting them in the document header. However, if a document header sets format, Quarto renders only the formats listed there, even if _quarto.yml lists others. If you override options for one format, list every format you want rendered, using default for formats with no overrides:

document.qmd
---
format:
  html:
    toc: false
  pdf: default
---
TipOptions for a directory: _metadata.yml

To set options for only the documents in one directory (and its subdirectories), put them in a _metadata.yml file in that directory. Options in _metadata.yml override those in _quarto.yml, and options in a document header override both. Learn more in Metadata Merging in the Quarto documentation.

15.2.3 Execution options

Execution options can always go in a code cell. When an execution option is also a format option for your target formats, you can move it from a code cell to the document header and it will apply to all code cells in the document. For example, you could move echo: false out of a code cell and into the document header:

document.qmd
---
title: My document
echo: false
---

You can override the document-level setting by setting the option in a code cell:

document.qmd
```{r}
#| echo: true

```

When you move an option in this way it behaves just like a format option: if you nest it under a format, it will only apply to that format. And, just like a format option, you can move it to _quarto.yml to apply it to all code cells in a project.

For example, to hide the code in all code cells, for all formats, in a project, set echo: false at the top level of _quarto.yml:

_quarto.yml
echo: false

You can then override this at the document or cell level as usual.

NoteThe execute key

In the Quarto documentation, and in other people’s projects, you’ll see some execution options nested under an execute key:

_quarto.yml
execute:
  echo: false

This does the same thing as echo: false at the top level. We leave execute out because it’s one more thing to get right, and you don’t need it.

NoteKnitr-specific options

Some execution options, like message, collapse, and prompt, are specific to the knitr engine and don’t have format option equivalents. To set them for all code cells, nest them under knitr: opts_chunk in the document header. You’ll find the details in Section 7.3.1.

15.3 Writing an option

In this section, you’ll learn the necessary YAML fundamentals to avoid syntax errors when setting options. It picks up where the essentials in Chapter 5 left off. Although the syntax applies generally to any YAML, all the examples you’ll see are valid Quarto options and values.

You write the same YAML wherever you put an option: in _quarto.yml, in the document header, or in a code cell. The only difference is that, in a code cell, you write options using YAML syntax after a special comment:

document.qmd
```{r}
#| echo: false

```

You need to set code cell options before any code. Start each line with the comment character for the code cell language (# for R and Python), followed by a pipe (|) and a space.

We’ve organized this section by the shape a value can take, using the names you’ll see in the Reference and in Quarto’s error messages: string, number, boolean, array, and object. Each type also has a name in the YAML specification, and we’ll mention it once so you can connect what you learn here to other YAML documentation.

15.3.1 Strings, numbers, and booleans

The simplest values are strings, numbers, and booleans:

document.qmd
---
title: My document  # A string
toc: true           # A boolean, `true`/`false`
toc-depth: 2        # A number
---

YAML calls these simple values scalars.

TipYAML comments

The hash symbol (#) starts a comment, which runs to the end of the line.

The only values recognized as boolean are true and false; numbers can be integers (2), decimals (2.1), or in scientific notation (2.1e+2), and anything else is treated as a string.

15.3.1.1 Quotes

Strings don’t require quotes, but you may need them if your string contains characters that have special meaning in YAML (like # or :), or is something that would otherwise be interpreted as a number or boolean:

document.qmd
---
title: "Quotes: and things that need them"
author: "#quarto"
subtitle: "100"
---

In YAML, double-quoted strings (") interpret escape sequences, like \t, whereas single-quoted strings (') are interpreted literally:

double.qmd
---
title: "Decay time: $\tau$"
---
single.qmd
---
title: 'Decay time: $\tau$'
---
A screenshot of a title that reads 'Decay time: $ au$', with a gap between the first dollar sign and 'au'.
(a) Rendered title using double quotes (")
A screenshot of a title that reads 'Decay time:' followed by the Greek letter tau.
(b) Rendered title using single quotes (')
Figure 15.1

In double quotes, YAML reads \t as a tab, so the math is broken before Quarto ever sees it (Figure 15.1 (a)). In single quotes, \tau survives and renders as the Greek letter (Figure 15.1 (b)). Use single quotes when your string has backslashes you want to keep, like TeX math.

15.3.1.2 Long strings

For strings that are longer than you’d comfortably put on a single line, or need explicit line breaks, you can use a multi-line string, what YAML calls a block scalar .

There are two styles: literal and folded.

A pipe (|) starts a literal string, which keeps line breaks, whereas a greater-than symbol (>) starts a folded string, which collapses line breaks to spaces.

In both cases, start your string on a new line indented under the key:

literal.qmd
---
title: |
  A title

  spanning two lines
---
folded.qmd
---
title: >
  A title

  collapsed into one line
---
A screenshot of a title that reads 'A title' then a line break followed by 'spanning two lines'.
(a) Rendered title using a literal (|) string
A screenshot of a title that reads 'A title collapsed into one line' all on a single line.
(b) Rendered title using a folded (>) string
Figure 15.2

Depending on where the string value is used, line breaks may or may not mean anything to Quarto. For instance, when interpreted as markdown, a single line break doesn’t result in anything in the output. However, a double line break will be interpreted as a new paragraph.

15.3.2 Arrays

An array gives a key an ordered set of values. In YAML terms, an array is a sequence. There are two ways to write an array. When the values in the array are simple, separate them with commas inside square brackets:

document.qmd
---
theme: [cosmo, styles.scss]
---

The alternative syntax puts each value on its own line, starting with a dash and a space:

document.qmd
---
theme:
  - cosmo
  - styles.scss
---

15.3.3 Objects

An object is a collection of key-value pairs, each written as key: value. In YAML terms, an object is a mapping. Values can be simple, like a string, boolean, or number, or more complicated, like another object. When the value of a key is another object, YAML uses indentation to keep track of scope. For example, here the value of the author key is an object, setting name to the value Soraya Drake:

document.qmd
---
title: My document
author:
  name: Soraya Drake
---

It doesn’t matter how many spaces you indent as long as you use spaces (as opposed to tabs), and use the same number consistently. A common style convention, and the one we follow in this book and in the Quarto documentation, is to use two spaces for each level of indentation. For example, the name key nested under author is indented two spaces, whereas the name key nested under affiliation, which itself is nested under author, is indented four spaces:

document.qmd
---
title: My document
author:
  name: Soraya Drake
  affiliation:
    name: Everpine University
    url: http://everpine.edu
---

The names you can use in Quarto, along with the types of values they take, are not specified by the YAML language, but by Quarto’s own schema. That’s the schema behind the Reference pages you saw in Section 15.1.

15.3.4 Arrays of objects

The dash syntax for arrays also works when the values are objects. For example, here the value of author is an array of length two, where each element is an object with name and affiliation keys:

document.qmd
---
title: My document
author:
  - name: Soraya Drake
    affiliation: Everpine University
  - name: Zahir Kamarov
    affiliation: Peregrine Heights Institute
---

15.3.5 Using default

You’ll come across examples where the value of a Quarto option is default, like when you are specifying multiple formats:

document.qmd
---
format:
  html: default
  pdf: default
---

The value default doesn’t have a special YAML meaning—it’s something Quarto adds for certain options where the value is usually an object, but you just want the default values for all its properties.

15.4 Choosing a value

The Reference is the place to start for the values an option takes, but it’s often brief. You’ll usually need to combine a few sources. With each one, you are looking for the shape of the value, so you can write it using the syntax in Section 15.3:

  1. Read the option’s description in the Reference. For example, the description of code-fold lists its three values: true, false, and show.

  2. Use YAML completion (Section 15.1.3) after completing the option name, to get suggested values. This is particularly useful for options that take one of a few special strings.

  3. Let a validation error tell you the type Quarto expected. You’ll see an example for toc in Section 15.5.

  4. Look for an example in the Guide section of the Quarto documentation. This is your best bet when the value has some structure to it, like an array of objects.

The last two often work together. Not all validation error messages are as illuminating as you’d hope. For example, consider trying to add a link with other-links like this:

other-links-string.qmd
---
title: "Untitled"
format:
  html:
    other-links: http://quarto.org
---

The validation error returned is:

ERROR: Validation of YAML front matter failed.
ERROR: In file other-links-string.qmd
(line 5, columns 18--34) Field "other-links" has value http://quarto.org, which must instead be 'false'
4:   html:
5:     other-links: http://quarto.org
                   ~~~~~~~~~~~~~~~~~
6: ---

ERROR: Render failed due to invalid YAML.

It’s clear where the error is ((line 5, columns 18--34) Field "other-links"), and that the wrong type of value has been encountered. But it’s unclear what Quarto actually wants. It says it was expecting false, but that doesn’t help you figure out how to specify a link.

TipMessages that are too brief

Quarto tries not to overwhelm you with output in an error message, but sometimes shortening a message makes it less helpful.

Messages that end with which must instead be x are often too brief to help. Quarto reports one value, x, often the first alphabetically, but in reality the option could take many values.

For validation errors like this, look for an example in the Guide. The Code Links and Other Links section of the Quarto documentation has an example that shows Quarto was expecting an array of objects—in YAML terms, a sequence of mappings:

document.qmd
---
title: "Untitled"
format:
  html:
    other-links:
      - text: Quarto
        href: https://quarto.org
---

15.5 When it goes wrong

When you render a project or document, one of the first things Quarto does is parse and validate the options. When you make a mistake in setting an option, there are a few possible outcomes:

  1. You get an error message, and the error message clearly points out your mistake.

  2. You get an error message, but it’s not clear from that message what or where your mistake is.

  3. You don’t get an error message at all.

It’s hard to enumerate all the ways you can make a mistake, but we’ll give a couple of examples for each of these outcomes in the hope it helps you track down yours.

We’ve split the examples by the kind of mistake you make:

  • Using the wrong YAML syntax
  • Using the wrong option name
  • Using the wrong type of value

15.5.1 Using the wrong YAML syntax

When Quarto can’t parse your YAML, you get a YAMLException error. These are surprisingly rare, not because everyone is great at YAML, but because most YAML syntax mistakes don’t result in invalid YAML. The YAML remains valid; it just won’t be interpreted in the way you intended.

For example, if you forget the space after a colon when setting an option like code-fold:

code-fold-no-space.qmd
---
title: "Untitled"
format:
  html:
    code-fold:true
---

Then you don’t get a YAMLException but instead a validation error:

ERROR: Validation of YAML front matter failed.
ERROR: (line 5, columns 5--18) Field "html" has value code-fold:true, which must instead be an object

✖ The value code-fold:true is a string.
ℹ The error happened in location format:html.
ℹ In YAML, key-value pairs in objects must be separated by a space.
ℹ Did you mean code-fold: true instead?

ERROR: Render failed due to invalid YAML.

The YAML has been parsed, but without the space, Quarto reads code-fold:true as a string, not an object. Quarto does a good job here of suggesting what your mistake might have been and a solution.

Generally, if you get a validation error, first check your syntax carefully.

15.5.2 Using the wrong option name

Sometimes, when you use the wrong name for an option, you get an error. For example, if you pluralize the lightbox option loop:

lightbox-loops.qmd
---
title: "Presentation"
format: html
lightbox: 
  loops: true 
---

You’ll get a YAML validation error that catches and reports the problem:

ERROR: Validation of YAML front matter failed.
ERROR: In file lightbox-loops.qmd
(line 5, columns 3--8) property name loops is invalid
4: lightbox: 
5:   loops: true 
    ~~~~~~
6: ---
ℹ The error happened in location lightbox:loops.
ℹ Did you mean loop?

ERROR: Render failed due to invalid YAML.

The lightbox option has a closed schema: Quarto allows only a fixed set of property names under it. However, not all Quarto options have closed schemas.

When the schema is open, Quarto simply accepts an unknown option name and you get no error. For example, you might use an underscore instead of a hyphen in code-fold:

document.qmd
---
title: My document
format:
  html:
    code_fold: true
---

Even though code_fold isn’t a recognized option for html, you’ll get no error. But you also won’t get any code folding. Why does Quarto have open schemas? There are lots of places where it makes sense to let users define their own keys, for example, when you want to use the meta shortcode and define some custom keys in your metadata.

A similar problem occurs when you nest options incorrectly. For example, you might get code-fold right, but forget to nest html under format:

document.qmd
---
title: My document
html:
  code-fold: true
---

As a top-level option, html isn’t something Quarto knows about, but it passes validation. The result is no error, and no folded code.

YAML completion (Section 15.1.3) can help you spot these problems. If you aren’t getting the completion you expect, perhaps you’ve got the wrong name, or you’ve nested it under the wrong option.

15.5.3 Using the wrong type of value

Checking the type of value you’ve passed is something Quarto is good at, assuming your syntax is correct and you’ve got the right option name.

For example, consider trying to add a table of contents like this:

toc-yes.qmd
---
title: My document
toc: yes
---

Upon render, Quarto returns the error:

ERROR: Validation of YAML front matter failed.
ERROR: (line 3, columns 6--8) Field "toc" has value yes, which must instead be `true` or `false`

✖ The value yes is a string.
ℹ The error happened in location toc.
ℹ Quarto uses YAML 1.2, which interprets booleans strictly.
ℹ Try using true instead.

ERROR: Render failed due to invalid YAML.

This is a really useful message—it tells you the YAML front matter validation failed; it tells you where the problem is, (line 3, columns 6--8) Field "toc"; and it tells you what the problem was: Quarto expected a boolean, but got a string. It even suggests a solution: Try using true instead.

When the message isn’t this helpful, like the one for other-links you saw in Section 15.4, look for an example in the Guide.

15.5.4 Catching mistakes with validation

You don’t have to wait for a render to see these errors. YAML validation, the other half of YAML intelligence (Section 15.1.3), runs the same checks as a render, but shows the results in your editor, where you are working. It runs when you save your file.

It will warn you when:

  • a value has the wrong type
  • a value isn’t one of the allowed values
  • the name of an option isn’t valid (but only where the schema is closed)
  • indentation is inconsistent

15.6 Wrapping up

In this chapter, you learned how to handle each step of setting a Quarto option.

To find an option’s name, look it up in the Reference (Section 15.1). The page that lists an option also tells you where you can write it: project options go in _quarto.yml, format options go in the document header or _quarto.yml, and execution options go in a code cell, or in the document header or _quarto.yml if they are also listed for your format. YAML completion can remind you of option names as you type.

When an option can go in more than one place, you decide where it goes (Section 15.2). Leave a format option at the top level to apply it to all formats, or nest it under a format to target only that format. Move it to _quarto.yml to apply it to every document in a project.

To write an option, use the YAML syntax that matches the shape of its value: a string, number, boolean, array, or object (Section 15.3). To choose a value, combine the Reference, YAML completion, validation errors, and examples in the Guide (Section 15.4).

Quarto’s validation catches many mistakes, like the wrong type of value, and YAML validation in your editor shows them before you render (Section 15.5). But some mistakes pass validation: a misspelled option name, or an option nested in the wrong place, is silently ignored.

If your YAML is right and the Reference says the option belongs where you put it, but you still don’t get what you expect, the next chapter can help. In Chapter 16, you’ll learn what Quarto does when it renders a document, and how to use that to track down a problem.