emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Using Org-mode macros as LaTeX macros
@ 2024-06-01  5:48 Sébastien Gendre
  2024-06-01  7:36 ` Bruno Barbier
  0 siblings, 1 reply; 4+ messages in thread
From: Sébastien Gendre @ 2024-06-01  5:48 UTC (permalink / raw)
  To: emacs-orgmode

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


TL;DR: How can I use the Org-mode macros as LaTeX macros inside an
       export LaTeX bloc ?



Hello,


In a document I write for my school, using Org-mode, I have defined a
Org-mode macro. Named "orga" (for organization) and who have the name of
my school as value.


Example:

#+MACRO: orga School Name


It's very useful to re-use the name of my school in the entire document.
And I can also re-use the title, author name, etc, who are defined at
the top of my Org-mode document with "#+TITLE:", "#+AUTHOR:", etc.


But my school ask for a special title page in the PDF export, with a
specific design. Something very different from what I get by default
with an Org-mode to LaTeX/PDF export.


For now, I have disabled the auto title generation, with an "#+OPTIONS:
title:nil". And I created an export LaTeX bloc at the top of my Org
document. In this bloc, I can make a custom title page in LaTeX and
export to a PDF who have the design requested by the school.

Example:

#+begin_export latex
\begin{titlepage}
  Some custom LaTeX here…
\end{titlepage}
#+end_export


But, I cannot use Org-mode macros inside this export LaTeX bloc. If I
write "{{{title}}}" in this bloc, it is exported into LaTeX with no
Org-mode macro substitution. I also tried to define a LaTeX macros, that
use the Org-mode macro value, with "#+LATEX_HEADER:" like this:

#+LATEX_HEADER:  \newcommand{\orga}{{{{orga}}}}


But the result in the LaTeX export file is:
"\newcommand{\orga}{{{{orga}}}}". And not "\newcommand{\orga}{School
Name}". No Org-mode macro substitution.

How can I use the Org-mode macros as LaTeX macros inside my export LaTeX
bloc ?

I have searched on the web, but didn't success to found a solution.


Best regards

-------
Gendre Sébastien

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 849 bytes --]

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

* Re: Using Org-mode macros as LaTeX macros
  2024-06-01  5:48 Using Org-mode macros as LaTeX macros Sébastien Gendre
@ 2024-06-01  7:36 ` Bruno Barbier
  2024-06-01 12:05   ` Sébastien Gendre
  0 siblings, 1 reply; 4+ messages in thread
From: Bruno Barbier @ 2024-06-01  7:36 UTC (permalink / raw)
  To: Sébastien Gendre, emacs-orgmode


Hi Sébastien,


Sébastien Gendre <seb@k-7.ch> writes:

> TL;DR: How can I use the Org-mode macros as LaTeX macros inside an
>        export LaTeX bloc ?
>

It looks like macros are not expanded in latex export blocks, indeed.

You may define your own filter to ask Org to expand them.

With the 2 functions below, and this configuration:

     (add-to-list 'org-export-filter-export-block-functions
                  'my-latex-filter-export-block)


the following org document:

     #+MACRO: orga School Name

     #+begin_export latex
     \begin{titlepage}
       Some custom LaTeX here
       This is my school: {{{orga}}}
     \end{titlepage}
     \newcommand{\orga}{{{{orga}}}}
     #+end_export

is exported like this:

     \begin{titlepage}
       Some custom LaTeX here
       This is my school: School Name
     \end{titlepage}
     \newcommand{\orga}{School Name}


Bruno.     




(cl-defun my-org-macro-expand-text (text &key templates)
  "Expand TEMPLATES in TEXT.
Assume the current-buffer is an org mode buffer.
If TEMPLATES is nil, use 'org-macro-templates'."
  (unless templates (setq templates org-macro-templates))
  (with-temp-buffer
    (insert text)
    (org-mode)
    (goto-char (point-min))
    ;; Extracted from 'org-macro-replace-all'
    (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
      (let ((macro (save-excursion
		                 (goto-char (match-beginning 0))
		                 (org-element-macro-parser)))
            value)
        (when macro 
	        (let* ((value (org-macro-expand macro templates))
		             (begin (org-element-begin macro)))
		        (delete-region
		         begin
		         (progn (goto-char (org-element-end macro))
			              (skip-chars-backward " \t")
			              (point)))
		        (save-excursion (insert value))))))
    (buffer-substring (point-min) (point-max))))

(defun my-latex-filter-export-block (text backend info)
  "Replace macros in LaTeX export blocks."
  (when (org-export-derived-backend-p backend 'latex)
    (my-org-macro-expand-text text)))



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

* Re: Using Org-mode macros as LaTeX macros
  2024-06-01  7:36 ` Bruno Barbier
@ 2024-06-01 12:05   ` Sébastien Gendre
  2024-06-01 18:06     ` Berry, Charles
  0 siblings, 1 reply; 4+ messages in thread
