Add latexify hooks for metadata and ops#1835
Conversation
|
@AayushSabharwal Can also you have a look at this PR? :) |
| @test_reference "latexify_refs/call_with_metadata.txt" latexify(f) | ||
|
|
||
| struct LatexHookCtx end | ||
| function LatexifyExt._toexpr_metadata(O, ::Type{LatexHookCtx}, val; latexwrapper = LatexifyExt.default_latex_wrapper) |
There was a problem hiding this comment.
Is defining methods of functions in extensions like this valid? I thought Base.get_extension was internal and only for debugging?
There was a problem hiding this comment.
Yeah it is heavily discouraged by the Julia Base maintainers. Never really understood why.
The real issue is that the hookable functions live in the extension, so downstream packages have no clean way to extend them. We can move _toexpr_metadata and _toexpr_op to the main Symbolics module with nothing-returning fallback methods. The extension then calls those Symbolics-defined functions. Downstream packages can extend them directly via import Symbolics without touching Base.get_extension. _toexpr_plain and default_latex_wrapper are Latexify-coupled helpers and stay in the extension; they'd only be needed for composing output inside a hook, and at that point the user already has Latexify loaded.
There was a problem hiding this comment.
Do you think this is the correct solution?
Summary
Add extension points to the Latexify pipeline so downstream packages can customize LaTeX rendering based on:
BasicSymbolicnodes, andThis enables non-pirating, opt-in rendering for DSLs (e.g., angle-bracket averages, sum prefixes) without forking Symbolics' LaTeX code.
What changed
_toexpr_metadata(O, ::Type{Ctx}, val; latexwrapper)to allow metadata-driven LaTeX overrides._toexpr_op(op, args; latexwrapper)to allow operator-driven LaTeX overrides._toexpr_plainso hooks can fall back to the default LaTeX formatting.Example usage
Example output
avg(x + y)renders as\langle x + y \rangle\Sigma(i=1:N) ...Tests
test/latexify.jlshowing a custom op hook returning aLaTeXString.Motivation
Downstream packages currently have no hook to customize LaTeX output for custom operators or metadata-rich nodes (e.g., average brackets, sum prefixes). This PR adds general hooks without changing default behavior.