Math Expressions in Markdown: Complete LaTeX and MathJax Guide
Mathematical expressions are essential for technical documentation, academic papers, and scientific content. While standard Markdown doesn’t natively support math rendering, you can display beautiful mathematical formulas using LaTeX syntax combined with rendering engines like MathJax or KaTeX. This comprehensive guide covers everything you need to know about incorporating math expressions into your Markdown documents.
Why Use Math Expressions in Markdown?
Mathematical notation in Markdown offers several advantages:
- Professional Appearance: Render complex equations with proper formatting
- Universal Syntax: LaTeX is the standard for mathematical typesetting
- Version Control Friendly: Text-based formulas work well with Git
- Cross-Platform Compatibility: Works across different documentation platforms
- Accessibility: Screen readers can process mathematical content when properly structured
LaTeX Math Syntax Basics
LaTeX provides a rich syntax for mathematical expressions. Here are the fundamental concepts:
Inline vs Block Math
Inline math appears within text using single dollar signs:
The formula $E = mc^2$ represents mass-energy equivalence.
Block math is displayed on separate lines using double dollar signs:
$$
E = mc^2
$$
Common Mathematical Elements
Fractions
$$
\frac{a}{b} = \frac{numerator}{denominator}
$$
Exponents and Subscripts
$$
x^2 + y^2 = z^2
$$
$$
H_2O + CO_2
$$
Greek Letters
$$
\alpha, \beta, \gamma, \delta, \epsilon, \pi, \sigma, \omega
$$
Square Roots
$$
\sqrt{x} + \sqrt[3]{y}
$$
Platform-Specific Implementation
GitHub and GitLab
GitHub recently added native support for math expressions:
The quadratic formula is $x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}$.
$$
\sum_{i=1}^{n} x_i = x_1 + x_2 + \cdots + x_n
$$
GitHub uses MathJax for rendering, which supports most LaTeX math commands.
Jekyll and GitHub Pages
For Jekyll sites, you can enable math support by adding MathJax to your layout:
<!-- Add to _layouts/default.html or post.html -->
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js">
</script>
<script type="text/javascript">
window.MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']]
}
};
</script>
Hugo Static Site Generator
Hugo supports math rendering with KaTeX by default:
# In config.yml
markup:
goldmark:
renderer:
unsafe: true
highlight:
style: github
lineNos: true
params:
math: true
Notion
Notion supports LaTeX math using the /math
command or by typing $$
:
$$
\int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
$$
Advanced Mathematical Expressions
Matrices
$$
A = \begin{pmatrix}
a & b \\
c & d
\end{pmatrix}
$$
Systems of Equations
$$
\begin{cases}
x + y = 5 \\
2x - y = 1
\end{cases}
$$
Integrals and Derivatives
$$
\frac{d}{dx}\int_a^x f(t)dt = f(x)
$$
$$
\int_0^{\pi} \sin(x) dx = 2
$$
Summations and Products
$$
\sum_{i=1}^{n} i = \frac{n(n+1)}{2}
$$
$$
\prod_{i=1}^{n} i = n!
$$
Limits
$$
\lim_{x \to \infty} \frac{1}{x} = 0
$$
Chemistry and Physics Notation
Chemical Formulas
$$
\ce{H2SO4 + 2NaOH -> Na2SO4 + 2H2O}
$$
Physics Equations
$$
F = ma = \frac{dp}{dt}
$$
$$
\nabla \cdot \vec{E} = \frac{\rho}{\epsilon_0}
$$
Troubleshooting Common Issues
Escaping Special Characters
Problem: Underscores and other special characters breaking math rendering
Solution: Use backslashes to escape characters:
$$
\text{variable\_name} = x\_1 + x\_2
$$
Line Breaks in Equations
Problem: Long equations extending beyond page width
Solution: Use alignment environments:
$$
\begin{align}
f(x) &= ax^2 + bx + c \\
&= a(x^2 + \frac{b}{a}x) + c \\
&= a(x + \frac{b}{2a})^2 - \frac{b^2}{4a} + c
\end{align}
$$
Platform Compatibility
Problem: Math renders differently across platforms
Solution: Test your expressions on target platforms and use widely supported LaTeX commands:
<!-- Use standard commands for better compatibility -->
\sum, \int, \frac, \sqrt, \sin, \cos, \log
Best Practices
Readability Guidelines
- Use Descriptive Text: Explain complex equations in surrounding text
- Consistent Notation: Maintain consistent variable naming throughout documents
- Proper Spacing: Use
\,
for thin spaces and\quad
for wider spaces - Clear Structure: Break complex expressions into multiple steps
Performance Considerations
<!-- Preload MathJax for better performance -->
<link rel="preload" href="https://polyfill.io/v3/polyfill.min.js?features=es6" as="script">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" as="script">
Accessibility
<!-- Use \text{} for descriptive text within formulas -->
$$
P(\text{success}) = \frac{\text{favorable outcomes}}{\text{total outcomes}}
$$
Rendering Engines Comparison
MathJax
Pros:
- Excellent LaTeX compatibility
- High-quality rendering
- Wide platform support
Cons:
- Larger file size
- Slower initial load
KaTeX
Pros:
- Faster rendering
- Smaller bundle size
- Server-side rendering support
Cons:
- Limited LaTeX command support
- Fewer advanced features
Implementation Example
<!-- MathJax -->
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<!-- KaTeX -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css">
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js"></script>
Real-World Examples
Statistical Formulas
$$
\sigma = \sqrt{\frac{\sum_{i=1}^{n}(x_i - \mu)^2}{n}}
$$
Machine Learning
$$
J(\theta) = \frac{1}{2m}\sum_{i=1}^{m}(h_\theta(x^{(i)}) - y^{(i)})^2
$$
Calculus
$$
\frac{d}{dx}[f(g(x))] = f'(g(x)) \cdot g'(x)
$$
Testing and Validation
Always test your mathematical expressions across different platforms:
- Local Development: Preview in your local markdown renderer
- Target Platform: Test on GitHub, GitLab, or your documentation site
- Mobile Devices: Ensure formulas display correctly on smaller screens
- Browser Compatibility: Check rendering across different browsers
Integration with Other Markdown Features
Math expressions work well with other Markdown elements. You can create comprehensive technical documentation by combining mathematical formulas with tables in Markdown for data presentation, and use collapsible sections to organize complex mathematical proofs or derivations.
Conclusion
Mathematical expressions in Markdown bridge the gap between simple text formatting and professional scientific documentation. By leveraging LaTeX syntax with rendering engines like MathJax or KaTeX, you can create beautiful, accessible mathematical content that integrates seamlessly with your documentation workflow.
Whether you’re writing academic papers, technical documentation, or educational materials, the combination of Markdown’s simplicity with LaTeX’s mathematical power provides an excellent foundation for communicating complex ideas clearly and effectively.
Remember to choose the right rendering engine for your needs, test across platforms, and follow accessibility best practices to ensure your mathematical content reaches the widest possible audience.