How to Define a LaTeX Command in Org Mode
At a glance
- 1. Define the command
Add a \newcommand definition to the Org file’s LaTeX header.
- 2. Export the document
Use the LaTeX header when exporting; configure XeLaTeX as the compiler if needed.
- 3. Configure previews
Add the definition to Org’s preview preamble too, because previews may compile fragments separately.
To define a reusable LaTeX command in an Org-mode document, add a \newcommand definition to the document’s LaTeX header. For example, you can define \R to represent the real numbers and then use it in equations.
There is one important distinction: LaTeX export and Org’s formula previews may use different preambles. A definition that works in the exported PDF is not necessarily available when Org renders an inline preview.
Define a command for LaTeX export
Use the #+LATEX_HEADER: keyword near the beginning of the Org file. Each such line adds LaTeX to the preamble of the exported document.
#+LATEX_HEADER: \usepackage{amsmath,amssymb}
#+LATEX_HEADER: \newcommand{\R}{\mathbb{R}}
Let $x \in \R$. Then $x^2 \geq 0$.
The first header line loads packages that provide \mathbb; the second defines \R. When you export to LaTeX or PDF, the command is available in the document. You can also define a command with an argument, for example:
#+LATEX_HEADER: \newcommand{\abs}[1]{\left|#1\right|}
Then $\abs{x+y}$ produces the absolute value of . Use \renewcommand instead of \newcommand only when you mean to replace an existing LaTeX command.
Make the command work in formula previews
Org may render a formula preview by creating and compiling a small LaTeX document containing just that formula. In that case, document-level #+LATEX_HEADER: lines might not be included in the preview’s preamble. The result can be an “undefined control sequence” error for a command that works fine in the exported PDF.
Add the macro to the preamble used for previews as well. The variable name depends on the Org version and preview implementation: older setups commonly use org-format-latex-header; newer preview systems may provide a preview-preamble variable. Check your installation with M-x describe-variable and search for org-format-latex-header or org-latex-preview-preamble. Add the definition to the variable that your preview system actually uses.
For a classic setup using org-format-latex-header, an Emacs Lisp configuration can look like this:
(setq org-format-latex-header
"\\documentclass[11pt]{article}
\\usepackage{amsmath,amssymb}
\\newcommand{\\R}{\\mathbb{R}}
\\pagestyle{empty}
\\begin{document}")
Keep the document class, package declarations, and \begin{document} in the header. Org supplies the formula and closes the document when it creates the preview. If your Org version uses a different preview-preamble variable, add the same package and macro lines there instead. Restart or reload the relevant configuration, then regenerate the previews.
Using XeLaTeX
The macro definition itself does not change when you use XeLaTeX. What matters is that the export or preview process is configured to invoke a LaTeX engine that supports the packages and fonts in your document. If the exported PDF is meant to use XeLaTeX, configure Org’s LaTeX export compiler accordingly; preview compilation is a separate path and may need its own process configuration.
A custom command that only combines standard math commands, such as \newcommand{\R}{\mathbb{R}}, generally needs no XeTeX-specific syntax. If the macro uses a package or font command, load the required package in both the export preamble and the preview preamble.
Check the definition with an example
Suppose the Org file contains:
#+LATEX_HEADER: \usepackage{amsmath,amssymb}
#+LATEX_HEADER: \newcommand{\R}{\mathbb{R}}
The interval is $[0,1] \subset \R$.
During export, LaTeX reads the header first, so it knows what \R means by the time it processes the sentence. If the PDF renders correctly but the formula preview does not, the command is probably missing from the preview preamble. Add it there, regenerate the preview, and check the Emacs messages or LaTeX log if compilation still fails.
Common mistakes
- Putting the definition in ordinary Org text. LaTeX will not treat a line of prose as a preamble definition. Use
#+LATEX_HEADER:for export or the preview-preamble setting for previews. - Assuming export and preview share a preamble. They can compile different LaTeX documents. Configure the definition in both places when you need both behaviors.
- Forgetting a required package. For example,
\mathbbrequires a suitable math package such asamsfontsoramssymb. Load it wherever the macro is compiled. - Using
\newcommandfor an existing name. LaTeX will report that the command is already defined. Choose a new name or deliberately use\renewcommand. - Expecting XeLaTeX export settings to configure previews automatically. Preview generation has its own process and may need separate engine and preamble settings.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.