With this patch, given

  * Derive | <-- POINT IS HERE
    ...
  ** Step 1.
     ... \( LaTeX \) ...
  ** Step 2.
     ... \( LaTeX \) ...

or

  * Derive
    ... | <-- POINT IS HERE
  ** Step 1.
     ... \( LaTeX \) ...
  ** Step 2.
     ... \( LaTeX \) ...

all of "Derive" is previewed upon `org-latex-preview',

  * Derive
    ... | <-- NOT STOPPING HERE
  ** Step 1.
     ... \( LaTeX \) ...
  ** Step 2.
     ... \( LaTeX \) ...

P.S. I hope the upcoming LaTeX preview will work like this too.

Rudy
>From bb0ece81889be34f8b45be01509b81558f99fed0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rudolf=20Adamkovi=C4=8D?= <rud...@adamkovic.org>
Date: Fri, 21 Mar 2025 19:22:27 +0100
Subject: [PATCH] Preview LaTeX in sections recursively

* lisp/org.el (org--latex-preview-section-bounds): Compute the bounds
of the current section for `org-latex-preview'.
(org-latex-preview): Use `org--latex-preview-section-bounds' to
preview sections recursively.
---
 lisp/org.el | 37 +++++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 16 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index ebc889463..4b8453661 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16196,6 +16196,19 @@ BEG and END are buffer positions."
 	 default-directory)
        'overlays nil 'forbuffer org-preview-latex-default-process))))
 
+(defun org--latex-preview-section-bounds ()
+  "Return the boundaries of the section.
+Value is a list of one or more cons cells of the form (START . END).
+Section bounds include subsections, if some exist."
+  (if (org-before-first-heading-p)
+      (cons (point-min)
+            (org-entry-end-position))
+    (save-excursion
+      (org-back-to-heading t)
+      (let ((element (org-element-at-point)))
+        (cons (org-element-begin element)
+              (org-element-end element))))))
+
 (defun org-latex-preview (&optional arg)
   "Toggle preview of the LaTeX fragment at point.
 
@@ -16231,15 +16244,10 @@ fragments in the buffer."
     (message "Creating LaTeX previews in buffer... done."))
    ;; Clear current section.
    ((equal arg '(4))
-    (org-clear-latex-preview
-     (if (use-region-p)
-         (region-beginning)
-       (if (org-before-first-heading-p) (point-min)
-         (save-excursion
-	   (org-with-limited-levels (org-back-to-heading t) (point)))))
-     (if (use-region-p)
-         (region-end)
-       (org-with-limited-levels (org-entry-end-position)))))
+    (let ((bounds (if (use-region-p)
+                      (region-bounds)
+                    (org--latex-preview-section-bounds))))
+      (org-clear-latex-preview (car bounds) (cdr bounds))))
    ((use-region-p)
     (message "Creating LaTeX previews in region...")
     (org--latex-preview-region (region-beginning) (region-end))
@@ -16257,13 +16265,10 @@ fragments in the buffer."
 	     t))))
    ;; Preview current section.
    (t
-    (let ((beg (if (org-before-first-heading-p) (point-min)
-		 (save-excursion
-		   (org-with-limited-levels (org-back-to-heading t) (point)))))
-	  (end (org-with-limited-levels (org-entry-end-position))))
-      (message "Creating LaTeX previews in section...")
-      (org--latex-preview-region beg end)
-      (message "Creating LaTeX previews in section... done.")))))
+    (let ((bounds (org--latex-preview-section-bounds)))
+       (message "Creating LaTeX previews in section...")
+       (org--latex-preview-region (car bounds) (cdr bounds))
+       (message "Creating LaTeX previews in section... done.")))))
 
 (defun org-format-latex
     (prefix &optional beg end dir overlays msg forbuffer processing-type)
-- 
2.39.5 (Apple Git-154)

-- 
"Logic is a science of the necessary laws of thought, without which no
employment of the understanding and the reason takes place."
--- Immanuel Kant, 1785

Rudolf Adamkovič <rud...@adamkovic.org> [he/him]
http://adamkovic.org

Reply via email to