Skip to content
LaTeX

From Python Code to Mathematical Notation: A Practical Guide to Understanding and Writing LaTeX Formulas

Rajiv KumarSeptember 16, 20266 min read
From Python Code to Mathematical Notation: A Practical Guide to Understanding and Writing LaTeX Formulas

Mathematics and programming often describe the same idea in two different languages.A mathematics textbook may write: A Python developer may write:y_hat = w1 * x1 + w2 * x2 + bBoth expressions…

Mathematics and programming often describe the same idea in two different languages.

A mathematics textbook may write:

A Python developer may write:

y_hat = w1 * x1 + w2 * x2 + b

Both expressions represent the same basic calculation. The difference is that the mathematical version is designed for human reading, while the Python version is designed for execution by a computer.

Understanding this difference is important for anyone learning:

  • Python

  • Machine Learning

  • Artificial Intelligence

  • Statistics

  • Data Science

  • Deep Learning

  • Mathematics for Computer Science

  • Technical documentation

  • Research papers

  • Jupyter Notebooks

  • AI and software engineering

LaTeX provides a bridge between these two worlds. It allows us to write mathematical notation in plain text and convert it into the professionally typeset formulas commonly seen in mathematics books, research papers, technical documentation, and educational content.

This article explains how to read mathematical LaTeX, how the same formula is written in Python, what output Python produces, and how the LaTeX source becomes a rendered mathematical expression.


1. Mathematics, Python, and LaTeX Describe the Same Idea Differently

Consider this mathematical expression:

It means:

  1. Multiply by .

  2. Add 3.

  3. Store the result in .

The same calculation in Python is:

x = 5
y = 2 * x + 3

print(y)

Python Output:

13

The Python interpreter executes the instructions and calculates the result.

However, if we want to explain the formula in a technical article, a mathematical textbook, or a research paper, we may prefer the following LaTeX source:

Latex

y = 2x + 3

Rendered LaTeX result

The three versions have different purposes:

Format

Purpose

Python code

Executes the calculation

Python output

Shows the calculated result

LaTeX source

Describes how the mathematical expression should be typeset

Rendered formula

Displays the mathematical expression visually

2. Why Should a Python Developer Learn Mathematical LaTeX?

Learning LaTeX is not mandatory for writing Python programs. You can become a Python developer without knowing LaTeX.

However, LaTeX becomes very useful when you work with mathematical or technical content.

1.1 Machine Learning formulas are usually written in mathematical notation

Machine Learning contains many formulas involving:

  • Greek letters

  • Subscripts

  • Superscripts

  • Fractions

  • Summations

  • Integrals

  • Matrices

  • Probability notation

  • Derivatives

  • Vectors

  • Optimization functions

For example, a linear regression model may be written as:

Writing the same expression in Python may look like this:

y_hat = w1 * x1 + w2 * x2 + ... + wn * xn + b

n real Python code, the ellipsis is normally not used for an actual calculation. We would usually use a loop, a list, or a numerical library:

y_hat = sum(w * x for w, x in zip(weights, features)) + b

1.2 LaTeX helps you understand formulas instead of memorizing symbols

When you see:

\hat{y} = h_{\theta}(x)

you can understand the structure:

  • \hat{y} creates a hat over y

  • h_{\theta} creates a subscript containing the Greek letter theta

  • (x) indicates that the function receives x

The rendered result is:

\hat{y} →

\theta →

\mid →

\text{Spam} →

\cdot →

\frac{a}{b} →

2. Superscript aur Subscript

LaTeX

Result

Meaning

x^2

Power

x^n

Power

x_i

Subscript

x_{ij}

Multiple-character subscript

x^{2n}

Multiple-character superscript

x_i^2

Both

x_{i,j}

Complex subscript

3. Accent aur Special Marks

LaTeX

Result

Use

\hat{y}

Estimated value

\bar{x}

Mean/average

\vec{v}

Vector

\dot{x}

First derivative

\ddot{x}

Second derivative

\tilde{x}

Approximation

\overline{AB}

Line/average

\underline{x}

Underline

\widehat{ABC}

Wide hat

\widetilde{ABC}

Wide tilde

4. Greek Letters

LaTeX

Result

LaTeX

Result

\alpha

\beta

\gamma

\delta

\epsilon

\varepsilon

\zeta

\eta

\theta

\vartheta

\iota

\kappa

\lambda

\mu

\nu

\xi

\pi

\varpi

\rho

\varrho

\sigma

\varsigma

\tau

\upsilon

\phi

\varphi

\chi

\psi

\omega

\Gamma

4.1 Uppercase Greek letters

LaTeX

Result

LaTeX

Result

\Gamma

\Delta

\Theta

\Lambda

\Xi

\Pi

\Sigma

\Upsilon

\Phi

\Psi

\Omega

5. Fractions

In the fraction uses numerator and denominator "\frac{numerator}{denominator}" →

\frac{a}{b} →

\frac{x+1}{x-1} →

\frac{P(W \mid \text{Spam}) \cdot P(\text{Spam})}{P(W)} →

