Cheatsheets:Quarto: Difference between revisions

From Wikibase
Jump to navigation Jump to search
mNo edit summary
Rewrite per style guide: source/rendered side-by-side basic example, fix multi-format syntax (map, not list), move notes into their sections, add document-wide execute options and Projects section (via update-page on MediaWiki MCP Server)
Line 12: Line 12:
</blockquote>
</blockquote>


== Basic example ==
== Basic example: Pancake recipe ==


A <syntaxhighlight lang="text" inline>.qmd</syntaxhighlight> file: front matter between <syntaxhighlight lang="text" inline>---</syntaxhighlight> lines, then Markdown with a Python chunk:
A <syntaxhighlight lang="text" inline>.qmd</syntaxhighlight> file: YAML front matter between <syntaxhighlight lang="text" inline>---</syntaxhighlight> lines, then Markdown with a Python chunk. Render it with <syntaxhighlight lang="bash" inline>quarto render pancake.qmd</syntaxhighlight>:


{| class="wikitable"
! Source (.qmd) !! Rendered (html)
|-
|
<syntaxhighlight lang="markdown">
<syntaxhighlight lang="markdown">
---
---
Line 30: Line 34:


```{python}
```{python}
# | echo: false
#| echo: false
flour = 150
flour = 150
milk = 250
milk = 250
Line 36: Line 40:
```
```
</syntaxhighlight>
</syntaxhighlight>
 
||
Render it:
 
<syntaxhighlight lang="bash">
quarto render pancake.qmd
</syntaxhighlight>
 
The chunk runs at render time; with <syntaxhighlight lang="yaml" inline>echo: false</syntaxhighlight> the source stays hidden and only the output appears in the document:
 
<blockquote>
<blockquote>
'''Ingredients'''
<p style="font-size:2em;font-weight:bold;">Pancake quantities</p>
* 150 g flour
<p style="font-size:1.5em;font-weight:bold;">Ingredients</p>
* 250 ml milk
<ul>
 
<li>150 g flour</li>
'''Batter volume'''
<li>250 ml milk</li>
</ul>
<p style="font-size:1.5em;font-weight:bold;">Batter volume</p>
<syntaxhighlight lang="text">
<syntaxhighlight lang="text">
Batter volume: 400 ml
Batter volume: 400 ml
</syntaxhighlight>
</syntaxhighlight>
</blockquote>
</blockquote>
|}
The chunk runs at render time. With <syntaxhighlight lang="yaml" inline>echo: false</syntaxhighlight> the source stays hidden; only the output appears in the document.


== Front matter ==
== Front matter ==
Line 79: Line 80:
| <syntaxhighlight lang="yaml" inline>date: today</syntaxhighlight> || date; the magic value <syntaxhighlight lang="yaml" inline>today</syntaxhighlight> becomes the current date at render time
| <syntaxhighlight lang="yaml" inline>date: today</syntaxhighlight> || date; the magic value <syntaxhighlight lang="yaml" inline>today</syntaxhighlight> becomes the current date at render time
|-
|-
| <syntaxhighlight lang="yaml" inline>format: html</syntaxhighlight> || output format; several at once: <syntaxhighlight lang="yaml" inline>[html, pdf]</syntaxhighlight>
| <syntaxhighlight lang="yaml" inline>format: html</syntaxhighlight> || output format
|-
|-
| <syntaxhighlight lang="yaml" inline>toc: true</syntaxhighlight> || table of contents
| <syntaxhighlight lang="yaml" inline>toc: true</syntaxhighlight> || table of contents
Line 85: Line 86:
| <syntaxhighlight lang="yaml" inline>lang: en</syntaxhighlight> || document language
| <syntaxhighlight lang="yaml" inline>lang: en</syntaxhighlight> || document language
|}
|}
Several formats at once — a '''map''', not a list. A list like <syntaxhighlight lang="yaml" inline>[html, pdf]</syntaxhighlight> fails at render time with <syntaxhighlight lang="text" inline>ERROR: Validation of YAML front matter failed</syntaxhighlight>. Declare each format instead:
<syntaxhighlight lang="yaml">
format:
  html: default
  pdf: default
