Use STIX Fonts in R Markdown and Quarto for Readability

Nan Xiao January 15, 2024 3 min read
Seashore with Shipwreck by Moonlight by Caspar David Friedrich.
Seashore with Shipwreck by Moonlight by Caspar David Friedrich.

I have been playing a lot of Elden Ring lately. After investing 200 (!) hours into it, I learned three valuable lessons:

  1. Life is hard. Something that seems unachievable today doesn’t mean it will be tomorrow. Explore, learn, practice, and with efforts, and perhaps a little luck, suddenly, you’ve done it.
  2. Life is unfair. You can choose to play aggressively, play conservatively, or even move on—just don’t be greedy.
  3. If your artwork or software has a Gothic theme, try use a serif typeface for the UI; it will fit really well!

Admiring the UI font of Elden Ring drove me to seek a professional-grade serif typeface with an open source license. This search led me to STIX fonts. The transitional style text font (STIX Two Text) and math font (STIX Two Math) offer Times-like typography with unique design details. One particular benefit is the “familiarity” factor, which some UX research suggests improves readability, meaning higher reading speed and precision.

Since serif fonts are often used in typesetting long documents, I created a GitHub repository, nanxstats/rmarkdown-quarto-stix2, to demonstrate how to use STIX fonts in both R Markdown and Quarto for PDF outputs via LaTeX. You can find the source document examples with rendered PDF specimen there, and here is a brief summary of how it works.

R Markdown

The simplest way to use STIX fonts in R Markdown is to include the LaTeX package stix2 with extra_dependencies:

output:
  pdf_document:
    extra_dependencies:
      stix2: ["notextcomp"]

Alternatively, use header-includes to write the LaTeX commands directly. You can also change the rendering engine from pdflatex to xelatex:

output:
  pdf_document:
    latex_engine: "xelatex"
header-includes:
  - \usepackage[notextcomp,nomath]{stix2}

Quarto

Since Quarto’s default LaTeX template uses a sans serif font (Latin Modern Sans) for the headings, it can look a bit odd when pairing with STIX fonts. Therefore, I used the helvet package to match the familiarity:

pdf-engine: pdflatex
format:
  pdf:
    include-in-header:
      text: |
        \usepackage[notextcomp]{stix2}
        \usepackage[scaled=0.92]{helvet}

Known issues

There are some common issues that can be fixed or alleviated by using additional options from the stix2 package. The stix2-type1 package documentation explains these options in detail.

  • Loading textcomp twice: You probably noticed that we used the notextcomp option to avoid loading the textcomp package. This is because the default pandoc template already loads it.

  • Math font conflicts: XeLaTeX has some issues with the math font, so we used the nomath option to disable it when using XeLaTeX. Otherwise, there will be errors related to “command already defined”.

    As a simple solution, using nomath has no practical impact, especially if you don’t use math in the document. However, if you do use math and try the savesym method to avoid the errors, there will still be significant issues when rendering, such as missing summation symbols in the formulas.

    So far, I have not found a working solution for this issue yet, so if you know how to make it work properly, please let me know.

That’s it. If you prefer the Computer Modern typeface but want a bolder version, see the previous post about MLModern.