emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using Org-mode macros as LaTeX macros


From: Bruno Barbier
Subject: Re: Using Org-mode macros as LaTeX macros
Date: Sat, 01 Jun 2024 09:36:37 +0200

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 via email to

[Prev in Thread] Current Thread [Next in Thread]