emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [Patch] Pre-/postpend arbitrary LaTeX code to a section
@ 2022-09-18 12:27 Juan Manuel Macías
  2022-09-18 16:14 ` Max Nikulin
                   ` (2 more replies)
  0 siblings, 3 replies; 21+ messages in thread
From: Juan Manuel Macías @ 2022-09-18 12:27 UTC (permalink / raw)
  To: orgmode

[-- Attachment #1: Type: text/plain, Size: 1168 bytes --]

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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-ox-latex.el-Add-properties-for-arbitrary-LaTeX-.patch --]
[-- Type: text/x-patch, Size: 2844 bytes --]

From 56924d69a2090dfeedf4b35ca33e10a48cbc42b5 Mon Sep 17 00:00:00 2001
From: Juan Manuel Macias <maciaschain@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


^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2022-09-26  7:49 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-18 12:27 [Patch] Pre-/postpend arbitrary LaTeX code to a section Juan Manuel Macías
2022-09-18 16:14 ` Max Nikulin
2022-09-19 10:04 ` Fraga, Eric
2022-09-20 13:26 ` Ihor Radchenko
2022-09-20 17:18   ` Juan Manuel Macías
2022-09-21  8:55     ` Ihor Radchenko
2022-09-21  9:32       ` Fraga, Eric
2022-09-21  9:49         ` Timothy
2022-09-22 13:50         ` Ihor Radchenko
2022-09-21 13:55       ` Daniel Fleischer
2022-09-21 14:51         ` Juan Manuel Macías
2022-09-21 15:21         ` Fraga, Eric
2022-09-22 13:51         ` Ihor Radchenko
2022-09-21 14:42       ` Max Nikulin
2022-09-21 14:43       ` Juan Manuel Macías
2022-09-22 14:08         ` Ihor Radchenko
2022-09-24 14:50           ` Juan Manuel Macías
2022-09-25  3:33             ` Ihor Radchenko
2022-09-25 12:06               ` Juan Manuel Macías
2022-09-26  3:56                 ` Ihor Radchenko
2022-09-26  7:47                   ` Juan Manuel Macías

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).