Is it possible to define LaTeX macros in a notebook? For example, I would like to write ${tex`\def\reals{\mathbb{R}`}
somewhere and then be able to refer to ${tex`\reals`}
elsewhere (ideally, across different blocks).
There are a few ways to do this, but the best option may be to augment the standard library slightly to allow passing options through to KaTeX.
Weâre using KaTeX 0.10-beta, which does support \def
, but this only affects the given input to tex
âit doesnât persistently change the behavior of tex
. This is good because it avoids mutable state and nondeterministic behavior. (KaTeX also appears to support \gdef
, which sounds like it would persistently change the behavior of tex
, but I couldnât get it to work and wouldnât recommend it anyway.)
Defining a macro to use it once isnât especially useful, but it works:
tex`\def\reals{\mathbb{R}} \reals^2`
To make reusable macros, you could have a macros cell to define your macros, and then embed them wherever you call tex
. Note youâll need String.raw to avoid double-backslashes.
macros = String.raw`\def\reals{\mathbb{R}}`
tex`${macros}\reals^2`
You can go one step further and define a wrapper for tex
that has your macros in it:
function mtex() {
return tex`
\def\reals{\mathbb{R}}
${String.raw.apply(String, arguments)}
`;
}
mtex`\reals`
If you want total control, hereâs how do define your own template literal using KaTeX directly:
katex = require("katex")
function mtex() {
const root = document.createElement("div");
katex.render(String.raw.apply(String, arguments), root, {
macros: {
"\\reals": "\\mathbb{R}"
}
});
return root.removeChild(root.firstChild);
}
In the future, Iâd like something like this:
mtex = tex.options({
macros: {
"\\reals": "\\mathbb{R}"
}
})