</syntaxhighlight>
<blockquote>
'''A colon in an unquoted value must be quoted''' — it ends the mapping, and the render fails:
{| class="wikitable"
! Wrong !! Right
|-
|
<syntaxhighlight lang="yaml">
title: Pancakes: the recipe
</syntaxhighlight>
Render error:
<syntaxhighlight lang="text">
ERROR: YAMLException: bad indentation of a mapping entry
</syntaxhighlight>
||
<syntaxhighlight lang="yaml">
title: "Pancakes: the recipe"
</syntaxhighlight>
|}
</blockquote>


== Code chunks ==
== Code chunks ==
Line 98: Line 129:
Engines include python (via Jupyter), r (via knitr), julia, bash and ojs.
Engines include python (via Jupyter), r (via knitr), julia, bash and ojs.


'''Chunk options''' — lines starting with <syntaxhighlight lang="yaml" inline>#|</syntaxhighlight> at the top of the chunk configure it:
=== Chunk options ===
 
Lines starting with <syntaxhighlight lang="yaml" inline>#|</syntaxhighlight> at the top of the chunk configure it:


{| class="wikitable"
{| class="wikitable"
Line 132: Line 165:
||
||
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
# | echo: false
#| echo: false
flour = 150
flour = 150
milk = 250
milk = 250
Line 145: Line 178:


'''eval: false''' — the source is shown but nothing runs, handy for scratch code you want to keep visible.
'''eval: false''' — the source is shown but nothing runs, handy for scratch code you want to keep visible.
'''include: false''' — source and output both stay out of the document, for setup code the reader does not need to see.
<blockquote>
'''A fence without braces is a static code block — it never runs.''' A chunk that "does nothing" is usually this:
{| class="wikitable"
! Wrong !! Right
|-
|
<syntaxhighlight lang="markdown">
```python
print("hi")
```
</syntaxhighlight>
Nothing executes; the source shows as a plain code block.
||
<syntaxhighlight lang="markdown">
```{python}
print("hi")
```
</syntaxhighlight>
The engine runs it and inserts the output.
|}
The inline form follows the same rule — see [[#Inline code|Inline code]].
</blockquote>
=== Options for the whole document ===
Set a chunk option for every chunk at once with <syntaxhighlight lang="yaml" inline>execute:</syntaxhighlight> in the front matter:
<syntaxhighlight lang="yaml">
---
title: "Pancake quantities"
format: html
execute:
  echo: false
---
</syntaxhighlight>
Every chunk hides its source; the outputs still appear. A <syntaxhighlight lang="yaml" inline>#|</syntaxhighlight> option on one chunk overrides the document default.


== Inline code ==
== Inline code ==
Line 187: Line 264:
| <syntaxhighlight lang="yaml" inline>html</syntaxhighlight> || standalone HTML page
| <syntaxhighlight lang="yaml" inline>html</syntaxhighlight> || standalone HTML page
|-
|-
| <syntaxhighlight lang="yaml" inline>pdf</syntaxhighlight> || PDF, via LaTeX or the Typst engine
| <syntaxhighlight lang="yaml" inline>pdf</syntaxhighlight> || PDF via LaTeX
|-
|-
| <syntaxhighlight lang="yaml" inline>typst</syntaxhighlight> || PDF via the Typst engine bundled with Quarto
| <syntaxhighlight lang="yaml" inline>typst</syntaxhighlight> || PDF via the Typst engine bundled with Quarto
Line 197: Line 274:
| <syntaxhighlight lang="yaml" inline>gfm</syntaxhighlight> || GitHub Flavored Markdown
| <syntaxhighlight lang="yaml" inline>gfm</syntaxhighlight> || GitHub Flavored Markdown
|}
|}
== Projects ==
A directory with a <syntaxhighlight lang="text" inline>_quarto.yml</syntaxhighlight> file is a '''project''': documents share its front matter and render as one site. Scaffold one:
<syntaxhighlight lang="bash">
quarto create-project --type website
</syntaxhighlight>
creates <syntaxhighlight lang="text" inline>index.qmd</syntaxhighlight>, <syntaxhighlight lang="text" inline>about.qmd</syntaxhighlight>, <syntaxhighlight lang="text" inline>styles.css</syntaxhighlight> and <syntaxhighlight lang="text" inline>_quarto.yml</syntaxhighlight>. The scaffolded project file:
<syntaxhighlight lang="yaml">
project:
  type: website
website:
  title: "webproj"
  navbar:
    left:
      - href: index.qmd
        text: Home
      - about.qmd
format:
  html:
    theme:
      - cosmo
      - brand
    css: styles.css
    toc: true
</syntaxhighlight>
Render the whole project:
<syntaxhighlight lang="bash">
quarto render
</syntaxhighlight>
builds the site into <syntaxhighlight lang="text" inline>_site/</syntaxhighlight>.


== CLI ==
== CLI ==
Line 211: Line 327:
| <syntaxhighlight lang="bash" inline>quarto create-project --type website</syntaxhighlight> || scaffold a project (default, book, website, manuscript)
| <syntaxhighlight lang="bash" inline>quarto create-project --type website</syntaxhighlight> || scaffold a project (default, book, website, manuscript)
|-
|-
| <syntaxhighlight lang="bash" inline>quarto publish gh-pages</syntaxhighlight> || publish to GitHub Pages, Netlify or Quarto Pub
| <syntaxhighlight lang="bash" inline>quarto publish gh-pages</syntaxhighlight> || publish the built site to GitHub Pages; other providers: netlify, quarto-pub, …
|}
|}
<blockquote>
'''Gotcha:''' a fence without braces is a static code block — it never runs. A
chunk that "does nothing" is usually this:
<syntaxhighlight lang="markdown">
```python
print("hi")
```
</syntaxhighlight>
Fix: put the language in braces, then it executes:
<syntaxhighlight lang="markdown">
```{python}
print("hi")
```
</syntaxhighlight>
The inline form follows the same rule: <syntaxhighlight lang="markdown" inline>`{python} flour + milk`</syntaxhighlight> renders as 400, while <syntaxhighlight lang="markdown" inline>`flour + milk`</syntaxhighlight> renders the literal text flour + milk.
</blockquote>
<blockquote>
'''Gotcha:''' YAML values containing a colon must be quoted. Unquoted:
<syntaxhighlight lang="yaml">
title: Pancakes: the recipe
</syntaxhighlight>
fails at render time with
<syntaxhighlight lang="text">
ERROR: YAMLException: bad indentation of a mapping entry
</syntaxhighlight>
Fix: quote the value:
<syntaxhighlight lang="yaml">
title: "Pancakes: the recipe"
</syntaxhighlight>
</blockquote>


