Skip to content

feat: render formula outputs from code-lab cells - #64

Merged
bencode merged 4 commits into
mainfrom
feat/latex-output
Oct 1, 2026
Merged

bencode merged 4 commits into
mainfrom
feat/latex-output

Conversation

@bencode

@bencode bencode commented Oct 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds a latex output kind to code-lab, rendered with KaTeX (lazy-loaded, so plain cells never download it). All four languages can produce it.

How each language produces it — only the cell's final value counts; print / console.log / display stay plain text:

Language Convention
Python Jupyter's _repr_latex_ (SymPy implements it, so diff(x*sin(x), x) just renders). DataFrame HTML still wins; classes are skipped.
TypeScript An object with toLatex(): string, next to the existing toHtml()
Scheme New built-in (latex "\\frac{1}{2}") — for the SICP material
Clojure (latex "..."), defined in the user namespace at startup (user/latex after an ns switch); #js {:toLatex (fn [] ...)} also works. Cells are now wrapped so a value with toLatex is returned as-is and everything else still goes through pr-str.

LatexOutput sets KaTeX's renderToString markup through dangerouslySetInnerHTML, with a biome-ignore: KaTeX's default trust: false already rejects \href and \html* commands.

One small change from the agreed plan: Python ignores _repr_latex_ on class objects, so evaluating Symbol prints <class ...> instead of a traceback.

Verification

  • typecheck passes for code-lab, web and playground; code-lab tests and biome check pass.
  • Ran in the playground via headless Chrome:
    • Python: SymPy expression renders as a formula; print(...), Symbol, 42 and 'abc' stay text; a DataFrame is still a table.
    • TypeScript: toLatex() renders; malformed TeX shows KaTeX's red source instead of crashing; 21 * 2 stays text.
    • Scheme: (latex ...) renders, including from inside a defined procedure; (+ 1 2) stays text.
    • Clojure: (latex ...), a defn returning latex, #js {:toLatex ...} and user/latex after (ns sicp.ch1) render; (+ 1 2) and (map inc ...) stay text; nil prints nothing; (latex 42) reports latex expects a string, got 42.
    • Network: KaTeX was requested only after the first formula output.

Known issue (pre-existing, not fixed here)

In Scheme, (latex 42) fails as intended, but the cell shows BiwaScheme interpreter error { "form": null } instead of the message. Every Scheme runtime error looks like that today ((car 1), (string-length 42)), so it belongs in a separate fix.

@bencode bencode changed the title feat: render formula outputs from Python, TypeScript and Scheme cells feat: render formula outputs from code-lab cells Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant