How to Use LaTeX Macros in Markdown
At a glance
- 1. Check math support
Confirm that your Markdown editor or publishing platform renders LaTeX math at all.
- 2. Choose the macro location
Define macros in the math renderer's configuration when possible; inline definitions are not portable.
- 3. Use the macro
Write the macro inside supported math delimiters, such as dollar signs or double dollar signs.
- 4. Test the output
Preview your document on its destination platform because Markdown engines handle macros differently.
You can use LaTeX-style macros in Markdown only if the Markdown tool you use renders mathematical notation and supports those macros. Markdown does not have a standard macro system, and a command such as \newcommand is not guaranteed to work just because it appears in a Markdown file.
The reliable approach is to define a macro in the math renderer’s configuration, then call it inside the renderer’s math delimiters. The exact configuration depends on your editor, website, or Markdown conversion tool, so preview the result in the environment where you intend to publish it.
Markdown and LaTeX are different layers
Markdown is a lightweight format for ordinary text, headings, lists, and links. Mathematical expressions are usually handled by an additional component, such as MathJax or KaTeX. That component interprets math syntax such as $x^2$; it may also support custom macros.
A macro defined in a .tex file does not automatically become available in a Markdown document. Likewise, a macro definition that works in one site’s Markdown editor may fail in another, because the sites can use different renderers or configurations.
Define a macro in the renderer
Suppose you want \R to stand for the real numbers. A renderer configuration might define the macro using a mapping like this:
"\\R": "\\mathbb{R}"
This is an illustrative configuration value, not a complete setup command: the surrounding syntax and the place where you enter it vary by tool. For example, KaTeX integrations commonly accept a macros option, while MathJax can be configured with TeX macros. Check the documentation for your particular Markdown application or site to find the correct configuration format.
Once configured, your Markdown math can use the macro:
Let $x \in \R$. Then $x^2 \geq 0$.
$$
\R = (-\infty,\infty)
$$
The first example uses inline math, and the second uses display math. The renderer must recognize both the delimiters and the macro. Some Markdown systems use different delimiters or require an option to enable math.
What \newcommand does in a LaTeX document
In an ordinary LaTeX document, \newcommand defines a command for the rest of the document. This complete example compiles with a standard LaTeX installation:
\documentclass{article}
\usepackage{amssymb}
\newcommand{\R}{\mathbb{R}}
\begin{document}
Let $x \in \R$. Then $x^2 \geq 0$.
\end{document}
Here, \newcommand{\R}{\mathbb{R}} defines \R, and amssymb provides \mathbb. This example shows the LaTeX syntax, but it is not a guarantee that the same definition can be pasted into any Markdown file. A Markdown renderer might allow definitions inside math, require them in site configuration, or disallow them entirely.
Can you put \newcommand in the Markdown itself?
Sometimes. Whether a definition such as \newcommand{\R}{\mathbb{R}} works in a Markdown document depends on the math renderer and how the host platform handles definitions. Some setups permit macro definitions in math input; others require configuration, restrict definitions, or do not preserve them between separate expressions.
For a document that must work across multiple platforms, configure the macro in each renderer rather than relying on a definition embedded in the Markdown. If you cannot change the renderer configuration, use the full expression, such as \mathbb{R}, instead of a custom shortcut.
Common mistakes
- Assuming Markdown defines macros. It does not; a math extension or renderer has to process the command.
- Putting a definition in a code block. Code blocks display text literally, so math commands inside them are not evaluated.
- Defining a macro outside math delimiters. Some renderers require definitions inside a math expression; others expect configuration. Follow that renderer’s rules.
- Expecting a definition to persist. A definition may apply only to one expression or one page, depending on the integration.
- Testing only in an editor. A local preview and the published site may use different renderers or settings. Test the final output.
If a macro appears literally or produces an error, first check that math rendering is enabled. Then verify the renderer’s macro syntax and scope. If neither is available, write out the full LaTeX expression instead.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.