This page demonstrates the include shortcode strategy for loading macros into a Quarto document.
The {{< include macros.qmd >}} shortcode at the top of this file embeds the entire contents of macros.qmd inline at that point. Since macros.qmd contains only raw LaTeX macro definitions (\def, \providecommand, etc.), Quarto passes them through as raw LaTeX for PDF output (where they are processed inline in the document body as LaTeX commands) and as math for HTML/RevealJS output (where they are processed by MathJax).
Note: This demo uses
macros.qmddirectly (same directory). When using this repository as a submodule, usemacros/macros.qmdinstead.
The table below uses macros defined with \def in macros.qmd. These macros work across all output formats:
| Description | Code | Output |
|---|---|---|
| Greek: alpha | $\a$ |
\(\alpha\) |
| Greek: beta | $\b$ |
\(\beta\) |
| Greek: gamma | $\g$ |
\(\gamma\) |
| Greek: lambda | $\l$ |
\(\lambda\) |
| Greek: sigma | $\s$ |
\(\sigma\) |
| Log-likelihood | $\llik$ |
\(\ell\) |
| Independent | $X \ind Y$ |
\(X \perp\!\!\!\perp Y\) |
| IID distributed | $X_i \siid \Normal(\m, \ss)$ |
\(X_i \ \sim_{\operatorname{iid}}\ \operatorname{N}(\mu, \sigma^2)\) |
| Vector: mu | $\vm$ |
\(\tilde{\mu}\) |
| Hat beta | $\hb$ |
\(\hat \beta\) |
macros.qmd defines a one-argument macro for each of the 19 LaTeX base colors (\red, \blue, \green, \orange, …), using the \color switch form ({\color{red}{#1}}), which is supported by MathJax (HTML/RevealJS), lualatex (PDF), and pandoc’s texmath parser (docx):
| Description | Code | Output |
|---|---|---|
| Red | $\red{x}$ |
\({\color{red}{x}}\) |
| Blue | $\blue{y}$ |
\({\color{blue}{y}}\) |
| Green | $\green{z}$ |
\({\color{green}{z}}\) |
| Orange | $\orange{\mu}$ |
\({\color{orange}{\mu}}\) |
| Combined | $\red{x} + \blue{y} = \green{z}$ |
\({\color{red}{x}} + {\color{blue}{y}} = {\color{green}{z}}\) |
| Nested macros | $X_i \sim_{\red{\ind}} \blue{\Normal}(\orange{\mu_i}, \green{\ss})$ |
\(X_i \sim_{{\color{red}{\perp\!\!\!\perp}}} {\color{blue}{\operatorname{N}}}({\color{orange}{\mu_i}}, {\color{green}{\sigma^2}})\) |
Note (docx): pandoc converts these equations to native Word math without warnings, but its docx writer drops the color styling, so in Word the equations render correctly in the default text color.
\providecommand behavior in this shortcode demoThe macro \floor in macros.qmd is defined with \providecommand. Compare this with a \def macro:
| Macro type | Code | Output |
|---|---|---|
\providecommand macro (\floor) |
$\floor{1.2}$ |
\(\mathopen{}\left\lfloor 1.2\right\rfloor\mathclose{}\) |
\def macro (\a) |
$\a$ |
\(\alpha\) |
In this shortcode demo, both macros render in HTML/RevealJS. So in this setup, \providecommand from the included source is being handled correctly.
Note (RevealJS blank slide): Content placed before the first
##heading in RevealJS output becomes a blank title slide. Since the include shortcode is at the top of this file, RevealJS may show a blank slide before the first section. This is a known limitation of the shortcode approach for RevealJS. For RevealJS without a blank slide, use theinclude-in-headerstrategy instead.