Using LaTeX Math in Astro Markdown
latex

Using LaTeX Math in Astro Markdown

Jan 20, 2024
· STRUKTURIERTES LERNEN ·

Using LaTeX Math in Astro Markdown - iPad edit

Astro with the remark-math and rehype-katex plugins allows you to write beautiful mathematical equations directly in your markdown files. Let’s explore how to use inline and block math.

Inline Math

You can write inline mathematical expressions using single dollar signs. For example, the quadratic formula is x=b±b24ac2ax = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}.

Here are some more examples:

  • The area of a circle is A=πr2A = \pi r^2
  • The Pythagorean theorem states a2+b2=c2a^2 + b^2 = c^2
  • Euler’s identity: eiπ+1=0e^{i\pi} + 1 = 0

Block Math

For larger equations that should be displayed on their own line, use double dollar signs:

ex2dx=π\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}

Here’s the famous Maxwell’s equations:

E=ρϵ0B=0×E=Bt×B=μ0J+μ0ϵ0Et\begin{align} \nabla \cdot \mathbf{E} &= \frac{\rho}{\epsilon_0} \\ \nabla \cdot \mathbf{B} &= 0 \\ \nabla \times \mathbf{E} &= -\frac{\partial \mathbf{B}}{\partial t} \\ \nabla \times \mathbf{B} &= \mu_0\mathbf{J} + \mu_0\epsilon_0\frac{\partial \mathbf{E}}{\partial t} \end{align}

Common Mathematical Symbols

Greek Letters

  • α,β,γ,δ,ϵ,θ,λ,μ,π,σ,ϕ,ω\alpha, \beta, \gamma, \delta, \epsilon, \theta, \lambda, \mu, \pi, \sigma, \phi, \omega
  • A,B,Γ,Δ,Θ,Λ,Π,Σ,Φ,Ω\Alpha, \Beta, \Gamma, \Delta, \Theta, \Lambda, \Pi, \Sigma, \Phi, \Omega

Operators

  • i=1nxi\sum_{i=1}^{n} x_i (summation)
  • i=1nxi\prod_{i=1}^{n} x_i (product)
  • abf(x)dx\int_{a}^{b} f(x) dx (integral)
  • ddxf(x)\frac{d}{dx} f(x) (derivative)

Matrices

[abcd](xy)=(ax+bycx+dy)\begin{bmatrix} a & b \\ c & d \end{bmatrix} \begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} ax + by \\ cx + dy \end{pmatrix}

Code Example

Here’s how you would write the quadratic formula in your markdown:

The quadratic formula is $x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$.

For block math:
$$
\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}
$$

Tips for Writing Math

  1. Use proper spacing: LaTeX handles spacing automatically, but you can add \, for small spaces or \quad for larger spaces
  2. Group expressions: Use {} to group parts of your expression: e^{2x} vs e^2x
  3. Common functions: Use \sin, \cos, \log, \ln, \exp for proper formatting
  4. Fractions: Use \frac{numerator}{denominator} for fractions

Example: Complex Number Operations

Let’s work with complex numbers. If z=a+biz = a + bi and w=c+diw = c + di, then:

z+w=(a+c)+(b+d)izw=(acbd)+(ad+bc)iz=a2+b2\begin{align} z + w &= (a + c) + (b + d)i \\ z \cdot w &= (ac - bd) + (ad + bc)i \\ |z| &= \sqrt{a^2 + b^2} \end{align}

The complex conjugate is z=abi\overline{z} = a - bi, and we have zz=z2z \cdot \overline{z} = |z|^2.

Conclusion

With LaTeX support in your Astro blog, you can now write beautiful mathematical content that renders perfectly on the web. Whether you’re writing about physics, mathematics, engineering, or any field that uses mathematical notation, this setup will serve you well!

Remember to use single $ for inline math and double $$ for block math equations.

ex2dx=π\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}

ex2dx=π\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}