Hi all,

I don't know if the following scenario usually appears in the workflow
of other Org users as well. Otherwise, I think this patch could be
ignored.

In my workflow I often need to pre- or postpend some LaTeX code
immediately before or after a section. Consider the following example:

------------------
* A section

Lorem ipsum

#+latex: \foo

* Another section

Lorem ipsum
-----------------

The \foo command affects the second section, but for Org it belongs to
the content of the first section. If I comment this section out or mark
it as non-exportable, then the LaTeX code has no effect. I think a
simple solution could be to have the PRESEC AND POSTSEC properties to
prepend or postpend arbitrary code to a section. These properties could be
extended with PRESEC+ and POSTSEC+. An example of use:

* Section
 :PROPERTIES:
 :presec:  \begingroup\foo
 :postsec: \endgroup
 :END:
...

Best regards,

Juan Manuel 

-- 
--
------------------------------------------------------
Juan Manuel Macías 

https://juanmanuelmacias.com

https://lunotipia.juanmanuelmacias.com

https://gnutas.juanmanuelmacias.com

>From 56924d69a2090dfeedf4b35ca33e10a48cbc42b5 Mon Sep 17 00:00:00 2001
From: Juan Manuel Macias <maciasch...@posteo.net>
Date: Sun, 18 Sep 2022 13:56:05 +0200
Subject: [PATCH] lisp/ox-latex.el: Add properties for arbitrary LaTeX code.

* (org-latex-headline): The `PRESEC' and `POSTSEC' properties prepend
and postpend arbitrary LaTeX code to a section, respectively.
---
 lisp/ox-latex.el | 42 ++++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 16 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 1eb70ab20..a8c9aecd2 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2322,22 +2322,32 @@ holding contextual information."
 			     (and (string-match-p "\\<headlines\\>" v)
 				  (string-match-p "\\<local\\>" v)
 				  (format "\\stopcontents[level-%d]" level)))))
-		    info t)))))
-	  (if (and opt-title
-		   (not (equal opt-title full-text))
-		   (string-match "\\`\\\\\\(.+?\\){" section-fmt))
-	      (format (replace-match "\\1[%s]" nil nil section-fmt 1)
-		      ;; Replace square brackets with parenthesis
-		      ;; since square brackets are not supported in
-		      ;; optional arguments.
-		      (replace-regexp-in-string
-		       "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
-		      full-text
-		      (concat headline-label pre-blanks contents))
-	    ;; Impossible to add an alternative heading.  Fallback to
-	    ;; regular sectioning format string.
-	    (format section-fmt full-text
-		    (concat headline-label pre-blanks contents))))))))
+		    info t))))
+              ;; `PRESEC' and `POSTSEC' properties for arbitrary LaTeX code.
+              (pre-sec (let ((beg (org-element-property :begin headline)))
+			 (goto-char beg)
+			 (org-entry-get nil "PRESEC")))
+	      (post-sec (let ((beg (org-element-property :begin headline)))
+			  (goto-char beg)
+			  (org-entry-get nil "POSTSEC"))))
+          (concat
+	   (when pre-sec (format "%s\n\n" pre-sec))
+	   (if (and opt-title
+		    (not (equal opt-title full-text))
+		    (string-match "\\`\\\\\\(.+?\\){" section-fmt))
+	       (format (replace-match "\\1[%s]" nil nil section-fmt 1)
+		       ;; Replace square brackets with parenthesis
+		       ;; since square brackets are not supported in
+		       ;; optional arguments.
+		       (replace-regexp-in-string
+		        "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
+		       full-text
+		       (concat headline-label pre-blanks contents))
+	     ;; Impossible to add an alternative heading.  Fallback to
+	     ;; regular sectioning format string.
+	     (format section-fmt full-text
+		     (concat headline-label pre-blanks contents)))
+           (when post-sec (format "%s\n\n" post-sec))))))))
 
 (defun org-latex-format-headline-default-function
     (todo _todo-type priority text tags _info)
-- 
2.37.3

Reply via email to