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; 18+ 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] 18+ messages in thread

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  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
  2 siblings, 0 replies; 18+ messages in thread
From: Max Nikulin @ 2022-09-18 16:14 UTC (permalink / raw)
  To: emacs-orgmode

On 18/09/2022 19:27, Juan Manuel Macías wrote:
> 
> * Section
>   :PROPERTIES:
>   :presec:  \begingroup\foo
>   :postsec: \endgroup
>   :END:

Juan Manuel, can it be implemented using a derived backend that calls 
the original variant of headline transcoder (`org-latex-headline') and 
adds pre/post code to the returned value? I have not tried to implement 
this idea, so perhaps I just have missed something obvious. Actually I 
wonder if the purpose of your patch is solely convenience or you faced a 
limitation with no reasonable workaround.

My only real objection is that the new keywords works for LaTeX only, 
but their names are rather generic. Moreover, Org's term is "headline", 
not "section". I would consider something like
- :headline_pre_latex: \begingroup\foo
- :headline_pre: :latex \begingroup\foo

By the way, are affiliated keywords a part of the following headline?

* Section One

#+attr_latex: :headline_pre \begingroup\foo
* Section Two



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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  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
  2 siblings, 0 replies; 18+ messages in thread
From: Fraga, Eric @ 2022-09-19 10:04 UTC (permalink / raw)
  To: Juan Manuel Macías; +Cc: orgmode

On Sunday, 18 Sep 2022 at 12:27, Juan Manuel Macías wrote:
> 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.

I would find something like this useful as I often have commands such as
\newpage and \appendix preceding a headline, commands that are indeed
not really part of the previous element.

I've not tried your patch: I've been on holiday [1] and have come back
to 3000+ emails so I'll be some time...

eric

Footnotes:
[1]  definition: turn all Internet access off and have a break,
     preferably with good company, food, drink, and sunshine. 

-- 
: Eric S Fraga, with org release_9.5.4-768-g5bb699 in Emacs 29.0.50

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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  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
  2 siblings, 1 reply; 18+ messages in thread
From: Ihor Radchenko @ 2022-09-20 13:26 UTC (permalink / raw)
  To: Juan Manuel Macías; +Cc: orgmode

Juan Manuel Macías <maciaschain@posteo.net> writes:

> 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:

I do not like this idea.
Please remember that headlines may be exported as parts, sections,
subsections, list items, or paragraphs depending on the headline level.
Arbitrary pre/post commands may unexpectedly break things during export.

However, I do agree that per-heading control over latex export is
currently cumbersome.

The canonical ox-latex approach to customize headline export is
org-latex-classes variable. This variable defines (among other things)
pre/post commands during headline export:

>> The sectioning structure of the class is given by the elements
>> following the header string.  For each sectioning level, a number
>> of strings is specified.  A %s formatter is mandatory in each
>> section string and will be replaced by the title of the section.
>> 
>> Instead of a cons cell (numbered . unnumbered), you can also
>> provide a list of 2 or 4 elements,
>> 
>>   (numbered-open numbered-close)
>> 
>> or
>> 
>>   (numbered-open numbered-close unnumbered-open unnumbered-close)
>> 
>> providing opening and closing strings for a LaTeX environment
>> that should represent the document section.  The opening clause
>> should have a %s to represent the section title.
>> 
>> Instead of a list of sectioning commands, you can also specify
>> a function name.  That function will be called with two
>> parameters, the (reduced) level of the headline, and a predicate
>> non-nil when the headline should be numbered.  It must return
>> a format string in which the section title will be added.

What about allowing to customize these open/close statements on
per-heading level during export? This will be more consistent with the
existing ox-latex structure.

-- 
Ihor Radchenko,
Org mode contributor,
Learn more about Org mode at https://orgmode.org/.
Support Org development at https://liberapay.com/org-mode,
or support my work at https://liberapay.com/yantar92


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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  2022-09-20 13:26 ` Ihor Radchenko
@ 2022-09-20 17:18   ` Juan Manuel Macías
  2022-09-21  8:55     ` Ihor Radchenko
  0 siblings, 1 reply; 18+ messages in thread
From: Juan Manuel Macías @ 2022-09-20 17:18 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: orgmode

Ihor Radchenko writes:

> I do not like this idea.
> Please remember that headlines may be exported as parts, sections,
> subsections, list items, or paragraphs depending on the headline level.
> Arbitrary pre/post commands may unexpectedly break things during export.

I don't see why, if the user knows LaTeX and knows what he/she is doing.
Sometimes it's just adding an "\addtocontents" just before the
section/subsection,etc. The property that adds the string before and the
property that adds the string after are understood to affect the entire
heading at the current level and its contents, including lower levels.
For example, if someone wants the current heading (and all its
sublevels) not to be included in the TOC but to be included in the
headers of the pages, it would suffice to (I keep here the original name
of the properties that I proposed in the patch, but I think Maxim's
proposed name is more accurate):

-----------------------------------

* Section
  :PROPERTIES:
  :presec:  \setcounter{secnumdepth}{0}
  :presec+:  \addtocontents{toc}{\protect\setcounter{tocdepth}{0}\ignorespaces}
  :postsec: \setcounter{secnumdepth}{2}
  :postsec+: \addtocontents{toc}{\protect\setcounter{tocdepth}{2}\ignorespaces}
  :END:
Lorem ipsum dolor.

** Subsection one
lorem

** Subsection two
ipsum

Which would pass to LaTeX as:

\setcounter{secnumdepth}{0} \addtocontents{toc}{\protect\setcounter{tocdepth}{0}\ignorespaces}

\section{Section}
Lorem ipsum dolor.
\subsection{Subsection one}
lorem
\subsection{Subsection two}
ipsum

\setcounter{secnumdepth}{2} \addtocontents{toc}{\protect\setcounter{tocdepth}{2}\ignorespaces}
----------------------------------

(The above can even be simplified from LaTeX by defining a simple
environment, but I've exemplified it like this to make it look better).

In what situations might this return unexpected results?

> However, I do agree that per-heading control over latex export is
> currently cumbersome.
>
> The canonical ox-latex approach to customize headline export is
> org-latex-classes variable. This variable defines (among other things)
> pre/post commands during headline export:

Apologies in advance if I misunderstood what you're suggesting, but
isn't the "org-latex-classes" property supposed to affect the structure
of the entire document? What I'm proposing here is rather something
specific to particular headings (and its entire content), like the
":ALT_TITLE:" property. If I understand correctly, what you are
suggesting is that org-latex-classes can have "local values" for
specific headings, if such headings are 'marked' with some property?

Best regards,

Juan Manuel 

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

https://juanmanuelmacias.com

https://lunotipia.juanmanuelmacias.com

https://gnutas.juanmanuelmacias.com



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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  2022-09-20 17:18   ` Juan Manuel Macías
@ 2022-09-21  8:55     ` Ihor Radchenko
  2022-09-21  9:32       ` Fraga, Eric
                         ` (3 more replies)
  0 siblings, 4 replies; 18+ messages in thread
From: Ihor Radchenko @ 2022-09-21  8:55 UTC (permalink / raw)
  To: Juan Manuel Macías, Daniel Fleischer; +Cc: orgmode

Adding Daniel Fleischer (the ox-latex maintainer) to this exchange.

Juan Manuel Macías <maciaschain@posteo.net> writes:

> Ihor Radchenko writes:
>
>> I do not like this idea.
>> Please remember that headlines may be exported as parts, sections,
>> subsections, list items, or paragraphs depending on the headline level.
>> Arbitrary pre/post commands may unexpectedly break things during export.
>
> I don't see why, if the user knows LaTeX and knows what he/she is doing.
> Sometimes it's just adding an "\addtocontents" just before the
> section/subsection,etc. The property that adds the string before and the
> property that adds the string after are understood to affect the entire
> heading at the current level and its contents, including lower levels.
> For example, if someone wants the current heading (and all its
> sublevels) not to be included in the TOC but to be included in the
> headers of the pages, it would suffice to (I keep here the original name
> of the properties that I proposed in the patch, but I think Maxim's
> proposed name is more accurate):
>
> -----------------------------------
>
> * Section
>   :PROPERTIES:
>   :presec:  \setcounter{secnumdepth}{0}
>   :presec+:  \addtocontents{toc}{\protect\setcounter{tocdepth}{0}\ignorespaces}
>   :postsec: \setcounter{secnumdepth}{2}
>   :postsec+: \addtocontents{toc}{\protect\setcounter{tocdepth}{2}\ignorespaces}
>   :END:
> Lorem ipsum dolor.

There is nothing wrong about this, but I feel that this kind of approach
is encouraging to shoot your own leg a bit too much. It will be better
if Org provides a semantics that is facilitating more safe approach.
More below.

> Which would pass to LaTeX as:
>
> \setcounter{secnumdepth}{0} \addtocontents{toc}{\protect\setcounter{tocdepth}{0}\ignorespaces}
>
> \section{Section}
> Lorem ipsum dolor.
> \subsection{Subsection one}
> lorem
> \subsection{Subsection two}
> ipsum
>
> \setcounter{secnumdepth}{2} \addtocontents{toc}{\protect\setcounter{tocdepth}{2}\ignorespaces}
> ----------------------------------
>
> (The above can even be simplified from LaTeX by defining a simple
> environment, but I've exemplified it like this to make it look better).
>
> In what situations might this return unexpected results?

It may produce unexpected results if "Section" heading is demoted all
the way to paragraph. Also, :presec/:postsec property names are
confusing --- it is unclear if they are specific to LaTeX. (when about,
say, Beamer)

>> However, I do agree that per-heading control over latex export is
>> currently cumbersome.
>>
>> The canonical ox-latex approach to customize headline export is
>> org-latex-classes variable. This variable defines (among other things)
>> pre/post commands during headline export:
>
> Apologies in advance if I misunderstood what you're suggesting, but
> isn't the "org-latex-classes" property supposed to affect the structure
> of the entire document? What I'm proposing here is rather something
> specific to particular headings (and its entire content), like the
> ":ALT_TITLE:" property. If I understand correctly, what you are
> suggesting is that org-latex-classes can have "local values" for
> specific headings, if such headings are 'marked' with some property?

Yes, org-latex-classes is controlling the entire document. What I am
proposing (as an alternative) is subtree-level equivalent of
org-latex-classes that is also close to org-latex-classes semantics.

More concretely, I mean something like

* Section
  :PROPERTIES:
  :attr_latex: :prepend "section" \setcounter{secnumdepth}{0}
  :attr_latex+: :prepend "section" \addtocontents{toc}{\protect\setcounter{tocdepth}{0}\ignorespaces}
  :attr_latex+: :append "section" \setcounter{secnumdepth}{2}
  :attr_latex+: :append "section" \addtocontents{toc}{\protect\setcounter{tocdepth}{2}\ignorespaces}
  :END:

I suggest to use more canonical attr_latex that explicitly limits the
export backend.

Further, it mentions a regexp limiting the applicable LaTeX environment
("section"). In other environments, the code will be omitted.

-- 
Ihor Radchenko,
Org mode contributor,
Learn more about Org mode at https://orgmode.org/.
Support Org development at https://liberapay.com/org-mode,
or support my work at https://liberapay.com/yantar92


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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  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
                         ` (2 subsequent siblings)
  3 siblings, 2 replies; 18+ messages in thread
From: Fraga, Eric @ 2022-09-21  9:32 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Juan Manuel Macías, Daniel Fleischer, orgmode

On Wednesday, 21 Sep 2022 at 16:55, Ihor Radchenko wrote:
> More concretely, I mean something like
>
> * Section
>   :PROPERTIES:
>   :attr_latex: :prepend "section" \setcounter{secnumdepth}{0}
>   :attr_latex+: :prepend "section" \addtocontents{toc}{\protect\setcounter{tocdepth}{0}\ignorespaces}
>   :attr_latex+: :append "section" \setcounter{secnumdepth}{2}
>   :attr_latex+: :append "section" \addtocontents{toc}{\protect\setcounter{tocdepth}{2}\ignorespaces}
>   :END:

This looks fine to me.  I don't particularly care about the actual
syntax.  However, a minor point out of curiosity: why not just ":latex:"
for the property name?

And I would hope to have ":beamer:" (or ":attr_beamer:") as well!

Thank you,
eric
-- 
: Eric S Fraga, with org release_9.5.4-768-g5bb699 in Emacs 29.0.50

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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  2022-09-21  9:32       ` Fraga, Eric
@ 2022-09-21  9:49         ` Timothy
  2022-09-22 13:50         ` Ihor Radchenko
  1 sibling, 0 replies; 18+ messages in thread
From: Timothy @ 2022-09-21  9:49 UTC (permalink / raw)
  To: emacs-orgmode
  Cc: Ihor Radchenko, Juan Manuel Macías, Daniel Fleischer, emacs-orgmode

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

Hi Eric,

> And I would hope to have “:beamer:” (or “:attr_beamer:”) as well!

I’d hope that pre/post-ending attributes would be backend-generalised.

All the best,
Timothy

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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  2022-09-21  8:55     ` Ihor Radchenko
  2022-09-21  9:32       ` Fraga, Eric
@ 2022-09-21 13:55       ` Daniel Fleischer
  2022-09-21 14:51         ` Juan Manuel Macías
                           ` (2 more replies)
  2022-09-21 14:42       ` Max Nikulin
  2022-09-21 14:43       ` Juan Manuel Macías
  3 siblings, 3 replies; 18+ messages in thread
From: Daniel Fleischer @ 2022-09-21 13:55 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Juan Manuel Macías, orgmode

Ihor Radchenko [2022-09-21 Wed 16:55] wrote:

> More concretely, I mean something like
>
> * Section
>   :PROPERTIES:
>   :attr_latex: :prepend "section" \setcounter{secnumdepth}{0}
>   :attr_latex+: :prepend "section" \addtocontents{toc}{\protect\setcounter{tocdepth}{0}\ignorespaces}
>   :attr_latex+: :append "section" \setcounter{secnumdepth}{2}
>   :attr_latex+: :append "section" \addtocontents{toc}{\protect\setcounter{tocdepth}{2}\ignorespaces}
>   :END:
>
> I suggest to use more canonical attr_latex that explicitly limits the
> export backend.

I don't understand the usecase, that's why I wasn't really following. If
you write \vspace{10cm} before some headline, it's going to do the right
thing even if it "belongs" to a previous headline.

If org-latex-classes is not a good enough solution I also think it should
be a general export feature, not specific to latex. In that case you
need a general syntax, e.g. properties like "export_prefix",
"export_postfix" and the code should be as simple as possible, i.e.
copying the text one line before/after the headline. 

-- 
Daniel Fleischer


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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  2022-09-21  8:55     ` Ihor Radchenko
  2022-09-21  9:32       ` Fraga, Eric
  2022-09-21 13:55       ` Daniel Fleischer
@ 2022-09-21 14:42       ` Max Nikulin
  2022-09-21 14:43       ` Juan Manuel Macías
  3 siblings, 0 replies; 18+ messages in thread
From: Max Nikulin @ 2022-09-21 14:42 UTC (permalink / raw)
  To: emacs-orgmode

On 21/09/2022 15:55, Ihor Radchenko wrote:
> 
> There is nothing wrong about this, but I feel that this kind of approach
> is encouraging to shoot your own leg a bit too much.

I am afraid, it is unavoidable facet of flexibility. Anyway arbitrary 
LaTeX command may be inserted using export snippet or block.

> Also, :presec/:postsec property names are
> confusing --- it is unclear if they are specific to LaTeX. (when about,
> say, Beamer)

An example of use case for beamer:

M. ‘quintus’ Gülker. Beamer export: Executing LaTeX between two frames. 
Tue, 21 Jun 2022 10:01:03 +0200. 
https//list.orgmode.org/87mte6tphc.fsf@guelker.eu

> * Section
>    :PROPERTIES:
>    :attr_latex: :prepend "section" \setcounter{secnumdepth}{0}
>    :attr_latex+: :prepend "section" \addtocontents{toc}{\protect\setcounter{tocdepth}{0}\ignorespaces}
>    :attr_latex+: :append "section" \setcounter{secnumdepth}{2}
>    :attr_latex+: :append "section" \addtocontents{toc}{\protect\setcounter{tocdepth}{2}\ignorespaces}
>    :END:
> 
> I suggest to use more canonical attr_latex that explicitly limits the
> export backend.

The only objection is that for ox-html users may expect any attr_html 
key-value pairs directly become attributes of the HTML element rather 
than control of output at higher level.



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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  2022-09-21  8:55     ` Ihor Radchenko
                         ` (2 preceding siblings ...)
  2022-09-21 14:42       ` Max Nikulin
@ 2022-09-21 14:43       ` Juan Manuel Macías
  2022-09-22 14:08         ` Ihor Radchenko
  3 siblings, 1 reply; 18+ messages in thread
From: Juan Manuel Macías @ 2022-09-21 14:43 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: orgmode

Ihor Radchenko writes:

> It may produce unexpected results if "Section" heading is demoted all
> the way to paragraph.

If Section heading is demoted all the way to paragraph, I assume that
the expected will happen: that in the output to LaTeX a string will be
added before \paragraph and another string after the content of
\paragraph, which is perfectly consistent with the behavior of these two
properties. It is not that I intend to insist on a discussion; I just
don't quite understand what you mean. Note that these properties are for
LaTeX fine-tuning, and the user is expected to know what he/she wants
and where he/she wants it. If a user wants the arbitrary LaTeX code
before a certain header to be exported as a section (because, for
example, he/she has defined a command in LaTeX that changes the style of
the next \section), you would expect to put those properties in a
"\section" heading.

> Also, :presec/:postsec property names are
> confusing --- it is unclear if they are specific to LaTeX. (when about,
> say, Beamer)

Yes, I agree with that, and I had already commented on it in my previous
message, based on what Maxim had pointed out before, that the names I
had chosen were too imprecise. I like part of what you propose below:
`:attr_latex: :prepend'.

>>> However, I do agree that per-heading control over latex export is
>>> currently cumbersome.
>>>
>>> The canonical ox-latex approach to customize headline export is
>>> org-latex-classes variable. This variable defines (among other things)
>>> pre/post commands during headline export:
>>
>> Apologies in advance if I misunderstood what you're suggesting, but
>> isn't the "org-latex-classes" property supposed to affect the structure
>> of the entire document? What I'm proposing here is rather something
>> specific to particular headings (and its entire content), like the
>> ":ALT_TITLE:" property. If I understand correctly, what you are
>> suggesting is that org-latex-classes can have "local values" for
>> specific headings, if such headings are 'marked' with some property?
>
> Yes, org-latex-classes is controlling the entire document. What I am
> proposing (as an alternative) is subtree-level equivalent of
> org-latex-classes that is also close to org-latex-classes semantics.
>
> More concretely, I mean something like
>
> * Section
>   :PROPERTIES:
>   :attr_latex: :prepend "section" \setcounter{secnumdepth}{0}
>   :attr_latex+: :prepend "section" \addtocontents{toc}{\protect\setcounter{tocdepth}{0}\ignorespaces}
>   :attr_latex+: :append "section" \setcounter{secnumdepth}{2}
>   :attr_latex+: :append "section" \addtocontents{toc}{\protect\setcounter{tocdepth}{2}\ignorespaces}
>   :END:
>
> I suggest to use more canonical attr_latex that explicitly limits the
> export backend.

I see. But in any case, something like `:prepend "section"' would be
unnecessary (and even counterproductive) for what I'm proposing, but I'm
afraid I didn't explain myself well in the first message. One of the
benefits of approaching this issue with a few minor modifications to
org-latex-headline is that the result is regardless of the section level
at which the property is applied. We may want to prefix the section with
a specific LaTeX code only for \section (or \paragraph or whatever) and
we may want to introduce a more general LaTeX code, level-agnostic.
Explicitly put "section", "subsection", etc, IMHO unnecessarily
complicates things. But I also insist (as I said at the beginning) that
I don't know if this use case can also be extended to other users.

Best regards,

Juan Manuel 

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

https://juanmanuelmacias.com

https://lunotipia.juanmanuelmacias.com

https://gnutas.juanmanuelmacias.com




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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  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
  2 siblings, 0 replies; 18+ messages in thread
From: Juan Manuel Macías @ 2022-09-21 14:51 UTC (permalink / raw)
  To: Daniel Fleischer; +Cc: Ihor Radchenko, orgmode

Daniel Fleischer writes:

> I don't understand the usecase, that's why I wasn't really following. If
> you write \vspace{10cm} before some headline, it's going to do the right
> thing even if it "belongs" to a previous headline.

Imagine, for example, that you have defined a LaTeX command that changes
the style of the section (or chapter, subsection, or whatever) below.
And you want to apply it before a certain section. If for any reason you
comment out the preceding section, or mark it as non-exportable, or even
delete it, the preceding command is no longer there when you export to
LaTeX. That would also happen if you move the section.

Best regards,

Juan Manuel

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

https://juanmanuelmacias.com

https://lunotipia.juanmanuelmacias.com

https://gnutas.juanmanuelmacias.com




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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  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
  2 siblings, 0 replies; 18+ messages in thread
From: Fraga, Eric @ 2022-09-21 15:21 UTC (permalink / raw)
  To: Daniel Fleischer; +Cc: Ihor Radchenko, Juan Manuel Macías, orgmode

On Wednesday, 21 Sep 2022 at 16:55, Daniel Fleischer wrote:
> I don't understand the usecase, that's why I wasn't really following. If
> you write \vspace{10cm} before some headline, it's going to do the right
> thing even if it "belongs" to a previous headline.

For me, the issue is that I often have many sections that are commented
out or not selected for export.  I may also move sections around.  In
these cases, the "pre" text ends up in the wrong place or not in effect
at all.

-- 
: Eric S Fraga, with org release_9.5.4-768-g5bb699 in Emacs 29.0.50

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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  2022-09-21  9:32       ` Fraga, Eric
  2022-09-21  9:49         ` Timothy
@ 2022-09-22 13:50         ` Ihor Radchenko
  1 sibling, 0 replies; 18+ messages in thread
From: Ihor Radchenko @ 2022-09-22 13:50 UTC (permalink / raw)
  To: Fraga, Eric; +Cc: Juan Manuel Macías, Daniel Fleischer, orgmode

"Fraga, Eric" <e.fraga@ucl.ac.uk> writes:

> This looks fine to me.  I don't particularly care about the actual
> syntax.  However, a minor point out of curiosity: why not just ":latex:"
> for the property name?

Because attr_backend is our convention for export attributes.

> And I would hope to have ":beamer:" (or ":attr_beamer:") as well!

Yup. And also #+attr_backend: affiliated keywords for non-headlines.

-- 
Ihor Radchenko,
Org mode contributor,
Learn more about Org mode at https://orgmode.org/.
Support Org development at https://liberapay.com/org-mode,
or support my work at https://liberapay.com/yantar92


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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  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
  2 siblings, 0 replies; 18+ messages in thread
From: Ihor Radchenko @ 2022-09-22 13:51 UTC (permalink / raw)
  To: Daniel Fleischer; +Cc: Juan Manuel Macías, orgmode

Daniel Fleischer <danflscr@gmail.com> writes:

> If org-latex-classes is not a good enough solution I also think it should
> be a general export feature, not specific to latex. In that case you
> need a general syntax, e.g. properties like "export_prefix",
> "export_postfix" and the code should be as simple as possible, i.e.
> copying the text one line before/after the headline. 

I agree that it will fit the general export better.

-- 
Ihor Radchenko,
Org mode contributor,
Learn more about Org mode at https://orgmode.org/.
Support Org development at https://liberapay.com/org-mode,
or support my work at https://liberapay.com/yantar92


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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  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
  0 siblings, 1 reply; 18+ messages in thread
From: Ihor Radchenko @ 2022-09-22 14:08 UTC (permalink / raw)
  To: Juan Manuel Macías; +Cc: orgmode

Juan Manuel Macías <maciaschain@posteo.net> writes:

>> I suggest to use more canonical attr_latex that explicitly limits the
>> export backend.
>
> I see. But in any case, something like `:prepend "section"' would be
> unnecessary (and even counterproductive) for what I'm proposing, but I'm
> afraid I didn't explain myself well in the first message. One of the
> benefits of approaching this issue with a few minor modifications to
> org-latex-headline is that the result is regardless of the section level
> at which the property is applied. We may want to prefix the section with
> a specific LaTeX code only for \section (or \paragraph or whatever) and
> we may want to introduce a more general LaTeX code, level-agnostic.
> Explicitly put "section", "subsection", etc, IMHO unnecessarily
> complicates things. But I also insist (as I said at the beginning) that
> I don't know if this use case can also be extended to other users.

Yeah. Extra matcher is probably too cumbersome.
Yet, I feel like conditional prefix/suffix may be useful in some
scenarios.

Having read the available replies in this thread, I am thinking of the
following:

1. Instead of explicit prefix and suffix, we can unify extra text around
   the exported Org element to a template:

* headline
:PROPERTIES:
:ATTR_BACKEND: :export_template "\begin{myenv}\n%s\n\end{myenv}"
:ATTR_BACKEND+: "The %%s instances are replaced by the exported element"
:ATTR_BACKEND+: (concat "arbitrary sexp, the exported element is bound to: " *this*)
:ATTR_BACKEND+: babel_block_name(exported=*this*)
:ATTR_BACKEND+: "the property lines are concatenated with \" \" (space),"
:ATTR_BACKEND+: "just like the usual approach in `org-export-read-attribute'"
:END:

#+ATTR_BACKEND: :export_template "can also work on non-headings"
Paragraph.

2. The generic Org export routine will remove the :export_template
   attributes prior to passing the element to backend-defined export
   transcoder, thus avoiding the problem Max raised wrt ox-html
   attributes.

3. Similar to :export_template, we can have
   :export_prefix/:export_suffix, but I feel that the template will be
   more flexible.

WDYT?

-- 
Ihor Radchenko,
Org mode contributor,
Learn more about Org mode at https://orgmode.org/.
Support Org development at https://liberapay.com/org-mode,
or support my work at https://liberapay.com/yantar92


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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  2022-09-22 14:08         ` Ihor Radchenko
@ 2022-09-24 14:50           ` Juan Manuel Macías
  2022-09-25  3:33             ` Ihor Radchenko
  0 siblings, 1 reply; 18+ messages in thread
From: Juan Manuel Macías @ 2022-09-24 14:50 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: orgmode

Hi, Ihor, sorry for the late reply,

Ihor Radchenko writes:

> Having read the available replies in this thread, I am thinking of the
> following:
>
> 1. Instead of explicit prefix and suffix, we can unify extra text around
>    the exported Org element to a template:
>
> * headline
> :PROPERTIES:
> :ATTR_BACKEND: :export_template "\begin{myenv}\n%s\n\end{myenv}"
> :ATTR_BACKEND+: "The %%s instances are replaced by the exported element"
> :ATTR_BACKEND+: (concat "arbitrary sexp, the exported element is bound to: " *this*)
> :ATTR_BACKEND+: babel_block_name(exported=*this*)
> :ATTR_BACKEND+: "the property lines are concatenated with \" \" (space),"
> :ATTR_BACKEND+: "just like the usual approach in `org-export-read-attribute'"
> :END:

I really like this approach and I would buy it. On the one hand, if I
understand correctly, it's a universal solution that doesn't depend on a
particular backend (although, to be honest, I don't see much use for
this beyond LaTeX: maybe in HTML). And, on the other hand,
`:export_template' is an attribute that can be, as you say, very
versatile. With this, in my opinion, it would no longer be necessary to
define two 'pre' and 'post' attributes.

I imagine the value of ATTR_BACKEND (would quotes be necessary?) could
be easily converted to a plist, with code borrowed from
`org-export-read-attribute':

(:export_template "\\begin{myenv}\\n%s\\n\\end{myenv} ... etc. ...")

> #+ATTR_BACKEND: :export_template "can also work on non-headings"
> Paragraph.

In this case I would not see it necessary, IMHO. For simple things (of
the begin/end style) there are the special blocks. And for more complex
pre- and/or post- code we have export blocks and export snippets. Since
there is no heading involved here, there would be no danger of the
pre-code leaving with the content of the previous header.

Best regards,

Juan Manuel 

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

https://juanmanuelmacias.com

https://lunotipia.juanmanuelmacias.com

https://gnutas.juanmanuelmacias.com



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

* Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section
  2022-09-24 14:50           ` Juan Manuel Macías
@ 2022-09-25  3:33             ` Ihor Radchenko
  0 siblings, 0 replies; 18+ messages in thread
From: Ihor Radchenko @ 2022-09-25  3:33 UTC (permalink / raw)
  To: Juan Manuel Macías; +Cc: orgmode

Juan Manuel Macías <maciaschain@posteo.net> writes:

>> #+ATTR_BACKEND: :export_template "can also work on non-headings"
>> Paragraph.
>
> In this case I would not see it necessary, IMHO. For simple things (of
> the begin/end style) there are the special blocks. And for more complex
> pre- and/or post- code we have export blocks and export snippets. Since
> there is no heading involved here, there would be no danger of the
> pre-code leaving with the content of the previous header.

I do not insist on this. I just see supporting this easier (it will work
automatically) compared to explicitly limiting :export_template to
headings/inlinetasks.

Also, some people prefer to have such option (Pedro Andres Aranda
Gutierrez in off-list reply to
https://orgmode.org/list/87fsgmyyhw.fsf@localhost)

-- 
Ihor Radchenko,
Org mode contributor,
Learn more about Org mode at https://orgmode.org/.
Support Org development at https://liberapay.com/org-mode,
or support my work at https://liberapay.com/yantar92


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

end of thread, other threads:[~2022-09-25  3:33 UTC | newest]

Thread overview: 18+ 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

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).