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
---fromcan be a top-level option (same level astitle) or a third-level option underformat/htmlin YAML.- It specifies formats to read from.
- Extensions can be individually enabled or disabled by appending
+EXTENSIONor-EXTENSIONto the format name (e.g. markdown+emoji). - See Quarto Extensions for available extensions.
from: markdown+tex_math_single_backslashtells Quarto/Pandoc to read the input as Pandoc Markdown with thetex_math_single_backslashextension enabled.fromtex_math_single_backslashsupports\(and\[as math delimiters.- See Pandoc: Math Input for available math extensions.
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_backslashNote 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-betweeneqandxxx. - Put the label after the
$$and inside curly braces{}. - References to equations are made using
@eq-xxx.
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_backslashextension, you can use\(and\[as math delimiters; you can add spaces after\(or before\).
Examples:
Inline math:
$E = mc^2$Block math:
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:
https://quarto.org/docs/reference/formats/html.html#rendering
https://pandoc.org/MANUAL.html#extension-tex_math_single_backslash
https://pandoc.org/MANUAL.html#extension-tex_math_double_backslash
Q: How to get rid of the qmd dependence file?
A: Use