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...@gmail.com> writes:

> Hi Sébastien,
>
>
> Sébastien Gendre <s...@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 via email to