LaTeX previews everywhere in Emacs

Proof of Concept

A proof of concept: the design of org-latex-preview makes it technically possible to preview LaTeX anywhere in Emacs. Here’s an example previewing LaTeX formatted comments in a code (Clojure) buffer:

And here’s LaTeX previews in the Emacs’ calculator:

The process

This feature does not exist – yet – but can be added fairly easily. To see why, let’s cast our eyes at the preview process breakdown. All tasks here are Org-mode-agnostic save for two:

olp-non-org-flowchart.svg

  • Collect LaTeX fragments: org-latex-preview uses org-element,
  • Create preamble: The org-export library parses the buffer, collects LaTeX keywords and creates the preamble.

To use LaTeX previews outside of Org mode, we only need providers for these two tasks:

  • A parser that can identify the boundaries of LaTeX fragments in the buffer1. This does not need to be a full-fledged parser – just a regexp scan of the buffer looking for \begin{ or \[ can suffice. This is how the above demos were created.
  • A LaTeX preamble generator appropriate for the buffer. This can just be a constant string (instead of a function) that includes a reasonable set of LaTeX packages. I used Org’s default LaTeX preamble string (the variable org-latex-preview-preamble) for the above demos.

The Emacs-wide API for org-latex-preview

org-latex-preview tries to separate these independent concerns by providing the function org-latex-preview-place, that generates previews anywhere in Emacs given the above information. Previewing LaTeX fragments is as simple as:

(require 'org-latex-preview)

(org-latex-preview-place 'dvisvgm entries nil preamble)

where entries encodes the start and end positions of the fragments, and preamble is the LaTeX preamble to use.

;; entries is a list of lists
;; Each entry is (start end),
;; or (start end contents)
((1342 1388)
 (1911 2104)
 (2273 2295 "\\( \\epsilon + 3 \\)"))
;; preamble is a preamble string
;; See `org-latex-preview-preamble'
"\\documentclass{article}
 [DEFAULT-PACKAGES]
 [PACKAGES]
 \\usepackage{xcolor}"

The positions in entries would be obtained from your parser for the major mode. For preamble you can just reuse or tweak org-latex-preview-preamble in a pinch. See the function documentation for more details.

Requiring the org-latex-preview library will in-turn load a few Org libraries (org-element, org-persist, ox and org-macs), but it does not load all of Org. We tried hard to keep from loading the 16,000 line behemoth that is org.el.

This covers basic (static) preview generation. Live-updating previews in non-Org buffers are harder to pull off and will need to work with org-element. This is unlikely to be a seamless affair.

TODO Live LaTeX previews everywhere in Emacs

This is a work in progress. This will currently fail because of numerous calls to org-element-context and org-element-property, we need to provide a way to plug in a major-mode specific version here.

Footnotes:

1

Technically you could reuse org-element in non-Org buffers for this, but there will be many false negatives and occasional false positives.

Author: karthink

Created: 2024-04-04 Thu 12:48

Validate