Andre Pena

Code Blocks and Math

December 23, 2024
1 min read

This post demonstrates code blocks with syntax highlighting and mathematical equations.

Code Blocks

JavaScript

hello.js
function greet(name) {
console.log(`Hello, ${name}!`)
}
greet('World')

TypeScript with Line Numbers

utils.ts
interface User {
id: number
name: string
email: string
}
function getUser(id: number): User | undefined {
const users: User[] = [
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' },
]
return users.find(user => user.id === id)
}

Line Highlighting

example.py
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(10))

Terminal Commands

Terminal window
npm install astro
npm run dev

Diff Syntax

const old = 'removed'
const new = 'added'

JSON Configuration

package.json
{
"name": "my-blog",
"scripts": {
"dev": "astro dev",
"build": "astro build"
}
}

Mathematics

Inline Math

The famous equation E=mc2E = mc^2 shows the relationship between energy and mass.

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

Block Equations

The Gaussian integral:

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

Euler’s identity:

eiπ+1=0e^{i\pi} + 1 = 0

A matrix example:

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

Sum and Product Notation

i=1ni=n(n+1)2\sum_{i=1}^{n} i = \frac{n(n+1)}{2} i=1ni=n!\prod_{i=1}^{n} i = n!