# Configuring org-export to replace LaTeX def macros

I'm trying to configure org-export to replace LaTeX-def-macros while exporting to TeX or HTML.

Current situation: the header #+LATEX_HEADER: \def\R{\mathbb{R}} defines a macro that replaces \R to \mathbb{R} while compiling the TeX output. (Essentially, this will add the \def\R{\mathbb{R}} to the preamble of the TeX file---org-mode does not perform any macro-replacements)

Instead, is there some way to make org-export execute the macro-replacement, i.e., the exported TeX/HTML file should not contain \R (instead it should be replaced by \mathbb{R}.)

Possible uses: LaTeX macros can be used and the exported output will be properly rendered when in HTML, markdown, etc.

• Maybe export to tex, then call pandoc to convert the tex to html? – Tyler Jul 9 '16 at 12:14

I don't think org-export will actually do the macro expansions for you (i.e., it won't parse the macros and make the replacements), but you can add filters for each macro that you want replaced. The following code will add a filter that does the conversion you want when exporting from org to html:

(require 'ox)
(defun my-latex-filter-r-macro (text backend info)
"Replace \\R with \\mathbb{R}"
(when (org-export-derived-backend-p backend 'html)
(replace-regexp-in-string "\\\\R" "\\\\mathbb{R}" text)))


If you want the filter to act on all exports, remove the (when...) form, and just use the (replace-regexp-in-string ...) form on its own:
(defun my-latex-filter-r-macro (text backend info)

(Thanks to @Theldoria for pointing out that this needs to be added to the org-export-filter-latex-fragment-functions list!)