emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Sébastien Gendre" <seb@k-7.ch>
To: Bruno Barbier <brubar.cs@gmail.com>
Cc: emacs-orgmode <emacs-orgmode@gnu.org>
Subject: Re: Using Org-mode macros as LaTeX macros
Date: Sat, 01 Jun 2024 14:05:59 +0200	[thread overview]
Message-ID: <87mso4lvfc.fsf@k-7.ch> (raw)
In-Reply-To: <665acf87.050a0220.dc593.f236@mx.google.com> (Bruno Barbier's message of "Sat, 01 Jun 2024 09:36:37 +0200")


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>


  reply	other threads:[~2024-06-01 12:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2024-06-01 18:06     ` Berry, Charles

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87mso4lvfc.fsf@k-7.ch \
    --to=seb@k-7.ch \
    --cc=brubar.cs@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).