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, 2016 at 12:14

1 Answer 1


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

(add-to-list 'org-export-filter-latex-fragment-functions

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)
  "Replace \\R with \\mathbb{R}"
  (replace-regexp-in-string "\\\\R" "\\\\mathbb{R}" text))

For more details, see the orgmode manual.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.