LaTeX/MathJax Demo Notebook¶
This Jupyter notebook demonstrates mathematical rendering with our enhanced MathJax configuration.
Basic Mathematical Expressions¶
Inline math examples:
- Quadratic formula: $x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}$
- Circle area: $A = \pi r^2$
- Einstein equation: $E = mc^2$
Display Equations¶
$$\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}$$
$$\sum_{n=1}^{\infty} \frac{1}{n^2} = \frac{\pi^2}{6}$$
import matplotlib.pyplot as plt
import numpy as np
from IPython.display import Latex, Math, display
# Test Python mathematical computation
x = np.linspace(-5, 5, 100)
y = x**2
plt.figure(figsize=(8, 6))
plt.plot(x, y, "b-", linewidth=2, label=r"$f(x) = x^2$")
plt.xlabel(r"$x$")
plt.ylabel(r"$f(x)$")
plt.title(r"Graph of $f(x) = x^2$")
plt.grid(True, alpha=0.3)
plt.legend()
plt.show()
print(f"The derivative of f(x) = x² is f'(x) = 2x")
Vector and Matrix Notation¶
Vector: $\vec{v} = \begin{pmatrix} x \\ y \\ z \end{pmatrix}$
Matrix: $\mat{A} = \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix}$
# Display LaTeX using IPython's display capabilities
display(
Latex(
r"\begin{align}"
r"\vec{v} \cdot \vec{w} &= v_x w_x + v_y w_y + v_z w_z \\"
r"&= |\vec{v}| |\vec{w}| \cos \theta"
r"\end{align}"
)
)
print("Vector dot product formula displayed above")
# Demonstrate gradient descent
def f(x):
return x**2 + 2 * x + 1
def df_dx(x):
return 2 * x + 2
# Gradient descent parameters
x = 5.0 # starting point
alpha = 0.1 # learning rate
iterations = 20
x_history = [x]
for i in range(iterations):
grad = df_dx(x)
x = x - alpha * grad
x_history.append(x)
print(f"Starting point: {x_history[0]:.3f}")
print(f"Final point: {x_history[-1]:.3f}")
print(f"Minimum of f(x) = x² + 2x + 1 is at x = -1")
# Plot normal distribution
from scipy import stats
x = np.linspace(-4, 4, 100)
y1 = stats.norm.pdf(x, 0, 1) # μ=0, σ=1
y2 = stats.norm.pdf(x, 0, 0.5) # μ=0, σ=0.5
y3 = stats.norm.pdf(x, 1, 1) # μ=1, σ=1
plt.figure(figsize=(10, 6))
plt.plot(x, y1, "b-", label=r"$\mu=0, \sigma=1$")
plt.plot(x, y2, "r-", label=r"$\mu=0, \sigma=0.5$")
plt.plot(x, y3, "g-", label=r"$\mu=1, \sigma=1$")
plt.xlabel(r"$x$")
plt.ylabel(r"$f(x)$")
plt.title(
r"Normal Distribution: $f(x|\mu,\sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}$"
)
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Advanced Mathematics¶
Fourier Transform¶
$$\mathcal{F}\{f(t)\} = F(\omega) = \int_{-\infty}^{\infty} f(t) e^{-i\omega t} dt$$
Maxwell's Equations¶
\begin{align} \nabla \times \vec{E} &= -\frac{\partial \vec{B}}{\partial t} \\ \nabla \times \vec{B} &= \mu_0 \vec{J} + \mu_0 \epsilon_0 \frac{\partial \vec{E}}{\partial t} \\ \nabla \cdot \vec{E} &= \frac{\rho}{\epsilon_0} \\ \nabla \cdot \vec{B} &= 0 \end{align}
Chemical Equations¶
Using the mhchem extension:
$$\ce{H2O + NaCl -> Na+ + Cl- + H2O}$$
$$\ce{2H2 + O2 -> 2H2O}$$
$$\ce{CaCO3 + 2HCl -> CaCl2 + H2O + CO2}$$
Testing Output Formats¶
This notebook tests various MathJax features:
- ✅ Inline math with
$
delimiters - ✅ Display math with
$$
delimiters - ✅ LaTeX environments (align, equation)
- ✅ Custom macros for sets, vectors, operators
- ✅ Chemical equations with mhchem
- ✅ Complex mathematical expressions
- ✅ Integration with Python code and matplotlib
When this notebook is rendered in MkDocs, all mathematical expressions should display correctly with proper formatting and typography.
# Final test: Display some mathematical constants
import math
print("Mathematical Constants:")
print(f"π (pi) = {math.pi:.10f}")
print(f"e (Euler's number) = {math.e:.10f}")
print(f"φ (Golden ratio) = {(1 + math.sqrt(5))/2:.10f}")
# Display as LaTeX
display(
Latex(
r"""\begin{align}
\pi &\approx 3.14159 \\
e &\approx 2.71828 \\
\phi &= \frac{1 + \sqrt{5}}{2} \approx 1.61803
\end{align}"""
)
)