responsive_katex = (cell_invalidation, katex_root) => {
const div = html`<div>${katex_root}`;
div.style.transformOrigin = `top left`;
const width_updates = width_observer();
cell_invalidation.then(() => width_updates.return());
(async function (){
await new Promise(callback => requestAnimationFrame(callback));
const katex_width = [...katex_root.querySelectorAll('.katex-html .base')]
.reduce((acc, elem) => acc + elem.getBoundingClientRect().width, 0);
const katex_height = katex_root.getBoundingClientRect().height;
for await (const width of width_updates) {
const scale = Math.min(width/katex_width, 1);
const vertical_gap = (1-scale)*katex_height;
div.style.transform = `scale(${scale})`;
div.style.marginBottom = `-${vertical_gap}px`;
}
})();
return div;
}