KaTeX Math Examples and Tips
KaTeX renders mathematical notation beautifully in web pages. This guide covers common patterns, syntax, and tips for writing mathematical expressions.
Use Case
Use KaTeX when you need to:
- Display mathematical formulas and equations
- Show scientific notation
- Write technical documentation with math
- Create educational content with equations
- Document algorithms with mathematical foundations
Basic Syntax
Inline Math
Use single dollar signs for inline math: $x = y + z$ renders as $x = y + z$
Display Math
Use double dollar signs for display math (centered, on its own line):
1$$
2E = mc^2
3$$
Renders as:
$$ E = mc^2 $$
Common Mathematical Expressions
Fractions
1$$
2\frac{a}{b} \quad \frac{numerator}{denominator} \quad \frac{x^2 + 1}{x - 1}
3$$
$$ \frac{a}{b} \quad \frac{numerator}{denominator} \quad \frac{x^2 + 1}{x - 1} $$
Superscripts and Subscripts
1$$
2x^2 \quad x^{n+1} \quad x_i \quad x_{i+1} \quad x^{y^z}
3$$
$$ x^2 \quad x^{n+1} \quad x_i \quad x_{i+1} \quad x^{y^z} $$
Roots
1$$
2\sqrt{x} \quad \sqrt[n]{x} \quad \sqrt{x^2 + y^2}
3$$
$$ \sqrt{x} \quad \sqrt[n]{x} \quad \sqrt{x^2 + y^2} $$
Sums and Products
1$$
2\sum_{i=1}^{n} x_i \quad \prod_{i=1}^{n} x_i \quad \int_{a}^{b} f(x) dx
3$$
$$ \sum_{i=1}^{n} x_i \quad \prod_{i=1}^{n} x_i \quad \int_{a}^{b} f(x) dx $$
Greek Letters
1$$
2\alpha \quad \beta \quad \gamma \quad \delta \quad \epsilon \quad \theta \quad \lambda \quad \mu \quad \pi \quad \sigma \quad \phi \quad \omega
3$$
$$ \alpha \quad \beta \quad \gamma \quad \delta \quad \epsilon \quad \theta \quad \lambda \quad \mu \quad \pi \quad \sigma \quad \phi \quad \omega $$
Capital Greek letters:
1$$
2\Alpha \quad \Beta \quad \Gamma \quad \Delta \quad \Theta \quad \Lambda \quad \Pi \quad \Sigma \quad \Phi \quad \Omega
3$$
$$ \Alpha \quad \Beta \quad \Gamma \quad \Delta \quad \Theta \quad \Lambda \quad \Pi \quad \Sigma \quad \Phi \quad \Omega $$
Operators and Relations
Comparison Operators
1$$
2< \quad > \quad \leq \quad \geq \quad \neq \quad \approx \quad \equiv
3$$
$$ < \quad > \quad \leq \quad \geq \quad \neq \quad \approx \quad \equiv $$
Set Operations
1$$
2\in \quad \notin \quad \subset \quad \subseteq \quad \cup \quad \cap \quad \setminus
3$$
$$ \in \quad \notin \quad \subset \quad \subseteq \quad \cup \quad \cap \quad \setminus $$
Logical Operators
1$$
2\land \quad \lor \quad \neg \quad \implies \quad \iff \quad \forall \quad \exists
3$$
$$ \land \quad \lor \quad \neg \quad \implies \quad \iff \quad \forall \quad \exists $$
Matrices and Vectors
Matrices
1$$
2\begin{pmatrix}
3a & b \\\\
4c & d
5\end{pmatrix}
6\quad
7\begin{bmatrix}
81 & 2 & 3 \\\\
94 & 5 & 6 \\\\
107 & 8 & 9
11\end{bmatrix}
12$$
$$ \begin{pmatrix} a & b \\ c & d \end{pmatrix} \quad \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} $$
More Matrix Examples
Determinant
1$$
2\det(\mathbf{A}) = \begin{vmatrix}
3a & b \\\\
4c & d
5\end{vmatrix} = ad - bc
6$$
$$ \det(\mathbf{A}) = \begin{vmatrix} a & b \\ c & d \end{vmatrix} = ad - bc $$
Matrix Types
1$$
2\begin{pmatrix} a & b \\\\ c & d \end{pmatrix} \quad
3\begin{bmatrix} a & b \\\\ c & d \end{bmatrix} \quad
4\begin{Bmatrix} a & b \\\\ c & d \end{Bmatrix} \quad
5\begin{vmatrix} a & b \\\\ c & d \end{vmatrix} \quad
6\begin{Vmatrix} a & b \\\\ c & d \end{Vmatrix}
7$$
$$ \begin{pmatrix} a & b \\ c & d \end{pmatrix} \quad \begin{bmatrix} a & b \\ c & d \end{bmatrix} \quad \begin{Bmatrix} a & b \\ c & d \end{Bmatrix} \quad \begin{vmatrix} a & b \\ c & d \end{vmatrix} \quad \begin{Vmatrix} a & b \\ c & d \end{Vmatrix} $$
Large Matrices
1$$
2\mathbf{A} = \begin{bmatrix}
3a_{11} & a_{12} & \cdots & a_{1n} \\\\
4a_{21} & a_{22} & \cdots & a_{2n} \\\\
5\vdots & \vdots & \ddots & \vdots \\\\
6a_{m1} & a_{m2} & \cdots & a_{mn}
7\end{bmatrix}
8$$
$$ \mathbf{A} = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix} $$
Block Matrices
1$$
2\begin{bmatrix}
3\mathbf{A} & \mathbf{B} \\\\
4\mathbf{C} & \mathbf{D}
5\end{bmatrix} = \begin{bmatrix}
6a_{11} & a_{12} & b_{11} & b_{12} \\\\
7a_{21} & a_{22} & b_{21} & b_{22} \\\\
8c_{11} & c_{12} & d_{11} & d_{12} \\\\
9c_{21} & c_{22} & d_{21} & d_{22}
10\end{bmatrix}
11$$
$$ \begin{bmatrix} \mathbf{A} & \mathbf{B} \\ \mathbf{C} & \mathbf{D} \end{bmatrix} = \begin{bmatrix} a_{11} & a_{12} & b_{11} & b_{12} \\ a_{21} & a_{22} & b_{21} & b_{22} \\ c_{11} & c_{12} & d_{11} & d_{12} \\ c_{21} & c_{22} & d_{21} & d_{22} \end{bmatrix} $$
Vectors
1$$
2\vec{v} = \begin{pmatrix} x \\\\ y \\\\ z \end{pmatrix} \quad \mathbf{v} = \begin{bmatrix} v_1 \\\\ v_2 \\\\ v_3 \end{bmatrix}
3$$
$$ \vec{v} = \begin{pmatrix} x \\ y \\ z \end{pmatrix} \quad \mathbf{v} = \begin{bmatrix} v_1 \\ v_2 \\ v_3 \end{bmatrix} $$
Functions and Special Notation
Common Functions
1$$
2\sin(x) \quad \cos(x) \quad \tan(x) \quad \log(x) \quad \ln(x) \quad \exp(x)
3$$
$$ \sin(x) \quad \cos(x) \quad \tan(x) \quad \log(x) \quad \ln(x) \quad \exp(x) $$
Limits
1$$
2\lim_{x \to \infty} f(x) \quad \lim_{n \to 0} \frac{\sin(n)}{n} = 1
3$$
$$ \lim_{x \to \infty} f(x) \quad \lim_{n \to 0} \frac{\sin(n)}{n} = 1 $$
Derivatives
1$$
2\frac{d}{dx}f(x) \quad f'(x) \quad \frac{\partial f}{\partial x} \quad \nabla f
3$$
$$ \frac{d}{dx}f(x) \quad f'(x) \quad \frac{\partial f}{\partial x} \quad \nabla f $$
Aligned Equations
Single Alignment
1$$
2\begin{aligned}
3x &= a + b \\\\
4y &= c + d \\\\
5z &= x + y
6\end{aligned}
7$$
$$ \begin{aligned} x &= a + b \\ y &= c + d \\ z &= x + y \end{aligned} $$
Multi-line Equations
1$$
2\begin{align}
3f(x) &= x^2 + 2x + 1 \\\\
4 &= (x + 1)^2 \\\\
5 &= x^2 + 2x + 1
6\end{align}
7$$
$$ \begin{align} f(x) &= x^2 + 2x + 1 \\ &= (x + 1)^2 \\ &= x^2 + 2x + 1 \end{align} $$
Cases and Piecewise Functions
Basic Cases
1$$
2f(x) = \begin{cases}
3x^2 & \text{if } x \geq 0 \\\\
4-x^2 & \text{if } x < 0
5\end{cases}
6$$
$$ f(x) = \begin{cases} x^2 & \text{if } x \geq 0 \\ -x^2 & \text{if } x < 0 \end{cases} $$
Conditional Expressions
1$$
2a = \begin{cases}
32 & \text{if } b = 2 \\\\
44 & \text{if } b = 4 \\\\
56 & \text{if } b = 6 \\\\
60 & \text{otherwise}
7\end{cases}
8$$
$$ a = \begin{cases} 2 & \text{if } b = 2 \\ 4 & \text{if } b = 4 \\ 6 & \text{if } b = 6 \\ 0 & \text{otherwise} \end{cases} $$
Multiple Conditions
1$$
2f(x) = \begin{cases}
3x + 1 & \text{if } x < 0 \\\\
4x^2 & \text{if } 0 \leq x < 1 \\\\
52x - 1 & \text{if } x \geq 1
6\end{cases}
7$$
$$ f(x) = \begin{cases} x + 1 & \text{if } x < 0 \\ x^2 & \text{if } 0 \leq x < 1 \\ 2x - 1 & \text{if } x \geq 1 \end{cases} $$
Conditional Matrix
1$$
2\mathbf{A} = \begin{cases}
3\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \end{bmatrix} & \text{if } \det(\mathbf{B}) \neq 0 \\\\
4\begin{bmatrix} 0 & 0 \\\\ 0 & 0 \end{bmatrix} & \text{otherwise}
5\end{cases}
6$$
$$ \mathbf{A} = \begin{cases} \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} & \text{if } \det(\mathbf{B}) \neq 0 \\ \begin{bmatrix} 0 & 0 \\ 0 & 0 \end{bmatrix} & \text{otherwise} \end{cases} $$
Conditional with Logical Operators
1$$
2y = \begin{cases}
3x^2 & \text{if } x > 0 \text{ and } x \neq 1 \\\\
4\log(x) & \text{if } x > 0 \text{ and } x = 1 \\\\
50 & \text{if } x \leq 0
6\end{cases}
7$$
$$ y = \begin{cases} x^2 & \text{if } x > 0 \text{ and } x \neq 1 \\ \log(x) & \text{if } x > 0 \text{ and } x = 1 \\ 0 & \text{if } x \leq 0 \end{cases} $$
Spacing and Formatting
Manual Spacing
1$$
2a\,b \quad a\;b \quad a\:b \quad a\!b \quad a\ b \quad a\quad b \quad a\qquad b
3$$
$$ a,b \quad a;b \quad a:b \quad a!b \quad a\ b \quad a\quad b \quad a\qquad b $$
Text in Math
1$$
2\text{for all } x \in \mathbb{R} \quad \text{where } n > 0
3$$
$$ \text{for all } x \in \mathbb{R} \quad \text{where } n > 0 $$
Number Sets
1$$
2\mathbb{N} \quad \mathbb{Z} \quad \mathbb{Q} \quad \mathbb{R} \quad \mathbb{C}
3$$
$$ \mathbb{N} \quad \mathbb{Z} \quad \mathbb{Q} \quad \mathbb{R} \quad \mathbb{C} $$
Practical Examples
Example 1: Quadratic Formula
1$$
2x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
3$$
$$ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $$
Example 2: Euler's Identity
1$$
2e^{i\pi} + 1 = 0
3$$
$$ e^{i\pi} + 1 = 0 $$
Example 3: Bayes' Theorem
1$$
2P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)}
3$$
$$ P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)} $$
Example 4: Matrix Multiplication
1$$
2\mathbf{C} = \mathbf{A} \mathbf{B} = \begin{bmatrix}
3a_{11} & a_{12} \\\\
4a_{21} & a_{22}
5\end{bmatrix}
6\begin{bmatrix}
7b_{11} & b_{12} \\\\
8b_{21} & b_{22}
9\end{bmatrix}
10$$
$$ \mathbf{C} = \mathbf{A} \mathbf{B} = \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix} \begin{bmatrix} b_{11} & b_{12} \\ b_{21} & b_{22} \end{bmatrix} $$
Example 5: Gradient Descent Update
1$$
2\theta_{t+1} = \theta_t - \alpha \nabla_\theta J(\theta_t)
3$$
$$ \theta_{t+1} = \theta_t - \alpha \nabla_\theta J(\theta_t) $$
Example 6: Neural Network Forward Pass
1$$
2\mathbf{h} = \sigma(\mathbf{W}\mathbf{x} + \mathbf{b})
3$$
4$$
5
6where $\sigma$ is the activation function, $\mathbf{W}$ is the weight matrix, $\mathbf{x}$ is the input vector, and $\mathbf{b}$ is the bias vector.
7
8### Example 7: Loss Function
9
10```markdown
11$$
12\mathcal{L} = -\frac{1}{N}\sum_{i=1}^{N} \left[ y_i \log(\hat{y}_i) + (1-y_i)\log(1-\hat{y}_i) \right]
13$$
$$ \mathcal{L} = -\frac{1}{N}\sum_{i=1}^{N} \left[ y_i \log(\hat{y}_i) + (1-y_i)\log(1-\hat{y}_i) \right] $$
Tips and Best Practices
1. Use Display Math for Important Equations
Display math (double $$) is better for:
- Standalone equations
- Multi-line expressions
- Complex formulas
Inline math (single $) is better for:
- Short expressions in text
- Variables mentioned in sentences
- Simple formulas
2. Escape Special Characters
In markdown, you may need to escape:
$→\$(if not used for math)_→\_(in non-math contexts)*→\*(in non-math contexts)
3. Use Aligned Environments for Multi-line
1$$
2\begin{aligned}
3f(x) &= x^2 + 2x + 1 \\\\
4 &= (x+1)^2
5\end{aligned}
6$$
Better than:
1$$
2f(x) = x^2 + 2x + 1 = (x+1)^2
3$$
4. Label Important Equations
While KaTeX doesn't support \label like LaTeX, you can add text labels:
1$$
2\text{(1)} \quad E = mc^2
3$$
5. Use Text Mode for Words
Always use \text{} for words in math mode:
1$$
2\text{for } x \in \mathbb{R} \text{ and } y > 0
3$$
Not:
1$$
2for x \in \mathbb{R} and y > 0 \quad \text{(incorrect)}
3$$
Common Mistakes to Avoid
Mistake 1: Mixing Math and Text
❌ Wrong:
1The value is $x$ where $x > 0$ and positive.
✅ Correct:
1The value is $x$ where $x > 0$ and is positive.
Mistake 2: Forgetting Curly Braces
❌ Wrong:
1$x^10$ (renders as $x^10$)
✅ Correct:
1$x^{10}$ (renders as $x^{10}$)
Mistake 3: Incorrect Spacing
❌ Wrong:
1$f(x)=x^2+1$ (no spacing)
✅ Correct:
1$f(x) = x^2 + 1$ (proper spacing)
Advanced Examples
Example: Fourier Transform
1$$
2F(\omega) = \int_{-\infty}^{\infty} f(t) e^{-i\omega t} dt
3$$
$$ F(\omega) = \int_{-\infty}^{\infty} f(t) e^{-i\omega t} dt $$
Example: Schrödinger Equation
1$$
2i\hbar\frac{\partial}{\partial t}\Psi(\mathbf{r},t) = \hat{H}\Psi(\mathbf{r},t)
3$$
$$ i\hbar\frac{\partial}{\partial t}\Psi(\mathbf{r},t) = \hat{H}\Psi(\mathbf{r},t) $$
Example: Einstein Field Equations
1$$
2R_{\mu\nu} - \frac{1}{2}Rg_{\mu\nu} + \Lambda g_{\mu\nu} = \frac{8\pi G}{c^4}T_{\mu\nu}
3$$
$$ R_{\mu\nu} - \frac{1}{2}Rg_{\mu\nu} + \Lambda g_{\mu\nu} = \frac{8\pi G}{c^4}T_{\mu\nu} $$
Resources
- KaTeX Documentation
- LaTeX Math Symbols
- Detexify - Draw symbol to find LaTeX command
Gotchas/Warnings
- ⚠️ Dollar Signs: In markdown,
$starts math mode - escape with\$if needed - ⚠️ Curly Braces: Always use
{}for multi-character subscripts/superscripts - ⚠️ Text Mode: Use
\text{}for words, not raw text in math mode - ⚠️ Spacing: Math mode removes spaces - use
\,,\;,\quad, etc. for spacing - ⚠️ Alignment: Use
alignedoralignenvironments for multi-line equations - ⚠️ Backslashes: In markdown, you may need
\\for line breaks in matrices
Related Snippets
- Architecture Diagrams (C4 Model)
Create system architecture diagrams using C4 model with Mermaid - Chart.js Bar Chart
Create bar charts for data visualization - Graphviz DOT Diagrams
Create complex graph layouts with Graphviz DOT language - Mermaid Arbitrary Graphs
Create arbitrary graphs and networks with Mermaid - Mermaid Block Diagrams
Create block diagrams for system architecture with Mermaid - Mermaid Charts (Pie, Bar, Line)
Create pie charts, bar charts, and line charts with Mermaid - Mermaid Class Diagrams (UML)
Create UML class diagrams with Mermaid - Mermaid Entity Relationship Diagrams
Create Entity Relationship Diagrams (ERD) with Mermaid - Mermaid Flowchart
Create flowcharts and decision trees with Mermaid - Mermaid Gantt Chart
Create Gantt charts for project timelines and scheduling - Mermaid Git Diagrams
Create Git branch and commit diagrams with Mermaid - Mermaid Kanban Boards
Create Kanban boards for project management with Mermaid - Mermaid Mindmaps
Create mindmaps for brainstorming and organizing ideas with Mermaid - Mermaid Packet Diagrams
Create packet diagrams for network protocols with Mermaid - Mermaid Quadrant Charts
Create quadrant charts for prioritization with Mermaid - Mermaid Radar Charts
Create radar charts for multi-dimensional comparisons with Mermaid - Mermaid Requirement Diagrams
Create requirement diagrams for system requirements with Mermaid - Mermaid Sankey Diagrams
Create Sankey diagrams for flow visualization with Mermaid - Mermaid Sequence Diagram
Create sequence diagrams for interactions and API flows - Mermaid State Diagrams
Create state diagrams and state machines with Mermaid - Mermaid Timeline Diagrams
Create timeline diagrams for chronological events with Mermaid - Mermaid Treemap Diagrams
Create treemap diagrams for hierarchical data visualization with Mermaid - Mermaid User Journey Diagrams
Create user journey maps with Mermaid - Mermaid ZenUML Diagrams
Create ZenUML sequence diagrams with Mermaid - P5.js Interactive Visualizations
Create interactive visualizations and animations with P5.js