Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-26 Thread Juan Manuel Macías
Ihor Radchenko writes:

> What I have in mind is to modify `org-export-read-attribute' directly.
> Then, we can call `org-export-read-attribute' in `org-export-data';
> resolve the refs in the template (re-use code from
> `org-babel-ref-resolve' and `org-babel-parse-multiple-vars'); do normal
> export and pass it to the template. Before running the normal export, we
> strip :export_template from the INFO to avoid issues with ox-html which
> puts every single attr into the generated html.

OK, it makes sense. I can try something on this idea, although I'm
afraid not in the short term. If someone else wants to do it I have no
problem.

Best regards,

Juan Manuel 

-- 
--
--
Juan Manuel Macías 

https://juanmanuelmacias.com

https://lunotipia.juanmanuelmacias.com

https://gnutas.juanmanuelmacias.com





Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-25 Thread Ihor Radchenko
Juan Manuel Macías  writes:

> Going back to the earlier :ATTR_BACKEND: issue as a property for
> headings, I've been doing some testing and scribbled down a possible
> function[1] whose code is almost entirely stolen from
> org-export-read-attribute, with some modifications. Evaluated at the
> headline, it returns the value of the ATTR_BACKEND property as a plist.
> And then that plist could be easily manipulated on each backend to
> format export_template conveniently. For example:
>
> * headline
> :PROPERTIES:
> :ATTR_LaTeX: :export_template \begin{myenv}\n%s\n\end{myenv}
> :ATTR_LaTeX+: blah blah blah
> :END:
>
> ==> (:export_template "\\begin{myenv}\\n%s\\n\\end{myenv} blah blah blah")
>
> I don't know if that would be the way to go...

What I have in mind is to modify `org-export-read-attribute' directly.
Then, we can call `org-export-read-attribute' in `org-export-data';
resolve the refs in the template (re-use code from
`org-babel-ref-resolve' and `org-babel-parse-multiple-vars'); do normal
export and pass it to the template. Before running the normal export, we
strip :export_template from the INFO to avoid issues with ox-html which
puts every single attr into the generated html.

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



Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-25 Thread Juan Manuel Macías
Ihor Radchenko 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)

Well, it's evident that your idea of the export_template attribute is
very productive and can be consistently extended to more situations. One
scenario where I think it would be very useful is also in tables and
figures, but in this case to insert arbitrary code *inside* the table,
figure, etc. environments. This is something that gets asked on the list
from time to time and the solutions provided are usually a bit tricky.

Going back to the earlier :ATTR_BACKEND: issue as a property for
headings, I've been doing some testing and scribbled down a possible
function[1] whose code is almost entirely stolen from
org-export-read-attribute, with some modifications. Evaluated at the
headline, it returns the value of the ATTR_BACKEND property as a plist.
And then that plist could be easily manipulated on each backend to
format export_template conveniently. For example:

* headline
:PROPERTIES:
:ATTR_LaTeX: :export_template \begin{myenv}\n%s\n\end{myenv}
:ATTR_LaTeX+: blah blah blah
:END:

==> (:export_template "\\begin{myenv}\\n%s\\n\\end{myenv} blah blah blah")

I don't know if that would be the way to go...

Best regards,

Juan Manuel

[1]
(defun possible-function (attribute)
  "TODO"
  (let* ((prepare-value
  (lambda (str)
(save-match-data
  (cond ((member str '(nil "" "nil")) nil)
((string-match "^\"\\(\"+\\)?\"$" str)
 (or (match-string 1 str) ""))
(t str)
 (attributes
  (let ((value (org-entry-get nil attribute)))
(when value
  (let ((s value) result)
(while (string-match
"\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ 
\t]+\\|$\\)"
s)
  (let ((value (substring s 0 (match-beginning 0
(push (funcall prepare-value value) result))
  (push (intern (match-string 1 s)) result)
  (setq s (substring s (match-end 0
;; Ignore any string before first property with `cdr'.
(cdr (nreverse (cons (funcall prepare-value s) result
attributes))


-- 
--
--
Juan Manuel Macías 

https://juanmanuelmacias.com

https://lunotipia.juanmanuelmacias.com

https://gnutas.juanmanuelmacias.com




Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-24 Thread Ihor Radchenko
Juan Manuel Macías  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



Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-24 Thread Juan Manuel Macías
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




Re: Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-22 Thread Pedro Andres Aranda Gutierrez
Ihor writes:
> * 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.

That looks nice :-) If ypu could you do something like:

#+ATTR_BACKEND: :export_template  "\begin{myenv}[myopts]\n%s\n\end{myenv}"
#+ATTR_BACKEND+: :export_template raw_text(exported="this")
#+BEGIN_example
...
#+END_example

I would be able to do what I asked for some time ago :-)

Thanks for the patience, /PA
--
Fragen sind nicht da um beantwortet zu werden,
Fragen sind da um gestellt zu werden
Georg Kreisler

Headaches with a Juju log:
unit-basic-16: 09:17:36 WARNING juju.worker.uniter.operation we should run
a leader-deposed hook here, but we can't yet


Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-22 Thread Ihor Radchenko
Daniel Fleischer  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



Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-22 Thread Ihor Radchenko
"Fraga, Eric"  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



Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-22 Thread Ihor Radchenko
Juan Manuel Macías  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



Re: Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-22 Thread Pedro Andres Aranda Gutierrez
Ihor writes;

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

Could do if it where for the 'little' detail that a section is not an
environment in Latex
The ORG document structure mimics the LaTEX document structure. To control
headers, we need the \At... commands Juan Manuel suggests IMv(^n)HO [n>10]

My 10^-10 cents, /PA
-- 
Fragen sind nicht da um beantwortet zu werden,
Fragen sind da um gestellt zu werden
Georg Kreisler

Headaches with a Juju log:
unit-basic-16: 09:17:36 WARNING juju.worker.uniter.operation we should run
a leader-deposed hook here, but we can't yet


Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-21 Thread Fraga, Eric
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


Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-21 Thread Juan Manuel Macías
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





Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-21 Thread Daniel Fleischer
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



Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-21 Thread Max Nikulin

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





Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-21 Thread Juan Manuel Macías
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





Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-21 Thread Timothy
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


Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-21 Thread Fraga, Eric
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


Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-21 Thread Ihor Radchenko
Adding Daniel Fleischer (the ox-latex maintainer) to this exchange.

Juan Manuel Macías  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



Re: Re [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-20 Thread Juan Manuel Macías
Ihor Radchenko writes:

>> Can it be extended to add properties to a
>> #BEGIN_example
>> #END_example
>> snippet?
>
> Didn't we conclude that wrapping blocks during LaTeX export should be
> done via special blocks? (This question has been raised multiple times,
> I am unsure if you are referring to the same discussion I have in mind).

Special blocks is usually the best solution for these cases. But
(without wishing to add more fuel to old discussions) I think it would
be nice to have as attr_latex a series of positions similar to the hooks
in the etoolbox LaTeX package:

\AtBeginEnvironment 
\AtEndEnvironment 
\BeforeBeginEnvironment
\AfterEndEnvironment

Something like :pre :post :precontent :postcontent.

In the case of blocks, I think this would simplify the documents a lot
if what you are looking for makes sense only in LaTeX export (special
blocks are exported to everything).

And in the case of floating objects such as tables or figures, it would
be really useful, since here you cannot resort to the use of special
blocks (*inside* those environments, I mean), and the workarounds that are
usually provided are still a bit tricky.

Best regards,

Juan Manuel 

-- 
--
--
Juan Manuel Macías 

https://juanmanuelmacias.com

https://lunotipia.juanmanuelmacias.com

https://gnutas.juanmanuelmacias.com




Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-20 Thread Juan Manuel Macías
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




Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-20 Thread Ihor Radchenko
Juan Manuel Macías  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



Re: Re [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-20 Thread Ihor Radchenko
Pedro Andres Aranda Gutierrez  writes:

> Can it be extended to add properties to a
> #BEGIN_example
> #END_example
> snippet?

Didn't we conclude that wrapping blocks during LaTeX export should be
done via special blocks? (This question has been raised multiple times,
I am unsure if you are referring to the same discussion I have in mind).

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



Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-19 Thread Fraga, Eric
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

Re [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-19 Thread Pedro Andres Aranda Gutierrez
HI Juan Manuel,

Looks cool and would be a +1 from my side, since I need that too...

Can it be extended to add properties to a
#BEGIN_example
#END_example
snippet?

I already suggested that some time ago...

/PA
-- 
Fragen sind nicht da um beantwortet zu werden,
Fragen sind da um gestellt zu werden
Georg Kreisler

Headaches with a Juju log:
unit-basic-16: 09:17:36 WARNING juju.worker.uniter.operation we should run
a leader-deposed hook here, but we can't yet


Re: [Patch] Pre-/postpend arbitrary LaTeX code to a section

2022-09-18 Thread Max Nikulin

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