observablehq / stdlib

The Observable standard library.
https://observablehq.com/@observablehq/standard-library
ISC License
966 stars 83 forks source link

include KaTeX's mhchem extension #184

Closed espinielli closed 3 years ago

espinielli commented 3 years ago

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 ;-)

espinielli commented 3 years ago

mhchem is quite nice...

mbostock commented 3 years ago

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);
  };
}
espinielli commented 3 years ago

Great! And thanks for the notebook

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