== Further reading ==
== Further reading ==

Revision as of 18:53, 19 August 2026

Languages: English · français · Esperanto

Quick reference for smart people — part of our dev cheatsheets collection.

Quarto is a scientific and technical publishing system: Markdown content, YAML front matter and executable code chunks that render to HTML, PDF, Word and more. It builds on Pandoc.

Quarto uses Markdown for prose content. If you are unfamiliar with Markdown syntax, see Cheatsheets:markdown.

New to Quarto? You may find this Get Started tutorial interesting.

Basic example: Pancake recipe

A .qmd file: YAML front matter between --- lines, then Markdown with a Python chunk. Render it with quarto render pancake.qmd:

Source (.qmd) Rendered (html)
---
title: "Pancake quantities"
format: html
---

## Ingredients

- 150 g flour
- 250 ml milk

## Batter volume

```{python}
#| echo: false
flour = 150
milk = 250
print(f"Batter volume: {flour + milk} ml")
```

Pancake quantities

Ingredients

  • 150 g flour
  • 250 ml milk

Batter volume

Batter volume: 400 ml

The chunk runs at render time. With echo: false the source stays hidden; only the output appears in the document.

Front matter

YAML between --- lines at the top of the file:

---
title: "Pancake quantities"
author: "Ada"
date: today
format: html
toc: true
---
Key Effect
title: "Pancakes" document title
author: Ada author; a list of authors: [Ada, Bo]
date: today date; the magic value today becomes the current date at render time
format: html output format
toc: true table of contents
lang: en document language

