LaTeX fragment preview via Kitty graphics (#3)
sportmacher opened 2 months ago

Reported by: pathemata (Reddit)

Add support for rendering LaTeX fragments in org-mode buffers using Kitty graphics. Currently org renders LaTeX fragments to PNG via dvipng/dvisvgm for GUI Emacs. In terminal Emacs with kitty-graphics-mode, these previews could be displayed using the same Kitty graphics pipeline.

Approach:

  • Hook into org-latex-preview (org 9.7+) or org-toggle-latex-fragment (older)
  • Use org's existing LaTeX-to-image pipeline to generate PNG
  • Display the resulting PNG via kitty-gfx-display-image at the fragment location
  • May need to handle inline vs display math differently for sizing

Related: nabla.nvim was mentioned as a reference for fancier math rendering in terminals (Unicode-based approach).

  • sportmacher commented 2 months ago

    LaTeX fragment preview implemented with three components:

    1. kitty-gfx--org-latex-preview-advice: Around advice on org-latex-preview that bypasses the (display-graphic-p) guard. The image generation pipeline (latex + dvipng/dvisvgm) works fine in terminal -- only the entry point and display step assume a GUI. Handles C-u (clear region) and C-u C-u (clear buffer) prefix args.

    2. kitty-gfx--org-make-preview-overlay-advice: Around advice on org--make-preview-overlay that intercepts overlay creation. Instead of an Emacs image spec (which doesn't render in terminal), displays the generated PNG/SVG via our existing Kitty graphics pipeline. Tags the overlay with org-overlay-type = org-latex-overlay so org's cleanup functions still find it.

    3. kitty-gfx--org-clear-latex-preview: Cleanup function that removes Kitty placements for LaTeX overlays, used by the preview advice for C-u prefix arg.

    Test org file updated with LaTeX fragment examples. Byte-compiles and load-tests clean. Needs manual testing with dvipng or dvisvgm installed.

  • sportmacher changed state to 'In Review' 2 months ago
    Previous Value Current Value
    Open
    In Review
  • sportmacher commented 2 months ago

    Verified working. All three test fragments render correctly:

    • Inline: $E = mc^2$
    • Display: integral equation with equation number
    • Inline: summation formula

    Required fixes beyond initial implementation:

    1. clear-image-cache requires GUI frame — suppressed with cl-letf
    2. Terminal face attributes return "unspecified-fg" which breaks org-latex-color-format — forced concrete fallback colors (Black fg, Transparent bg)
  • sportmacher commented 2 months ago

    Fixed in b60fcce. C-c C-x C-l now renders LaTeX fragments as images in terminal Emacs via Kitty graphics. Advises org-latex-preview, org--make-preview-overlay, and adds clear-preview support. User verified working.

  • sportmacher changed state to 'Closed' 2 months ago
    Previous Value Current Value
    In Review
    Closed
issue 1/1
Type
New Feature
Priority
Major
Assignees
Not assigned
Issue Votes (0)
Watchers (1)
Reference
cashmere/kitty-graphics#3
Please wait...
Connection lost or session expired, reload to recover
Page is in error, reload to recover