Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

docs: render katex (#3553)

Myles Scolnick1c269fa95dc6
diff --git a/docs/_static/js/math.js b/docs/_static/js/math.js
new file mode 100644
index 0000000000..822f08a7dc
--- /dev/null
+++ b/docs/_static/js/math.js
@@ -0,0 +1,53 @@
+(() => {
+ const renderMath = (element) => {
+ const tex = element.textContent || element.innerHTML;
+ if (tex.startsWith("\\(") && tex.endsWith("\\)")) {
+ katex.render(tex.slice(2, -2), element, { displayMode: false });
+ } else if (tex.startsWith("\\[") && tex.endsWith("\\]")) {
+ katex.render(tex.slice(2, -2), element, { displayMode: true });
+ }
+ };
+
+ const renderAllMath = () => {
+ const maths = document.querySelectorAll(
+ ".arithmatex:not([data-processed])",
+ );
+ maths.forEach((element) => {
+ try {
+ renderMath(element);
+ element.setAttribute("data-processed", "true");
+ } catch (error) {
+ console.warn("Failed to render math:", error);
+ }
+ });
+ };
+
+ // Watch for new content
+ const observer = new MutationObserver((mutations) => {
+ const shouldRender = mutations.some((mutation) => {
+ return mutation.addedNodes.length > 0;
+ });
+ if (shouldRender) {
+ renderAllMath();
+ }
+ });
+
+ const init = () => {
+ if (typeof katex === "undefined") {
+ console.warn("KaTeX not loaded");
+ return;
+ }
+
+ renderAllMath();
+ observer.observe(document.body, {
+ childList: true,
+ subtree: true,
+ });
+ };
+
+ if (document.readyState === "loading") {
+ document.addEventListener("DOMContentLoaded", init);
+ } else {
+ init();
+ }
+})();
diff --git a/mkdocs.yml b/mkdocs.yml
index e12641eade..70af9226c6 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -23,9 +23,12 @@ extra:
- icon: fontawesome/brands/python
link: https://pypi.org/project/marimo/
extra_css:
+ - https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.9.0/katex.min.css
- stylesheets/extra.css
extra_javascript:
+ - https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.9.0/katex.min.js
- _static/js/analytics.js
+ - _static/js/math.js
markdown_extensions:
- blocks:MarimoBlocksExtension
- admonition