Building a Modern Blog with MDX, KaTeX, and Custom Components
If you're building a technical blog or portfolio, you'll quickly realize that plain Markdown isn't enough. You need mathematical equations, interactive components, and the flexibility to extend your content beyond static text. In this tutorial, I'll show you how to combine MDX, KaTeX, and custom React components to create a powerful content authoring system.
What is MDX and Why Should You Care?
MDX is Markdown on steroids. It lets you seamlessly integrate JSX (React components) directly into your Markdown files. This means you can write regular Markdown for text, then drop in interactive components wherever you need them.
Here's why this matters:
- π Keep the simplicity of Markdown for writing
- βοΈ Use React components for complex interactions
- π§ Build reusable content blocks
- π¨ Create custom layouts without leaving your content
Let me show you how it works in practice.
Setting Up Math Rendering with KaTeX
For technical writing, especially in fields like physics, engineering, or computer science, mathematical notation is essential. KaTeX is a fast, lightweight math typesetting library that renders LaTeX math equations beautifully on the web.
Inline Mathematics
You can write inline math by wrapping LaTeX expressions in single dollar signs. For example, when discussing energy-mass equivalence, you might write: "Einstein showed that , where is energy, is mass, and is the speed of light."
The Pythagorean theorem can be expressed as for a right triangle with sides and , and hypotenuse .
When working with complex numbers, Euler's formula relates exponentials to trigonometric functions in a beautiful way.
Display Mathematics (Block-Level)
For more complex equations that deserve their own space, use double dollar signs. This creates a centered, display-style equation:
The Quadratic Formula - Essential for solving second-degree polynomials:
Euler's Identity - Often called the most beautiful equation in mathematics, connecting five fundamental constants:
The Gaussian Integral - Fundamental in probability theory and quantum mechanics:
Maxwell's Equations - The foundation of classical electromagnetism:
SchrΓΆdinger Equation - The fundamental equation of quantum mechanics:
where is the Hamiltonian operator.
Organizing Your Content Assets
One challenge with content-heavy blogs is asset management. I recommend organizing each post in its own directory with dedicated subdirectories for different asset types:
content/posts/my-post/
βββ index.mdx # Your main content
βββ images/ # Screenshots, diagrams, figures
βββ files/ # Downloadable files (code, datasets, PDFs)
βββ assets/ # Other media (videos, audio, etc.)
This structure keeps everything organized and makes referencing assets simple:

[Download the MATLAB script](./files/simulation.m)
Combining Code and Mathematics
One of MDX's superpowers is seamlessly switching between code, math, and prose. Here's an example showing a numerical implementation alongside its mathematical foundation.
Problem: Solve the quadratic equation
Mathematical Solution:
where the discriminant determines the nature of the roots.
Implementation in JavaScript:
/**
* Solves a quadratic equation axΒ² + bx + c = 0
* @param number a - Coefficient of xΒ²
* @param number b - Coefficient of x
* @param number c - Constant term
* @returns Object containing roots and their nature
*/
function solveQuadratic(a, b, c) {
// Calculate discriminant
const discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
// Two distinct real roots
const x1 = (-b + Math.sqrt(discriminant)) / (2 * a);
const x2 = (-b - Math.sqrt(discriminant)) / (2 * a);
return `{` roots: [x1, x2], nature: 'real and distinct' `}`;
} else if (discriminant === 0) {
// One repeated real root
const x = -b / (2 * a);
return `{` roots: [x], nature: 'real and equal' `}`;
} else {
// Complex conjugate roots
const realPart = -b / (2 * a);
const imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
return `{`
roots: [
`{` re: realPart, im: imaginaryPart `}`,
`{` re: realPart, im: -imaginaryPart `}`
],
nature: 'complex conjugate'
`}`;
}
}
// Example usage
console.log(solveQuadratic(1, -3, 2)); // xΒ² - 3x + 2 = 0
// Output: `{` roots: [2, 1], nature: 'real and distinct' `}`
Notice how we can discuss the mathematical theory and immediately show the practical implementation. This is incredibly powerful for educational content.
Advanced KaTeX Features
KaTeX supports a wide range of LaTeX features. Here are some advanced techniques:
Matrices and Vectors
Rotation matrix in 2D space:
State vector in quantum mechanics:
Summations and Products
Fourier series representation:
Taylor series expansion:
Piecewise Functions
The Heaviside step function:
Creating Comparison Tables
Tables are great for comparing features, specifications, or results. MDX fully supports GitHub Flavored Markdown tables:
| Feature | Markdown | MDX | MDX + KaTeX |
|---|---|---|---|
| Text formatting | β | β | β |
| Images & links | β | β | β |
| Code blocks | β | β | β |
| Math equations | β | β | β |
| React components | β | β | β |
| Interactive elements | β | β | β |
| Custom layouts | β | β | β |
| Syntax | Purpose | Example |
|---|---|---|
$...$ | Inline math | $E = mc^2$ |
$$...$$ | Display math | $$\int_0^1 f(x)dx$$ |
<Component /> | JSX component | <CustomCard /> |
`code` | Inline code | `const x = 5` |
Best Practices I've Learned
Through building this blog system, here are some key insights:
1. Separate Content from Presentation
Keep your MDX files focused on content. Use custom components for complex UI elements rather than mixing HTML into your Markdown.
2. Use Semantic Math
When writing equations, think about readability. Use \text for text within math mode, proper spacing with \quad and \,, and meaningful variable names.
3. Optimize Asset Loading
Images can slow down your blog significantly. Use Next.js Image optimization, WebP format, and lazy loading. Store assets close to your content for better organization.
4. Test Your Math Rendering
KaTeX doesn't support 100% of LaTeX. Test complex equations and have fallbacks. Some advanced LaTeX packages won't workβknow the limitations.
5. Make Content Portable
Even though you're using MDX, keep the Markdown core clean. This makes migration easier if you ever switch platforms.
What's Next?
Now that you understand the basics, you can:
- Build custom components for your specific use case (interactive demos, data visualizations, etc.)
- Create reusable content blocks (callouts, code sandboxes, embedded demos)
- Add syntax highlighting with libraries like Prism or Highlight.js
- Implement reading time estimation and table of contents generation
- Add meta tags and SEO optimization for each post
The combination of MDX and KaTeX gives you incredible flexibility. You can write technical content that rivals traditional LaTeX documents while maintaining the interactivity and modern UX of a web application.
Conclusion
Building a content system with MDX and KaTeX might seem complex at first, but the power it gives you is worth the initial setup. You can write beautiful mathematical equations, embed interactive components, and maintain clean, readable content files all at once.
The key is understanding that you're not just writing blog postsβyou're building a system for creating rich, interactive technical content. And once you have that system in place, creating new content becomes both powerful and enjoyable.
Happy writing! π
Tech Stack: This blog uses Next.js 14, MDX, KaTeX, TypeScript, and Tailwind CSS. Each post lives in its own directory under content/posts/[slug]/index.mdx with dedicated folders for images, files, and assets.
- Build custom components for your specific use case (interactive demos, data visualizations, etc.)
- Create reusable content blocks (callouts, code sandboxes, embedded demos)
- Add syntax highlighting with libraries like Prism or Highlight.js
- Implement reading time estimation and table of contents generation
- Add meta tags and SEO optimization for each post
The combination of MDX and KaTeX gives you incredible flexibility. You can write technical content that rivals traditional LaTeX documents while maintaining the interactivity and modern UX of a web application.
Conclusion
Building a content system with MDX and KaTeX might seem complex at first, but the power it gives you is worth the initial setup. You can write beautiful mathematical equations, embed interactive components, and maintain clean, readable content files all at once.
The key is understanding that you're not just writing blog postsβyou're building a system for creating rich, interactive technical content. And once you have that system in place, creating new content becomes both powerful and enjoyable.
Happy writing! π
Tech Stack: This blog uses Next.js 14, MDX, KaTeX, TypeScript, and Tailwind CSS. Each post lives in its own directory under content/posts/[slug]/index.mdx with dedicated folders for images, files, and assets.