Several formats at once — a map, not a list. A list like [html, pdf] fails at render time with ERROR: Validation of YAML front matter failed. Declare each format instead:

format:
  html: default
  pdf: default

A colon in an unquoted value must be quoted — it ends the mapping, and the render fails:

Wrong Right
title: Pancakes: the recipe

Render error:

ERROR: YAMLException: bad indentation of a mapping entry
title: "Pancakes: the recipe"

Code chunks

A fenced block whose info string names a language in braces runs as code; the engine executes it and inserts the output:

```{python}
print("hi")
```

Engines include python (via Jupyter), r (via knitr), julia, bash and ojs.

Chunk options

Lines starting with #| at the top of the chunk configure it:

Option Effect Example
echo show the source (default true) #| echo: false
eval run the code (default true) #| eval: false
include keep source and output in the document (default true) #| include: false
label chunk label, referenced with @label #| label: fig-batter
fig-cap caption for figure output #| fig-cap: "Batter volume"

echo — without it source and output both appear; with echo: false only the output:

Without (default) With echo: false
flour = 150
milk = 250
print(f"Batter volume: {flour + milk} ml")

Output:

Batter volume: 400 ml
#| echo: false
flour = 150
milk = 250
print(f"Batter volume: {flour + milk} ml")

Output:

Batter volume: 400 ml

eval: false — the source is shown but nothing runs, handy for scratch code you want to keep visible.

include: false — source and output both stay out of the document, for setup code the reader does not need to see.

A fence without braces is a static code block — it never runs. A chunk that "does nothing" is usually this:

Wrong Right
```python
print("hi")
```

Nothing executes; the source shows as a plain code block.

```{python}
print("hi")
```

The engine runs it and inserts the output.

The inline form follows the same rule — see Inline code.

Options for the whole document

Set a chunk option for every chunk at once with execute: in the front matter:

---
title: "Pancake quantities"
format: html
execute:
  echo: false
---

Every chunk hides its source; the outputs still appear. A #| option on one chunk overrides the document default.

Inline code

Inline code executes when the language is named in braces:

Source Renders as
`{python} flour + milk` 400
`flour + milk` flour + milk (literal, not evaluated)

Cross-references

Give a section, table or figure an identifier, then refer to it with @:

## Recipe data {#sec-data}

The amounts live in @sec-data; see Table @tbl-pan.

| Ingredient | Amount |
|---|---|
| Flour | 150 g |

: Pancake ingredients {#tbl-pan}

The reference @sec-data renders as "Section 1", the table caption becomes "Table 1: Pancake ingredients", and figures are referenced the same way with @fig-label.

Formats

The format key picks the output. HTML and PDF work out of the box:

format output
html standalone HTML page
pdf PDF via LaTeX
typst PDF via the Typst engine bundled with Quarto
docx Word document
revealjs HTML slide deck
gfm GitHub Flavored Markdown

Projects

A directory with a _quarto.yml file is a project: documents share its front matter and render as one site. Scaffold one:

quarto create-project --type website

creates index.qmd, about.qmd, styles.css and _quarto.yml. The scaffolded project file:

project:
  type: website

website:
  title: "webproj"
  navbar:
    left:
      - href: index.qmd
        text: Home
      - about.qmd

format:
  html:
    theme:
      - cosmo
      - brand
    css: styles.css
    toc: true

Render the whole project:

quarto render

builds the site into _site/.

CLI

Command What it does
quarto render file.qmd render one file, or a whole project
quarto render file.qmd --to pdf override the output format
quarto preview live-render preview while editing
quarto create-project --type website scaffold a project (default, book, website, manuscript)
quarto publish gh-pages publish the built site to GitHub Pages; other providers: netlify, quarto-pub, …

Further reading