observablehq/stdlib

include KaTeX's mhchem extension

espinielli opened this issue · 3 comments

I was just wondering how difficult it would be to include KaTeX's mhchem extension in tex...but unfortunately the code defining tex is beyond my possibilities to hack/test something (BTW is there some place where to learn?).

But maybe it would not be too difficult for the usual contributors...otherwise dump it ;-)

mhchem is quite nice...

Here’s how do define your own definition of tex with the mhchem extension:

tex = {
  const [katex, style] = await Promise.all([
    require("katex@0.12.0/dist/katex.min.js"),
    require.resolve("katex@0.12.0/dist/katex.min.css")
  ]);
  document.head.append(html`<link rel="stylesheet" href="${style}">`);
  await require.alias({katex})("katex@0.12.0/dist/contrib/mhchem.min.js");
  return function tex() {
    var root = document.createElement("div");
    katex.render(String.raw.apply(String, arguments), root);
    return root.removeChild(root.firstChild);
  };
}

Great! And thanks for the notebook

(I noticed that sometimes you get parsing errors and need to add extra white spaces before/after { })