/posts/writing-math-equations-in-astro
Writing Mathematical Equations in Astro
A comprehensive guide to rendering beautiful math equations in Astro markdown files
Publication date: 2024-10-15
Updated: 2024-10-16
Writing Mathematical Equations in Astro Markdown Files
As developers and technical writers, we often need to include mathematical equations in our documentation, blog posts, or educational content. In this tutorial, I’ll show you how to integrate mathematical equations into your Astro-powered website using popular math typesetting libraries.
Why Add Math Equations to Your Astro Site?
Mathematical equations are essential for:
- Technical documentation
- Scientific articles
- Educational content
- Data science and machine learning explanations
- Physics, engineering, and mathematics tutorials
Choosing a Math Rendering Library
There are two popular libraries for rendering mathematical equations on the web:
- KaTeX - Faster rendering, smaller bundle size, but supports fewer LaTeX commands
- MathJax - More comprehensive LaTeX support, but slightly larger and sometimes slower
For this tutorial, we’ll implement KaTeX as it’s lightweight and works well with Astro’s partial hydration model.
Setting Up KaTeX in Your Astro Project
Step 1: Install KaTeX
First, let’s install KaTeX and its dependencies:
npm install katex
npm install rehype-katex remark-math
Step 2: Configure Astro to Use KaTeX
Update your astro.config.mjs
file to include the KaTeX plugins:
import { defineConfig } from 'astro/config';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
export default defineConfig({
markdown: {
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],
},
});
Step 3: Add KaTeX CSS
You need to include the KaTeX CSS in your project. The easiest way is to add it to your main layout component. Open your layout file (e.g., BaseLayout.astro
or MarkdownPostLayout.astro
) and add:
---
// Your existing imports and frontmatter
---
<html lang="en">
<head>
<!-- Your existing head content -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.8/dist/katex.min.css"
integrity="sha384-GvrOXuhMATgEsSwCs4smul74iXGOixntILdUW9XmUC6+HX0sLNAK3q71HotJqlAn"
crossorigin="anonymous">
</head>
<body>
<!-- Your existing body content -->
<slot />
</body>
</html>
Writing Math Equations in Markdown
Now that we have KaTeX set up, let’s look at how to write equations in your markdown files.
Inline Equations
For inline equations, use single dollar signs $
:
The famous Einstein's equation $E = mc^2$ relates energy and mass.
This renders as: The famous Einstein’s equation $E = mc^2$ relates energy and mass.
Block Equations
For block equations (displayed on their own line), use double dollar signs $$
:
Newton's second law of motion:
$$F = m \cdot a$$
Where $F$ is force, $m$ is mass, and $a$ is acceleration.
This renders as:
Newton’s second law of motion:
$$F = m \cdot a$$
Where $F$ is force, $m$ is mass, and $a$ is acceleration.
Advanced Equation Examples
Let’s explore some more complex equations to showcase KaTeX’s capabilities:
The Quadratic Formula
$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$
Renders as:
$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$
Maxwell’s Equations
$$\begin{aligned}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{aligned}$$
Renders as:
$$\begin{aligned} \nabla \times \vec{\mathbf{B}} -, \frac1c, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \ \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \ \nabla \times \vec{\mathbf{E}}, +, \frac1c, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \ \nabla \cdot \vec{\mathbf{B}} & = 0 \end{aligned}$$
Euler’s Identity
$$e^{i\pi} + 1 = 0$$
Renders as:
$$e^{i\pi} + 1 = 0$$
Using Math in Tables
You can also include equations in markdown tables:
| Equation | Description |
|----------|-------------|
| $E = mc^2$ | Einstein's mass-energy equivalence |
| $F = G\frac{m_1 m_2}{r^2}$ | Newton's law of universal gravitation |
| $\vec{F} = m\vec{a}$ | Newton's second law of motion |
Equation | Description |
---|---|
$E = mc^2$ | Einstein’s mass-energy equivalence |
$F = G\frac{m_1 m_2}{r^2}$ | Newton’s law of universal gravitation |
$\vec{F} = m\vec{a}$ | Newton’s second law of motion |
Troubleshooting Common Issues
Equations Not Rendering
If your equations aren’t rendering correctly, check the following:
- Verify that the KaTeX CSS is properly loaded
- Check for syntax errors in your LaTeX equations
- Ensure there are no spaces between the dollar signs and your equation
Escaping Special Characters
Sometimes you need to use dollar signs as actual currency symbols, not as math delimiters. In these cases, you can escape the dollar sign with a backslash:
The product costs \$25, not $25 = 5^2$.
Alternative: Using MathJax
If you prefer MathJax over KaTeX, the setup is slightly different:
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
// MathJax doesn't need special remark/rehype plugins
});
Then add the MathJax script to your layout:
<script>
window.MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']],
},
svg: {
fontCache: 'global'
}
};
</script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
Conclusion
Adding mathematical equations to your Astro markdown files enhances your content’s expressiveness and professionalism. Whether you’re writing technical documentation, educational content, or scientific articles, the ability to include properly formatted equations is invaluable.
By following this guide, you’ve learned:
- How to set up KaTeX in your Astro project
- How to write inline and block equations
- Examples of complex mathematical formulas
- Troubleshooting common issues
- An alternative approach using MathJax
Now you’re ready to create content with beautiful mathematical equations that render perfectly on your Astro-powered website!