Styling documents and building extensions with Quarto
19 December 2023
Dr Nicola Rennie
Lecturer in Health Data Science at Lancaster Medical School.
Academic background in statistics, with experience in data science consultancy.
Blog about R and data science at nrennie.rbind.io/blog.
How to style Quarto documents with built-in options
How to style Quarto documents using Quarto extensions
How to build your own Quarto extension
Documents from different authors look the same.
Default options usually look like the default options.
Information can often be presented in better ways when designed actively.
Quarto is an open-source scientific and technical publishing system that allows you to combine text, images, code, plots, and tables in a fully-reproducible document. Quarto has support for multiple languages including R, Python, Julia, and Observable. It also works for a range of output formats such as PDFs, HTML documents, websites, presentations, …
Quarto includes 25 themes from the Bootswatch project for HTML output.
See quarto.org/docs/output-formats/html-themes.html for a complete list.
There are also 11 built-in themes for Revealjs presentations.
---
title: "My document"
format:
html:
backgroundcolor: lightblue
---
Content goes [here](https://github.com/).
See quarto.org/docs/reference/formats/html for all HTML options.
See quarto.org/docs/reference/formats/pdf for all PDF options.
document.qmd
In the upcoming Quarto 1.4 release, you can also create PDFs using Typst (no LaTeX!).
Extensions are a powerful way to modify and extend the behavior of Quarto. Extensions can be used to add styling to your documents.
Extensions aren’t just used to change document styling.
There are also extensions to:
There are different ways to distribute Quarto extensions. Many are distributed via GitHub.
To install an extension, run the following in the command line:
For example, to install my PrettyPDF
extension:
This creates an _extensions
folder in your current directory.
You then need to edit the YAML at the top of the Quarto document, to tell it to use the extension:
Need to reformat to a completely different style?
Some Quarto extensions (especially style extensions) come with a template file that changes the YAML for you.
For example, to use my PrettyPDF
template:
This creates an _extensions
folder in your current directory and a .qmd
file with the correct YAML.
Don’t copy and paste commonly used YAML options between documents.
Everybody in an organisation can create similar outputs without having to adjust the style themselves.
Changes to designs can be tracked alongside code.
The essentials…
_extensions folder
_extension.yml file
The nice to haves…
template.qmd file
Other files e.g. logos or fonts
The folder structure might look like this:
template.qmd
file---
title: "Pretty PDFs with Quarto"
format: PrettyPDF-pdf
---
## Quarto
Quarto enables you to weave together content and executable code into a finished
document. To learn more about Quarto see <https://quarto.org>.
### Running Code
When you click the **Render** button a document will be generated that includes both
content and the output of embedded code. You can embed code like this:
```{r}
1 + 1
```
_extension.yml
filePrettyPDF.tex
filePrettyPDF.tex
file%% Let's define some colours
\definecolor{light}{HTML}{E6E6FA}
\definecolor{highlight}{HTML}{800080}
\definecolor{dark}{HTML}{330033}
%% style the chapter/section fonts
\chapterfont{\color{dark}\fontsize{20}{16.8}\selectfont}
\sectionfont{\color{dark}\fontsize{20}{16.8}\selectfont}
\subsectionfont{\color{dark}\fontsize{14}{16.8}\selectfont}
\titleformat{\subsection}
{\sffamily\Large\bfseries}{\thesection}{1em}{}[{\titlerule[0.8pt]}]
PrettyPDF.tex
file%% Let's add the border on the right hand side
\AddToShipoutPicture{%
\AtPageLowerLeft{%
\put(\LenToUnit{\dimexpr\paperwidth-3cm},0){%
\color{light}\rule{3cm}{\LenToUnit\paperheight}%
}%
}%
% logo
\AtPageLowerLeft{% start the bar at the bottom right of the page
\put(\LenToUnit{\dimexpr\paperwidth-2.25cm},27.2cm){% move it to the top right
\color{light}\includegraphics[width=1.5cm]{_extensions/nrennie/PrettyPDF/logo.png}
}%
}%
}
After pushing code to GitHub, install the extension:
Render the document:
Things I learnt the hard way…
Things get confusing when you build an extension with same name as your GitHub username
(Font) paths are hard
You’ll likely want multiple output formats in one extension (e.g. book and pdf)
Slides: nrennie.rbind.io/talks/bplim-quarto-extensions
Blog: nrennie.rbind.io/blog/making-pretty-pdf-quarto
PrettyPDF extension: github.com/nrennie/PrettyPDF
Building extensions guide: quarto.org/docs/extensions/creating
Building your own Quarto extension is an effective way to streamline the process of styling your documents.