Quarto Q&A: How to use non-default LaTeX packages/macros in MathJax?

A second blog post of the “Quarto Q&A” series on how to activate additional MathJax packages in a Quarto document.

Quarto
Q&A
MathJax
LaTeX
Author

Mickaël CANOUIL

Published

Sunday, the 12th of March, 2023

A second blog post of the “Quarto Q&A” series.
This time, I will show how to activate additional MathJax packages in a Quarto document.

Quarto logo and text in the center of the image. Below, MathJax logo and text.

1 The Question/Problem

Sometimes you want to use additional MathJax packages in a Quarto document, such as being able to use \qty from physics or upper/lower case Greek letters from upgreek.

2 The Answer/Solution

The solution is to load the additional packages in the include-in-header key of the YAML header, using JavaScript as documented on MathJax documentation.
You can load several packages at once, as shown in the example below.

---
title: "Quarto MathJax"
format:
  html:
    html-math-method: mathjax
    include-in-header:
      - text: |
          <script>
          window.MathJax = {
            loader: {
              load: ['[tex]/upgreek', '[tex]/boldsymbol', '[tex]/physics']
            },
            tex: {
              packages: {
                '[+]': ['upgreek', 'boldsymbol', 'physics']
              }
            }
          };
          </script>
---

$$
\rank(\boldsymbol{\Upsigma})
$$

Screenshot of an html rendered by Quarto. It contains Quarto MathJax as a title, followed by an equation of rank bold upper sigma.

3 More on MathJax Extensions/Macros

See https://docs.mathjax.org/en/latest/input/tex/extensions/index.html for more information on MathJax LaTex extensions. See https://docs.mathjax.org/en/latest/input/tex/macros/index.html for more information on MathJax LaTeX macros.