Chapter 6 Quarto
Useful resources:
Quarto Tutorial:
Quarto is based on Pandoc and uses its variation of markdown as its underlying document syntax. See the full documentation of Pandoc’s Markdown for more in-depth documentation.
| Quarto Command | Keyboard Shortcut |
|---|---|
| Run Current Cell | ⇧ + ⌘ + Enter |
| Run Current Cell and Jump to Next Cell | ⇧ + Enter |
| Run Selected Line(s) | ⌘ + Enter |
| Run All Cells | ⌥ + ⌘ + R |
Projects
If you have multiple .qmd files in one directory, it’s a good practice to create a project for them.
Benefits of creating a project: ↩︎
Share YAML configuration across multiple .qmd files.
Sometimes yaml gets very long. Put shared YAML options in
_quarto.ymlfile and it will apply to all .qmd files in the same directory. This makes your .qmd files more clean.Redirect output to a specific folder.
Freeze rendered output, so that only changed files will be re-rendered
Types of projects:
default: default type; plain project, no linking between files.This is useful if you just want to render multiple .qmd files in the same directory.
If the file
_quarto.ymlis empty, or iftypeis unspecified, thetypeis assumed to bedefault.book: enforce chapters, build everything togetherwebsite: create navigation, expectindex.qmdas the homepage.blogmanuscriptandconfluence
6.0.0.1 Create a project
In terminal, run quarto create project and follow the prompts to create a new project.
You can also define the type and name as arguments in the command, e.g., quarto create project <type> <name>.
Quarto will then create the folder, e.g. project-name and populate it with _quarto.yml and a Quarto document with the same name as the project title, project-title.qmd:
temp-dirs/project-name
├── _quarto.yml
└── project-title.qmd
The file _quarto.yml is also populated with the project title:
You can manually create a project by creating a _quarto.yml file in the directory. The presence of _quarto.yml, even if empty, signals to Quarto this is a project, and allows you to render without specifying a file.
When you run quarto render, Quarto will render all Quarto documents in the project.
Put project metadata in _quarto.yml file. Any document rendered within the project directory will automatically inherit the metadata defined at the project level. Here is an example of what the _quarto.yml file might look like
project:
type: default
from: markdown+tex_math_single_backslash+markdown_in_html_blocks
bibliography: bibli.bib
format:
html:
theme:
light: [flatly, themes/light.scss]
dark: [darkly, themes/dark.scss]
respect-user-color-scheme: true
toc: true
css:
- ~/Library/CloudStorage/OneDrive-Norduniversitet/_shared-resources/custom-style.css
self-contained: true
html-math-method: mathjax
include-in-header:
- ~/Library/CloudStorage/OneDrive-Norduniversitet/_shared-resources/mathjax.html
fontsize: 14pt
grid:
body-width: 1000pxSome YAML options accepts multiple values, you can specify using the block style with dashes - or the inline style with square brackets [].
For example, the include-in-header option accepts multiple values. You can specify it in block style as follows:
Or you can specify it in inline style as follows:
include-in-header: Include contents of file, verbatim, at the end of the header. This can be used, for example, to include special CSS or JavaScript in HTML documents or to inject commands into the LaTeX preamble.cssofficial option can also be used to include CSS files in HTML documents.
Markdown Extensions can be enabled using from option in YAML.
For example, from: markdown+tex_math_single_backslash+markdown_in_html_blocks enables the following extensions:
tex_math_single_backslashallows you to use\(and\)to delimit inline math, and\[and\]to delimit display math.markdown_in_html_blocksallows you to use markdown syntax within HTML tags.markdown_in_html_blocks(markdown inside html) seems to be enabled by default in Rmd, but NOT in Quarto or Jekyll websites.A workaround if you don’t want to enable this extension is to use html innner and markdown outer, e.g.,
This way, both the html class and markdown bold syntax will apply.
For more on available markdown extensions see the Pandoc Markdown specification.
Note that Rmd enables Markdown extensions differently than Quarto.
- Rmd uses the
md_extensionsoption. - Quarto uses the
fromoption.
Both options can be specified under specific output formats, e.g., html or pdf, or as top-level options that apply to all output formats.
Under specific output formats:
Top-level options:
Host Quarto on GitHub Pages.
To get started, change your project configuration _quarto.yml to use docs as the output-dir.
Then, add a .nojekyll file to the root of your repository that tells GitHub Pages not to do additional processing of your published site using Jekyll (the GitHub default site generation tool):
- Note that
.nojekyll’s location is different than that ofbookdown, which is at/docsfolder.
Only re-render changed files
You can add the following to your _quarto.yml file to only re-render changed files:
When freeze: auto is enabled, Quarto checks for modifications in the source files of your computational documents. If no changes are detected, Quarto will utilize the cached results from previous computations, skipping the re-execution of code chunks.
This significantly speeds up rendering times, especially for large projects with many computational documents. ✅
There are drawbacks: some files may not be updated in time.
- Use
freeze: falseto force re-rendering of all files when you are able to submit your changes. - Use
freeze: autowhen you are editing actively and want to see your changes in time.
Strengths of Quarto:
- hoverable citations and cross-references, easy to read
- easy subplots
Weaknesses of Quarto:
slow compared to
BookdownWorkaround:
- Use
quarto previewin terminal to enable live preview - Set
freeze: autoin_quarto.ymlto only re-render changed files.
- Use
Issues when you want to compile one single page within a package. Changes are not reflected in time unless you render the whole website.
Workaround: Need to exclude from project index, and need file header
yamlto import mathjax settings and themes.Bookdownis reliable. Don’t needyamlin singleRmd, website theme will apply automatically.Not support
rstudioapifunctions. E.g., the following is often used to set working directory to the folder where the current script is located.But it does NOT work in Quarto.