From: Sébastien Gendre @ 2024-06-01 12:05 UTC (permalink / raw)
  To: Bruno Barbier; +Cc: emacs-orgmode


Thank you for your reply, Bruno.

I need to have an Org document that could be used as easily as possible
by anyone else. Sadly, having to define new Emacs function could be too
much for what my school ask.

Or I need to make it available publicly as an Emacs package.




Bruno Barbier <brubar.cs@gmail.com> writes:

> Hi Sébastien,
>
>
> Sébastien Gendre <seb@k-7.ch> writes:
>
>> TL;DR: How can I use the Org-mode macros as LaTeX macros inside an
>>        export LaTeX bloc ?
>>
>
> It looks like macros are not expanded in latex export blocks, indeed.
>
> You may define your own filter to ask Org to expand them.
>
> With the 2 functions below, and this configuration:
>
>      (add-to-list 'org-export-filter-export-block-functions
>                   'my-latex-filter-export-block)
>
>
> the following org document:
>
>      #+MACRO: orga School Name
>
>      #+begin_export latex
>      \begin{titlepage}
>        Some custom LaTeX here
>        This is my school: {{{orga}}}
>      \end{titlepage}
>      \newcommand{\orga}{{{{orga}}}}
>      #+end_export
>
> is exported like this:
>
>      \begin{titlepage}
>        Some custom LaTeX here
>        This is my school: School Name
>      \end{titlepage}
>      \newcommand{\orga}{School Name}
>
>
> Bruno.     
>
>
>
>
> (cl-defun my-org-macro-expand-text (text &key templates)
>   "Expand TEMPLATES in TEXT.
> Assume the current-buffer is an org mode buffer.
> If TEMPLATES is nil, use 'org-macro-templates'."
>   (unless templates (setq templates org-macro-templates))
>   (with-temp-buffer
>     (insert text)
>     (org-mode)
>     (goto-char (point-min))
>     ;; Extracted from 'org-macro-replace-all'
>     (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
>       (let ((macro (save-excursion
> 		                 (goto-char (match-beginning 0))
> 		                 (org-element-macro-parser)))
>             value)
>         (when macro 
> 	        (let* ((value (org-macro-expand macro templates))
> 		             (begin (org-element-begin macro)))
> 		        (delete-region
> 		         begin
> 		         (progn (goto-char (org-element-end macro))
> 			              (skip-chars-backward " \t")
> 			              (point)))
> 		        (save-excursion (insert value))))))
>     (buffer-substring (point-min) (point-max))))
>
> (defun my-latex-filter-export-block (text backend info)
>   "Replace macros in LaTeX export blocks."
>   (when (org-export-derived-backend-p backend 'latex)
>     (my-org-macro-expand-text text)))
<#secure method=pgpmime mode=sign>


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

* Re: Using Org-mode macros as LaTeX macros
  2024-06-01 12:05   ` Sébastien Gendre
@ 2024-06-01 18:06     ` Berry, Charles
  0 siblings, 0 replies; 4+ messages in thread
From: Berry, Charles @ 2024-06-01 18:06 UTC (permalink / raw)
  To: Sébastien Gendre; +Cc: emacs-orgmode

Sebastien,

One way to propagate values prior to export is to use named src blocks and noweb references to them.

For example, the following code:

---
#+name: orga-def
#+begin_src latex :exports none
  my old school
#+end_src

#+begin_src latex :noweb yes
  \begin{titlepage}
  Some custom LaTeX here
  This is my school:
  <<orga-def>>
  \end{titlepage}
  \newcommand{\orga}{
    <<orga-def>>
  }
#+end_src
----

will export as 

---
\begin{titlepage}
Some custom LaTeX here
This is my school:
my old school
\end{titlepage}
\newcommand{\orga}{
  my old school
}
---

if the latex is activated in `org-babel-load-languages`. You can customize this easily.

Or you just can `(require 'ob-latex)`.

You can preview the latex produced by eval-ing the src block or executing `org-babel-expand-src-block` (C-c C-v C-v) to see the latex produced.

Note that I added newlines around `<<orga-def>>` which was not necessary for this example, but if multi-line codes are used in the `orga-def` block, noweb will prefix/postfix each line and that could be a headache. See `Noweb Replacement Syntax` in the manual for more detail on prefixing.

HTH,

Chuck

> On Jun 1, 2024, at 5:05 AM, Sébastien Gendre <seb@k-7.ch> wrote:
> 
> 
> I need to have an Org document that could be used as easily as possible
> by anyone else. Sadly, having to define new Emacs function could be too
> much for what my school ask.


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

end of thread, other threads:[~2024-06-01 18:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-01  5:48 Using Org-mode macros as LaTeX macros Sébastien Gendre
2024-06-01  7:36 ` Bruno Barbier
2024-06-01 12:05   ` Sébastien Gendre
2024-06-01 18:06     ` Berry, Charles

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