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


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



  reply	other threads:[~2024-06-01  7:37 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 [this message]
2024-06-01 12:05   ` Sébastien Gendre
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=665acf87.050a0220.dc593.f236@mx.google.com \
    --to=brubar.cs@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=seb@k-7.ch \
    /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).