5.1 Nested Fraction

\frac{1}{1+\frac{a}{b}} →

6. Multiplication

LaTeX

Result

Meaning

ab

Normal multiplication

a b

Spacing ignored

a \cdot b

Center dot

a \times b

Cross multiplication

a \ast b

Asterisk

a \star b

Star operator

7. Relations aur Comparison Symbols

LaTeX

Result

=

\neq

\ne

< 

 

> 

 

\leq

\geq

\approx

\sim

\simeq

\equiv

\propto

\ll

\gg

Examples:

x \geq 0 →

P(A) \approx P(B) →

y \propto x →

8. Probability Symbols

LaTeX

Result

Meaning

P(A)

Probability

P(A \mid B)

Conditional probability

P(A \cap B)

Intersection

P(A \cup B)

Union

P(A^c)

Complement

P(\neg A)

Not A

\mid

Given/conditional

\Pr(A)

Probability notation

  • Dynamic-size brackets

\left(\frac{a}{b}\right) →

9. Set Theory Symbols

LaTeX

Result

Meaning

\in

Belongs to

\notin

Does not belong

\subset

Subset

\subseteq

Subset or equal

\supset

Superset

\emptyset

Empty set

\varnothing

Empty set

\mathbb{R}

Real numbers

\mathbb{N}

Natural numbers

\mathbb{Z}

Integers

\mathbb{Q}

Rational numbers

\mathbb{C}

Complex numbers

\cup

Union

\cap

Intersection

Example

x \in \mathbb{R} →

A \subseteq B →

11. Summation, Product and Calculus

Summation

\sum_{i=1}^{n} x_i

Product

\prod_{i=1}^{n} x_i

Definite integral

\int x^2 \, dx

Partial derivative

\frac{\partial f}{\partial x}

Normal derivative

\frac{dy}{dx}

Gradient

\nabla f

Limit

\lim_{x \to 0} f(x)

Infinity

\infty

12. Logic Symbols

LaTeX

Result

Meaning

\land

AND

\lor

OR

\neg

NOT

\Rightarrow

Implies

\Leftarrow

Implied by

\Leftrightarrow

If and only if

\forall

For all

\exists

Exists

\nexists

Does not exist

Example

A \Rightarrow B

\forall x \in \mathbb{R}.

13. Arrows

Tip

x \to \infty

LaTeX

Result

\to

\rightarrow

\leftarrow

\Rightarrow

\Leftarrow

\leftrightarrow

\Leftrightarrow

\uparrow

\downarrow

\mapsto

14. Vectors, Matrices aur Linear Algebra

Vector

\mathbf{x}

\vec{x}

Matrix

\begin{bmatrix}

a & b \\

c & d

\end{bmatrix}

\begin{matrix}

a & b \\

c & d

\end{matrix}

\begin{pmatrix}

a & b \\

c & d

\end{pmatrix}

\begin{vmatrix}

a & b \\

c & d

\end{vmatrix}

A = [a_{ij}]_{m \times n}

15. Common ML Formula Patterns

Linear regression

\hat{y} = w_1x_1 + w_2x_2 + \cdots + w_nx_n + b

Vector form

\hat{y} = \mathbf{w}^{T}\mathbf{x} + b

Sigmoid function

\sigma(z) = \frac{1}{1 + e^{-z}}

Mean squared error

\mathrm{MSE} = \frac{1}{n}\sum_{i=1}^{n}(y_i-\hat{y}_i)^2

Gradient descent

\theta := \theta - \alpha \nabla_{\theta} J(\theta)

Bayes theorem

P(A \mid B) =

\frac{P(B \mid A)P(A)}{P(B)}

Expected value

\mathbb{E}[X]

Variance

\operatorname{Var}(X)

Norm

\|\mathbf{x}\|_2

Dot product

\mathbf{x} \cdot \mathbf{y}

16. Spacing Commands

LaTeX

Use

\,

Small space

\:

Medium space

\;

Large space

\quad

Large horizontal space

\qquad

Extra-large space

\!

Negative space

Example

\int x^2 \, dx →

17. Special Characters

LaTeX

Result

\%

\$

\&

\#

\_

\{

\}

\textbackslash

Backslash

Example

\text{Accuracy} = 95\%

18. Cases / Piecewise Function and Aligned Equations

Example

Piecewise Function

f(x) =

\begin{cases}

x^2, & x \geq 0 \\

-x, & x < 0

\end{cases}

Aligned Equations

\begin{aligned}

y &= mx + b \\

&= 2x + 3

\end{aligned}

19. important 25 commands for ML

1.        ^

2.        _

3.  \hat{}

4.  \bar{}

5.  \mathbf{}

6.  \theta

7.   \sigma

8.   \mu

9.   \sum

10.  \prod

11.  \int

12.  \frac{}{}

13.  \cdot

14.  \times

15.  \mid

16.  \text{}

17.  \left

18.  \right

19.  \in

20.  \mathbb{}

21.  \nabla

22.  \partial

23.  \approx

24.  \leq

25.  \geq

Share