6.9 Equations

6.9.1 Individual qmd file

Load MathJax Config

For individual qmd files, load mathjax.html in YAML

---
title: "Model specifications"
author: "GDP and climate"
date: "2025-05-13"
format: 
  html:
    toc: true
    self-contained: true
    html-math-method: mathjax
    include-in-header: mathjax.html
    from: markdown+tex_math_single_backslash
---
  • from can be a top-level option (same level as title) or a third-level option under format/html in YAML.

    • It specifies formats to read from.
    • Extensions can be individually enabled or disabled by appending +EXTENSION or -EXTENSION to the format name (e.g. markdown+emoji).
    • See Quarto Extensions for available extensions.
  • from: markdown+tex_math_single_backslash tells Quarto/Pandoc to read the input as Pandoc Markdown with the tex_math_single_backslash extension enabled.

In mathjax.html, define the MathJax configuration, e.g., user-defined macros.

<script>
MathJax = { 
    tex: { 
        tags: 'ams',  // should be 'ams', 'none', or 'all' 
        macros: {  // define TeX macro
            RR: "{\\bf R}",
            bold: ["{\\bf #1}", 1]
        },
    },
};
</script>

tags: 'ams' allows equation numbering.


6.9.2 Quarto project

In _quarto.yml,

format:
  html:
    include-in-header: 
      - file: themes/mathjax.html       # MathJax for LaTeX custom macro support
      - file: themes/common-header.html # load js scripts and external css (e.g., font-awesome) for all pages
    from: markdown+tex_math_single_backslash

Note that from is under html, rather than at the top level of YAML.


6.9.3 Cross-referencing Equations

Equations need to be labeled to be numbered and cross-referenced.

  • Note that labels must begin with #eq-xxx. Don’t forget the hyphen - between eq and xxx.
  • Put the label after the $$ and inside curly braces {}.
  • References to equations are made using @eq-xxx.
Quarto way to label an equation:
$$
y_i = \beta_{i}'x + u_i.
$$ {#eq-cross_sectional_hetero}
  • Difference with bookdown.

    bookdown, on the other hand, use (\#eq:label) (must use colon) after the equation but inside the $$.


6.9.4 Math delimiters

Use $ delimiters for inline math and $$ delimiters for display math.

❗️ Note that

  • For inline math, NO spaces are allowed between the dollar signs and the math content. Otherwise, it will NOT be recognized as math.
  • When you load tex_math_single_backslash extension, you can use \( and \[ as math delimiters; you can add spaces after \( or before \).

Examples:

  • Inline math: $E = mc^2$

  • Block math:

    $$
    a^2 + b^2 = c^2
    $$
    
    or you can put the dollar signs on the same line as the equation:
    $$a^2 + b^2 = c^2$$

Issue: Cannot use \( and \[ for math delimiters.
Fix: Add from: markdown+tex_math_single_backslash to YAML frontmatter. Source

---
title: "Quarto Playground"
from: markdown+tex_math_single_backslash
format:
  html:
    html-math-method: mathjax
---

Inline math example: \( E = mc^2 \)

Block math example:

\[
a^2 + b^2 = c^2
\]

form: Format to read from. Extensions can be individually enabled or disabled by appending +EXTENSION or -EXTENSION to the format name. E.g. markdown+emoji specifies the base format markdown with the support of the emoji extension.

Extension: tex_math_single_backslash

This extension enables the use of \( and \) for inline math, and \[ and \] for display math.

Note: a drawback is that it precludes escaping ( and [.

Refer to Docs of Quarto and Pandoc:


Q: How to get rid of the qmd dependence file?
A: Use

format: 
  html:
    self-contained: true