Writing Mathematical Expressions in Markdown

This article discusses writing mathematical expressions in Markdown using KaTeX.
–
1 Writing Mathematical Expressions Using KaTeX in Markdown
KaTeX is a JavaScript library that allows you to render mathematical expressions using LaTeX syntax on web pages. KaTeX is known for being fast and lightweight, making it ideal for displaying mathematical expressions in Markdown, especially in web or static site applications like Hugo, Jekyll, or plain HTML sites.
This article will explain how to write mathematical expressions using KaTeX in Markdown, including basic setup, supported LaTeX syntax, and several usage examples. With this guide, you can create Markdown documents that present mathematical expressions professionally.
2 Why KaTeX?
KaTeX has several advantages over other libraries like MathJax:
- Speed: KaTeX is designed to render mathematical expressions instantly without affecting page performance.
- Small Size: KaTeX file size is smaller compared to other libraries.
- Markdown Compatibility: KaTeX is very suitable for use with Markdown on various platforms.
- High-Quality Output: Mathematical expressions are rendered with a neat and aesthetic appearance.
3 Preparation for Using KaTeX
Before you can write mathematical expressions with KaTeX in Markdown, you need to integrate KaTeX into your project.
4 Installing KaTeX
If you’re working with static sites like Hugo or Jekyll, download KaTeX from its official website or use a CDN. Add KaTeX scripts and styles to your HTML file.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>KaTeX Example</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/auto-render.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(document.body, {
delimiters: [
{ left: "$$", right: "$$", display: true },
{ left: "$", right: "$", display: false }
]
});
});
</script>
</head>
<body>
<h1>KaTeX Example</h1>
<p>This is an example: $E = mc^2$</p>
</body>
</html>
5 Integration with Markdown Platforms
If you’re using static site generators like Hugo or Jekyll, make sure to configure Markdown to support KaTeX. You can add KaTeX scripts and styles in the base layout (for example, baseof.html in Hugo).
6 LaTeX Syntax Supported by KaTeX
KaTeX supports most standard LaTeX/TeX syntax.
Here are some commonly used main categories:
6.1 a. Inline Mathematics
To write inline mathematical expressions, use single dollar signs ($).
Example:
$E = mc^2$
$c = \pm\sqrt{a^2 + b^2}$ and \\(f(x)=\int_{-\infty}^{\infty} \hat{f}(\xi) e^{2 \pi i \xi x} d \xi\\)
Result:
$E=mc^2$
$c = \pm\sqrt{a^2 + b^2}$ and \(f(x)=\int_{-\infty}^{\infty} \hat{f}(\xi) e^{2 \pi i \xi x} d \xi\)
6.2 b. Block Mathematics
Use double dollar signs ($$) to create mathematical expressions in block form.
Example:
$$
\int_a^b f(x) \ dx = F(b) - F(a)
$$
Result:
6.3 c. Mathematical Operators
KaTeX supports various mathematical operators, including:
- Addition: x + y
- Multiplication: x \cdot y or x \times y
- Fractions: \frac{a}{b}
- Roots: \sqrt{x}
Example:
$$
\frac{1}{\sqrt{2\pi}} e^{-\frac{x^2}{2}}
$$
Result:
6.4 d. Greek and Other Symbols
Use LaTeX syntax to write Greek or special symbols:
- Lowercase Greek: \alpha, \beta, \gamma
- Uppercase Greek: \Gamma, \Delta
- Others: \infty (infinity), \partial (partial derivative), \nabla (nabla operator)
Example:
$\alpha + \beta = \gamma$
Result:
6.5 e. Matrices and Systems of Equations
KaTeX supports matrix format using the \begin{matrix} command.
Example:
$$
\begin{bmatrix}
a & b \\
c & d
\end{bmatrix}
$$
Result:
7 Further Customization
7.1 a. Custom Delimiters
You can customize delimiters in KaTeX. For example, use for inline, or [] for blocks.
7.2 b. Visual Styles
KaTeX supports visual styles such as:
- Bold Text: \mathbf{A} for bold letters.
- Italic Letters: \mathit{A} for italic letters.
- Font Size: \small, \large, \Huge.
Example:
$$
\mathbf{F} = \frac{d}{dt} \mathit{p}
$$
Result:
7.3 c. Error Handling
KaTeX has good error handling. If you write incorrect syntax, the expression will still be rendered but will provide an error notification.
8 Mathematical Expressions with KaTeX
This is an example of using KaTeX in Markdown.
9 Inline Example
Famous formula by Einstein: $E = mc^2$
10 Block Example
Here is an integral:
11 Matrix
Matrix representation:
12 Greek Symbols
Some Greek symbols: $\alpha$, $\beta$, $\gamma$.
13 Visual Styles
Bold letters: $\mathbf{A}$. Italic letters: $\mathit{A}$.
The results will be rendered according to your KaTeX configuration.
14 Conclusion
With KaTeX, you can create beautiful and efficient mathematical expressions in Markdown.
Simple setup steps and extensive LaTeX syntax support make KaTeX an excellent choice for online mathematical needs. Make sure to adjust settings according to your project so that KaTeX runs optimally.