Introduction to Working with Quarto Documents
National Data Management Center for Health (NDMC) at EPHI
February 03-08, 2025
A .qmd
file consists of:
YAML Header
: Metadata (title, author, format, options)Markdown-formatted text
: Narrative contentCode Blocks
: Embedded analysis with RRendering Process
: Converts .qmd
into output format (HTML, PDF, etc.)
The key to our reproducible workflow is using Quarto files in RStudio to dynamically render both code and paper narrative.
So what is anatomy of Quarto file?
What is YAML anyway?
---
:Common key words in YAML
#
starts a comment, plain text in Quarto is simply narrative text. Formatting symbols like ##
, **bold**
, and < >
help structure the document.(```)
in source mode.{}
containing options that control how R processes and displays the code.
(ctrl + alt + i)
in the sourceTip: Enable “Render on Save”to preview changes automatically.
Tip
quarto install tool tinytex
.There are multiple ways of formatting valid YAML:
:
:
format: html
with selections made with proper indentationbrand
: Define branding (file path, false
, or inline object).
🔹 Uses branding.yaml
for theme customization.
theme
: Set a theme (default or custom SCSS).
🔹 Combines the default theme with custom.scss
.
anchor-sections
: Show an anchor link when hovering over headings.
📌 Hover over a title to see a 🔗 link.
html-math-method
: Choose math rendering (mathjax
, katex
, etc.).
section-divs
: Wrap sections in <section>
tags.
mainfont
: Set the main font for HTML or LaTeX.fontsize
: Adjust the base font size.linestretch
: Adjust line spacing by 1.5x.fontcolor
: Set the text color.linkcolor
: Set the link color.monobackgroundcolor
: Set the background color for code blocks.backgroundcolor
: Set the document background color.cap-location
: Place figure and table captions (top, bottom, or margin).
📌 Places captions at the bottom of figures and tables.
fig-cap-location
: Place figure captions (top, bottom, or margin).
📌 Places figure captions at the top.
tbl-cap-location
: Place table captions (top, bottom, or margin).
📌 Places table captions in the margin.
classoption
: Set LaTeX/PDF class options or KaTeX math alignment.
📌 Renders display math equations flush left.
page-layout
: Choose page layout (article, full, or custom).
📌 Uses the article layout for the document.
grid
: Configure the grid system for HTML pages.
📌 Sets a 12-column grid layout.
appendix-style
: Choose appendix layout (none, plain, or default).
📌 Applies minimal styling to the appendix.
appendix-cite-as
: Control citation formats in the appendix.
📌 Displays both display
and bibtex
citation formats.
title-block-style
: Choose title block layout (none, plain, or default).
📌 Uses the default title block styling.
title-block-banner
: Add a banner to the title block.
📌 Enables a banner with an auto-selected background color.
title-block-banner-color
: Set banner text color.
📌 Sets banner text color to white.
title-block-categories
: Enable/disable categories in the title block.
📌 Displays categories in the title block.
max-width
: Set a maximum width for the body element.
📌 Limits the body width to 800px.
margin-left
: Set the left margin.
📌 Sets the left margin to 2cm.
margin-right
: Set the right margin.
📌 Sets the right margin to 2cm.
margin-top
: Set the top margin.
📌 Sets the top margin to 2cm.
margin-bottom
: Set the bottom margin.
📌 Sets the bottom margin to 2cm.
code-fold
: Collapse code into an HTML <details>
tag.
📌 Collapses code blocks, allowing users to expand them on demand.
code-summary
: Add summary text for folded code blocks.
📌 Displays “Click to show code” as the summary text.
code-overflow
: Handle code overflow (scroll or wrap).
📌 Adds a scrollbar for overflowing code.
code-line-numbers
: Include line numbers in code blocks.
📌 Adds line numbers to code blocks.
Highlight specific lines:
📌 Highlights lines 1-3 first, then 5-7.
code-copy
: Enable a copy icon for code blocks.
📌 Shows the copy icon when hovering over code blocks.
code-link
: Enable hyperlinking of functions to documentation.
📌 Links functions in code blocks to their online documentation.
code-annotations
: Style or hide code annotations.
📌 Hides code annotations.
code-tools
: Add a code tools menu.
📌 Enables a menu for hiding/showing code.
code-block-border-left
: Add a left border to code blocks.
📌 Adds a green left border to code blocks.
code-block-bg
: Set a background color for code blocks.
📌 Sets a light gray background for code blocks.
highlight-style
: Set the syntax highlighting style.
📌 Uses the Pygments style for syntax highlighting.
syntax-definitions
: Use custom KDE syntax definitions.
📌 Applies custom syntax definitions from custom.xml
.
indented-code-classes
: Specify classes for indented code.
📌 Applies the language-python
class to indented code.
Markdown Syntax | Output |
---|---|
|
italics and bold |
|
superscript2 / subscript2 |
|
|
|
verbatim code |
Markdown Syntax | Output |
---|---|
|
Header 1 |
|
Header 2 |
|
Header 3 |
|
Header 4 |
|
Header 5 |
|
Header 6 |
Markdown
You can embed [named hyperlinks](https://quarto.org/),
direct urls like https://quarto.org/,
and links to [other places](#quarto-anatomy) in the document.
Output
Unordered list:
Output
Ordered list:
Markdown:
> Let us change our traditional attitude to the construction of programs,....
> _Donald Knuth, Literate Programming_
. . .
Output:
Let us change our traditional attitude to the construction of programs,…. Donald Knuth, Literate Programming
:::
div as a HTML <div>
but it can also apply in specific situations to content in PDF:This content can be styled with a border
[text]{.class}
spans can be thought of a <span .class>Text</span>
but again are a bit more transferable if using Pandoc/Quarto native attributes.This is text with special formatting.
Single class: Two equivalent syntaxes
Multiple classes: use {
and .
, separate with spaces
::: callout-note
Note that there are five types of callouts, including:
`note`, `tip`, `warning`, `caution`, and `important`.
:::
Note
Note that there are five types of callouts, including: note
, tip
, warning
, caution
, and important
.
Warning
Callouts provide a simple way to attract attention, for example, to this warning.
Important
Danger, callouts will really improve your writing.
Caution
Here is something under construction.
Caption
Tip with caption.
Pandoc supports numbering and formatting footnotes.
Here is an inline note.^[Inlines notes are easier to write, since you don't have to pick an identifier and move down to type the note.]
Here is an inline note.[^1]
Here is an footnore reference[^1]
[^1]: This can be easy in some situations when you have a really long note or
don't want to inline complex outputs.
. . .
Here is an footnote reference[^2]
. . .
Notice in both situations that the footnote is placed at the bottom of the page in presentations, whereas in a document it would be hoverable or at the end of the document.
r
) is indicated between curly braces {r}
#|
(hashpipe): #| option1: value
Available code cell options: https://quarto.org/docs/reference/cells/cells-knitr.html
If you simply want code formatting but don’t want to execute the code:
```r
```r
head(penguins)
```
. . .
eval: false
as chunk optionecho
echo
option shows the code when set to true
and hides it when set to false
.echo: fenced
is your friend!In reproducible reports and manuscripts, the most commonly included code outputs are tables and figures.
So they get their own special sections in our deep dive!
Markdown:
Output:
Right | Left | Default | Center |
---|---|---|---|
12 | 12 | 12 | 12 |
123 | 123 | 123 | 123 |
1 | 1 | 1 | 1 |
Markdown:
+---------------+---------------+--------------------+
| Fruit | Price | Advantages |
+===============+===============+====================+
| Bananas | $1.34 | - built-in wrapper |
| | | - bright color |
+---------------+---------------+--------------------+
| Oranges | $2.10 | - cures scurvy |
| | | - tasty |
+---------------+---------------+--------------------+
: Sample grid table.
Output:
Fruit | Price | Advantages |
---|---|---|
Bananas | $1.34 |
|
Oranges | $2.10 |
|
+---------------+---------------+--------------------+
| Right | Left | Centered |
+==============:+:==============+:==================:+
| Bananas | $1.34 | built-in wrapper |
+---------------+---------------+--------------------+
+--------------:+:--------------+:------------------:+
| Right | Left | Centered |
+---------------+---------------+--------------------+
Note that grid tables are quite awkward to write with a plain text editor because unlike pipe tables, the column indicators must align.
The Visual Editor can assist in making these tables!
The knitr package can turn data frames into tables with knitr::kable()
:
species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year |
---|---|---|---|---|---|---|---|
Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 |
Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 |
Adelie | Torgersen | 40.3 | 18.0 | 195 | 3250 | female | 2007 |
Adelie | Torgersen | NA | NA | NA | NA | NA | 2007 |
Adelie | Torgersen | 36.7 | 19.3 | 193 | 3450 | female | 2007 |
Adelie | Torgersen | 39.3 | 20.6 | 190 | 3650 | male | 2007 |
If you want fancier tables, try the gt package and all that it offers!
species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year |
---|---|---|---|---|---|---|---|
Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 |
Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 |
Adelie | Torgersen | 40.3 | 18.0 | 195 | 3250 | female | 2007 |
Adelie | Torgersen | NA | NA | NA | NA | NA | 2007 |
Adelie | Torgersen | 36.7 | 19.3 | 193 | 3450 | female | 2007 |
Adelie | Torgersen | 39.3 | 20.6 | 190 | 3650 | male | 2007 |

Penguins playing with a Quarto ball
{fig-align="left"}
{fig-align="right" fig-alt="Illustration of two penguins playing with a Quarto ball."}
Markdown:
::: {#fig-penguins layout-ncol=2}
{#fig-blue width="250px"}
{#fig-orange width="250px"}
Two penguins
:::
Output:
Markdown:
Output:
Last paragraph in the div block is used as the figure caption.
In places like markdown, YAML, or the command line/shell/terminal, you’ll need to use absolute or relative file paths:
"/Users/mine/quarto-asa-nebraska"
- Whose computer will this work on?Relative = BETTER:
"../
= up one directory, ../../
= up two directories, etc.
/..
or /
= start from root
directory of your current computer
Help readers to navigate your document with numbered references and hyperlinks to entities like figures and tables.
Cross referencing steps:
fig-
or tbl-
.@fig-...
or @tbl-...
.The presence of the caption (Blue penguin
) and label (#fig-blue-penguin
) make this figure referenceable:
Markdown:
See @fig-blue-penguin for a cute blue penguin.
{#fig-blue-penguin}
Output:
See Figure 3 for a cute blue penguin.
Figure 3: Blue penguin
The presence of the caption (A few penguins
) and label (#tbl-penguins
) make this table referenceable:
Markdown:
Output: See Table 1 for data on a few penguins.
species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year |
---|---|---|---|---|---|---|---|
Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 |
Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 |
Adelie | Torgersen | 40.3 | 18.0 | 195 | 3250 | female | 2007 |
Adelie | Torgersen | NA | NA | NA | NA | NA | 2007 |
Adelie | Torgersen | 36.7 | 19.3 | 193 | 3450 | female | 2007 |
Adelie | Torgersen | 39.3 | 20.6 | 190 | 3650 | male | 2007 |
knitr
or jupyter
. . .
Weave it all together, and you have a beautiful, functional slide deck!
Let’s build a presentation together from hello-penguins-slides.qmd
and showcase the following features of Quarto presentations:
revealjs
xaringan
, but with Pandoc-native syntaxbeamer
for LaTeX slides.qmd
) documents can be edited in either source mode or visual mode.(.qmd)
documents.At the top of your document, you may have a recommended citation that includes the journal title “Data in Brief.”
To emphasize it in italics:
📌 Tip: If you need bold text, click the “B” icon instead.
📌 Tip: Keyboard shortcuts for headings appear next to their names in the menu.
📌 Tip: To clear formatting, select the text and click the crossed “T” icon.
Lists help organize information clearly and improve readability.
To create a bullet list:
📌 Tip: You can indent (sink) or outdent (lift) items using the increase/decrease indent buttons.
📌 Example: Insert an image for Figure 1 (Fig. 1
).
To insert mathematical equations in Quarto, you have three options:
$E = mc^2$
$$E = mc^2$$
📌 Tip: The font color and style change automatically when a math formula is detected.
Action | Mac Shortcut | Windows Shortcut |
---|---|---|
Bold text | ⌘ B | Ctrl B |
Italic text | ⌘ I | Ctrl I |
Insert link | ⌘ K | Ctrl K |
Heading Level 2 | ## + Space | ## + Space |
Insert code block | ``` + Enter | ``` + Enter |
Beyond basic formatting, Quarto’s visual editor allows for advanced content styling, structured layouts, and enriched documents.
Let’s explore additional formatting options, including HTML elements, blockquotes, footnotes, code chunks, and fenced divs for enhanced styling.
Inserting Images:
Browse for an image file or simply copy and paste it into the document.
Insert HTML, line blocks, blockquotes, and footnotes for structured content.
Use the Insert menu for easy access. 📌 Up Next: Learn how to add code chunks to your .qmd
document.
:::
).{}
to define a class for styling.::: {.border}
Example of some content with a border.
:::
✅ Output: A boxed section that highlights important content.
.columns
class::::: {.columns}
::: {.column width="50%"}
Some content in the left column
:::
::: {.column width="50%"}
Some content in the right column
:::
::::
✅ Output: Two side-by-side columns for better layout control.
After applying these edits, render the document to see how it looks in the final output.
✅ The visual editor simplifies formatting, requiring minimal Quarto knowledge. ✅ You can structure documents with inline code, dynamic content, and special styling. ✅ Fenced divs allow for customized layouts like borders, columns, and sections.
📌 Next Steps: Explore Quarto’s Divs and Spans documentation for more advanced styling techniques! 🚀
The Render button in Quarto follows a two-step process:
📌 Example Issue:
When running analysis, messages like warnings, console outputs, or function messages can clutter the final document.
Solution: Use Code Chunk Options to control what appears in the output.
Code chunk options are written in YAML format within {}
after #|
inside a Quarto code chunk:
#| option: value
Option | Description |
---|---|
include |
(TRUE/FALSE) Whether to include the output in the document (default: TRUE ). |
eval |
(TRUE/FALSE) Whether to evaluate the code. Use eval = c(1,3) to run only parts of the code. |
echo |
(TRUE/FALSE) Show or hide the code chunk itself. |
results |
(markup/hide/asis/hold/true/false) Controls how the text output is displayed. |
warning |
(TRUE/FALSE) Whether to show warnings in the final document. |
message |
(TRUE/FALSE) Whether to display function messages. |
#| echo: false #| message: false #| warning: false #| results: false
🔹 What This Does:
✔ No source code in the rendered output (echo: false
).
✔ No messages or warnings in the final document (message: false
, warning: false
).
✔ Only figures and outputs remain (results: false
).
#| echo: false #| message: false #| warning: false
library(ggplot2)
ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point()
✅ Final Output:
A clean figure with no extra text in the HTML/PDF document.
Click the Render button and check if your document now looks clean and polished.
✔ The Render button runs all code chunks and generates the final document.
✔ Code chunk options help control output visibility in the document.
✔ Use echo: false
, message: false
, and warning: false
to remove unnecessary clutter.
✔ Figures and key results will still appear, keeping the document professional and concise.
📌 Next Steps: Explore advanced chunk options, caching, and custom styling in Quarto! 🚀
When working with Quarto, code chunks allow you to test and preview the code output. However, to generate a final output (e.g., HTML, PDF, or Word document), you must render the document.
Code Execution: All code chunks are executed automatically.
Document Rendering: If there are no errors, the entire Quarto document is compiled into the selected output format.
However, default rendering includes unwanted outputs such as messages, warnings, and other results that may clutter the document. To control this, Quarto provides code chunk options.
Quarto has over 50 different code chunk options for controlling evaluation, text output, formatting, caching, and figures. Here are some common ones:
Option | Description |
---|---|
include = FALSE |
Runs the code but does not display it or its output. |
eval = FALSE |
Prevents the code from executing. |
echo = FALSE |
Hides the source code but shows the output. |
results = 'hide' |
Hides textual output but still allows plots to be displayed. |
warning = FALSE |
Suppresses warnings from appearing in the document. |
message = FALSE |
Hides messages (e.g., package loading messages). |
r
CopyEdit
#| echo: false #| message: false #| warning: false
This setup ensures:
The source code is hidden in the final output.
Messages and warnings are only printed to the console (not included in the document).
Quarto provides several options for controlling figure appearance, including width, height, alignment, and captions.
Option | Description |
---|---|
fig-width & fig-height
|
Sets figure dimensions (in inches). |
out-width & out-height
|
Controls the output size in the document (scaling possible). |
fig-align |
Aligns figures ('left' , 'right' , 'center' ). |
fig-cap |
Adds a caption to the figure. |
fig-link |
Adds a hyperlink to the figure. |
fig-alt |
Specifies alternative text for accessibility. |
r
CopyEdit
#| fig-width: 6 #| fig-height: 4 #| fig-align: "center" #| fig-cap: "This is a sample figure." ggplot(df, aes(x = x_variable, y = y_variable)) + geom_point()
The figure width is 6 inches, and the height is 4 inches.
The figure is center-aligned in the document.
A caption is added.
To improve document organization and rendering speed:
✅ Load all necessary libraries and datasets at the beginning of the document.
✅ Avoid reloading data and packages in multiple code chunks.
Why is this important?
Libraries and datasets loaded in the first chunk remain available throughout the document.
This prevents unnecessary repetition and saves rendering time.
It also makes it easier to track dependencies.
🚨 If you load data before loading the necessary libraries, you may get errors.
💡 Solution: Load all required packages before loading your dataset.
Sometimes, instead of a full code chunk, you only need a quick calculation inside your narrative.
Quarto supports inline code, which allows dynamic values inside text.
r
to embed R code inside text:`` The dataset contains observations. ``