13.5 Macro Replacement
Macros replace text snippets during export. Macros are defined globally in org-export-global-macros
, or document-wise with the following syntax:
#+MACRO: name replacement text; $1, $2 are arguments
which can be referenced using β{{{name(arg1, arg2)}}}
β1. For example
#+MACRO: poem Rose is $1, violet's $2. Life's ordered: Org assists you.
{{{poem(red,blue)}}}
becomes
Rose is red, violet's blue. Life's ordered: Org assists you.
As a special case, Org parses any replacement text starting with β(eval
β as an Emacs Lisp expression and evaluates it accordingly. Within such templates, arguments become strings. Thus, the following macro
#+MACRO: gnustamp (eval (concat "GNU/" (capitalize $1)))
turns β{{{gnustamp(linux)}}}
β into βGNU/Linux
β during export.
Org recognizes macro references in following Org markup areas: paragraphs, headlines, verse blocks, tables cells and lists. Org also recognizes macro references in keywords, such as βCAPTION
β, βTITLE
β, βAUTHOR
β, βDATE
β, and for some back-end specific export options.
Org comes with following pre-defined macros:
β{{{keyword(NAME)}}}
ββ
β{{{title}}}
ββ
β{{{author}}}
ββ
β{{{email}}}
ββ
The βkeyword
β macro collects all values from NAME
keywords throughout the buffer, separated with white space. βtitle
β, βauthor
β and βemail
β macros are shortcuts for, respectively, β{{{keyword(TITLE)}}}
β, β{{{keyword(AUTHOR)}}}
β and β{{{keyword(EMAIL)}}}
β.
β{{{date}}}
ββ
β{{{date(FORMAT)}}}
ββ
This macro refers to the βDATE
β keyword. FORMAT
is an optional argument to the βdate
β macro that is used only if βDATE
β is a single timestamp. FORMAT
should be a format string understood by format-time-string
.
β{{{time(FORMAT)}}}
ββ
β{{{modification-time(FORMAT, VC)}}}
ββ
These macros refer to the documentβs date and time of export and date and time of modification. FORMAT
is a string understood by format-time-string
. If the second argument to the modification-time
macro is non-nil
, Org uses βvc.el
β to retrieve the documentβs modification time from the version control system. Otherwise Org reads the file attributes.
β{{{input-file}}}
ββ
This macro refers to the filename of the exported file.
β{{{property(PROPERTY-NAME)}}}
ββ
β{{{property(PROPERTY-NAME, SEARCH OPTION)}}}
ββ
This macro returns the value of property PROPERTY-NAME
in the current entry. If SEARCH-OPTION
(see Search Options) refers to a remote entry, use it instead.
β{{{n}}}
ββ
β{{{n(NAME)}}}
ββ
β{{{n(NAME, ACTION)}}}
ββ
This macro implements custom counters by returning the number of times the macro has been expanded so far while exporting the buffer. You can create more than one counter using different NAME
values. If ACTION
is β-
β, previous value of the counter is held, i.e., the specified counter is not incremented. If the value is a number, the specified counter is set to that value. If it is any other non-empty string, the specified counter is reset to 1. You may leave NAME
empty to reset the default counter.
Moreover, inline source blocks (see Structure of Code Blocks) use the special βresults
β macro to mark their output. As such, you are advised against re-defining it, unless you know what you are doing.
The surrounding brackets can be made invisible by setting org-hide-macro-markers
to a non-nil
value.
Org expands macros at the very beginning of the export process.
- Since commas separate the arguments, commas within arguments have to be escaped with the backslash character. So only those backslash characters before a comma need escaping with another backslash character.β©