Typst or LaTeX?

Styling PDF documents with Quarto extensions

Nicola Rennie, Lancaster University
satRdays London, 27 April 2024

About Me


Academic background in statistics and operational research.

Experience in statistics/data science consultancy.

Lecturer in Health Data Science in Lancaster Medical School.

Research interests: reproducible research, modelling healthcare data, research software engineering, communicating data effectively, …

What’s this talk about?

  • Creating PDF documents with Quarto

  • Styling PDF documents

  • The pros and cons of Typst and LaTeX for PDFs

  • Combining R outputs with different types of PDF

Why should you care about styling documents?

Consistency

Documents from different authors look the same.


Professionalism

Default options usually look like the default options.


Engagement

It’s easier to read stuff that looks nice.



Disclaimer

I don’t really like PDFs…

…but if I have to make them, I make them with Quarto.

What is Quarto?

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 logo



The real question is…

LaTeX or Typst?



Styling Quarto PDFs
with LaTeX

Starting with a plain document

---
title: "My document"
format: pdf
---

Content goes here.

Quarto document screenshot

Built-in options for PDF output

---
title: "My document"
format:
  pdf:
    urlcolor: "red"
---

Content goes [here](https://github.com/).


See quarto.org/docs/reference/formats/pdf for all PDF options.

Quarto document screenshot

What is LaTeX?

LaTeX is a software system for typesetting documents, that separates out the content from the styling.

Quarto (and R Markdown) use it to convert from .qmd to .pdf files (with help from Pandoc and {knitr}).

LaTeX logo

Adding your own LaTeX styling

document.qmd

---
title: "My document"
format:
  pdf:
    include-in-header: 
       - "styles.tex"
---

# Section 1

Content goes [here](https://github.com/).

styles.tex

%% load packages
\usepackage{xcolor}
\usepackage{sectsty}

%% Let's define some colours
\definecolor{MyPurple}{HTML}{800080}

%% Section font colour
\sectionfont{\color{MyPurple}}

Adding your own LaTeX styling

Quarto document screenshot

Quarto extensions

Quarto 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:

  • Embed shinylive applications in a Quarto document
  • Embed HTML forms in documents
  • Use Font Awesome icons in HTML and PDF outputs

Quarto extensions for LaTeX styling

Install (e.g. from GitHub):

quarto install extension username/repository


Example:

quarto install extension nrennie/PrettyPDF
---
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
```

You can add options to executable code like this

```r
#| echo: false
2 * 2
```

The `echo: false` option disables the printing of code (only output is displayed).

Quarto extensions for LaTeX

Quarto document screenshot

LaTeX styling in R

Tables with {knitr}

knitr::kable(head(mtcars[, 1:4]), "latex")

Tables with {kableExtra}

kableExtra::kable(head(mtcars[, 1:4])) |> 
  kableExtra::kable_styling(latex_options = "striped")

LaTeX styling in R

Tables with {gt}

{gt} supports the HTML, LaTeX, and RTF output formats.

library(gt)
mtcars[, 1:4] |>
  head() |> 
  gt() |>
  tab_header(
    title = md("Data from **mtcars**"),
  ) |>
  as_latex()

Downsides of LaTeX

  • You need to install it separately from Quarto

  • Errors are often hard to understand or debug

  • It can be quite slow to render documents

If only there was another way …



Styling Quarto PDFs
with Typst

What is Typst?

Typst is a markup-based typesetting system that is designed to be as powerful as LaTeX while being much easier to learn and use which has been around since late December 2022.


It comes with Version 1.4+ of Quarto.

Typst logo

Starting with a plain document

---
title: "My document"
format: typst
---

Content goes here.

Quarto document screenshot

Built-in options for Typst output

---
title: "My document"
format: 
  typst:
    mainfont: "Cascadia Code"
---

Content goes here.


See quarto.org/docs/reference/formats/typst for all Typst options.

Quarto document screenshot

Adding your own Typst styling

document.qmd

---
title: "My document"
format:
  typst:
      template-partials:
        - typst-template.typ
        - typst-show.typ
---

Content goes [here](https://github.com/).

typst-show.typ

#show: article.with(
$if(title)$
  title: "$title$",
$endif$
)

typst-template.typ

#let article(
  // The document title.
  title: "Some title",

  // The document content.
  body
) = {

  // Set document metadata.
  set document(title: title)

  // Links should be purple.
  show link: set text(rgb("#800080"))
  
  // Title.
  text(weight: 800, upper(title))

  // The main body text.
  body

}

Adding your own Typst styling

Quarto document screenshot

Quarto extensions for Typst

Quarto extensions for Typst work similarly:

  • Installing the extension downloads a .typ file with some styling
  • Change the format in the YAML

Example: dept-news (by Quarto team)

---
title: "Chemistry Department"
format:
  dept-news-typst: default
---

Quarto document screenshot

See github.com/quarto-ext/typst-templates.

Typst outputs from R

library(tinytable)
tt(mtcars[1:4, 1:5],
   theme = "striped",
   width = 0.5,
   caption = "Data about cars.") |> 
  style_tt(
   i = 2,
   j = 2,
   background = "red")

Table in document screenshot



Typst or LaTeX?

Choosing between types of PDF

How easy is it to learn (or read)?

Screenshot of a pdf with a light purple sidebar. Screenshot of a pdf with a light purple sidebar.
Similar PDF styling implemented with LaTeX (left) and Typst (right)

How easy is it to learn (or read)?

LaTeX

\usepackage{geometry}
\usepackage{eso-pic}
\usepackage{xcolor}
\geometry{a4paper, total={170mm,257mm}, left=20mm, top=20mm, bottom=20mm, right=50mm}
\definecolor{light}{HTML}{E6E6FA}
\AddToShipoutPicture{% 
  \AtPageLowerLeft{% 
      \put(\LenToUnit{\dimexpr\paperwidth-3cm},0){% 
          \color{light}\rule{3cm}{\LenToUnit\paperheight}}%
   }%
}%

Typst

set page(
  margin: (left: 2cm, right: 1.5cm, top: 2cm, bottom: 2cm),
  background: place(right + top, rect(
    fill: rgb("#E6E6FA"),
    height: 100%,
    width: 3cm,
  ))
)

Pros and Cons

Reasons to use LaTeX:

  • You want to include equations (currently easier in LaTeX)

  • (Currently) more flexibility with images and tables

  • Most journals are not (yet) ready for Quarto or Typst

  • You already know LaTeX, and have built lots of stuff using it

Reasons to use Typst:

  • Easier to learn (personal opinion!)

  • More informative error messages

  • No extra installations

  • Speed