Skip to main content

Writing Math in LaTeX

Introduction

LaTeX\LaTeX is the universal standard for writing mathematical expressions. Coupled with modern editors like VS Code, Obsidian, or Overleaf, it allows you to write mathematics efficiently on a PC.

Why LaTeX?

With LaTeX, you can write absolutely anything in mathematics:

  • Simple or complex equations with fractions, roots, indices
  • Matrices, determinants, systems of equations
  • Multiple integrals, partial derivatives
  • Specialized symbols (quantum physics, set theory, logic)
  • Commutative diagrams, graphs
  • Musical notation, chess, chemistry...

The good news: It is impossible (and useless) to know all LaTeX commands by heart. There are thousands of them, and even experts regularly look up the exact syntax.

Learn progressively

Don't worry if you don't remember everything immediately. Mastery comes with practice. Commands you use often (fractions, sums, integrals) will naturally engrave themselves in your memory. For the rest, there are tools!

Detexify: Your Best Ally

Detexify is a magical tool that recognizes symbols drawn with the mouse:

  1. Draw approximately the symbol you're looking for
  2. The AI suggests corresponding LaTeX commands
  3. Copy the command and use it in your document

Recommended workflow for beginners:

  • Use Detexify every time you're looking for an unknown symbol
  • Note the commands you use frequently
  • After a few weeks, you'll naturally know the common symbols
  • Create snippets for commands you type often (see following sections)
My personal advice

At first, keep Detexify open in a tab. Over time, you'll barely need it for common mathematics, but it will remain essential for rare or specialized symbols.

Learning objectives:

  • Master basic mathematical syntax in LaTeX
  • Be autonomous in learning new commands
  • Use Detexify to quickly find symbols
  • Create custom snippets to speed up typing
  • Use LaTeX in different environments (Markdown, Overleaf)

Example of what you'll be able to do:

(i=1nαixii!abf(t)dt2gy2y=0β2+γ2)x=θ^0vγ  +  limx0+k=0(1)kx2k(2k)!cosx  =  Γ ⁣(12)π\displaystyle \boxed{ \left( \begin{smallmatrix} \color{red}\displaystyle \sum_{i=1}^{n}\frac{\alpha_i x^{i}}{i!} & \displaystyle \color{purple} \cancel{\int_{a}^{b} f(t)\,\mathrm{d}t}\\[4pt] \displaystyle \left.\frac{\partial^2 g}{\partial y^2}\right|_{y=0} & \displaystyle \color{green}\sqrt{\beta^2+\gamma^2} \end{smallmatrix} \right)_{x=\hat{\theta}_{\scriptscriptstyle 0}} \cdot \vec{v}_{\lfloor\gamma\rfloor} \;+\; \overbrace{\lim_{x\to0^{+}}\sum_{k=0}^{\infty}\frac{(-1)^k x^{2k}}{(2k)!}}^{\cos x} \;=\; \color{blue} \frac{\Gamma\!\bigl(\tfrac{1}{2}\bigr)}{\sqrt{\pi}} }
warning

What is written above is false, it's only for demonstration purposes!

Prerequisites & Installation

Several options are available for writing mathematical expressions in LaTeX.

Code editor with LaTeX rendering via extensions: code.visualstudio.com

Recommended extensions:

Math Modes

LaTeX offers two modes for writing mathematics. Understanding these modes is essential as they influence the rendering of symbols and spacing.

Inline vs Display Mode

ModeSyntaxUsageRendering
Inline$ ... $ or \( ... \)Mathematics within running textCompact, reduced indices/exponents
Display$$ ... $$ or \[ ... \]Equations centered on their own lineEnlarged, indices/exponents placed above/below

Example:

The Pythagorean theorem is written $a^2 + b^2 = c^2$ for a right triangle.

$$
\int_{0}^{\infty} e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
$$

The Pythagorean theorem is written a2+b2=c2a^2 + b^2 = c^2 for a right triangle.

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

Force display style in inline mode

Use \displaystyle to get enlarged rendering even in inline mode:

Compare $\sum_{i=1}^{n} i$ with $\displaystyle\sum_{i=1}^{n} i$ in the text.

Compare i=1ni\sum_{i=1}^{n} i with i=1ni\displaystyle\sum_{i=1}^{n} i in the text.

Professional tip

In inline mode, avoid \displaystyle as it disrupts line spacing. Rather prefer switching to display mode with $$ ... $$ for complex expressions.

Quick Symbol Reference

Basic Operators

LaTeX CommandRenderingDescription
\cdot\cdotMultiplication
\frac{a}{b}ab\frac{a}{b}Fraction
a_i, x^{n+1}ai,  xn+1a_i,\; x^{n+1}Indices and exponents
\sqrt[n]{x}xn\sqrt[n]{x}nth root
\sum_{k=0}^nk=0n\displaystyle\sum_{k=0}^nSum
\int_a^bab\displaystyle\int_a^bIntegral
\prod_{i=1}^ni=1n\displaystyle\prod_{i=1}^nProduct
\lim_{x \to 0}limx0\displaystyle\lim_{x \to 0}Limit

Sets and Relations

CommandRenderingDescription
\in\inBelongs to
\notin\notinDoes not belong to
\subset\subsetStrict inclusion
\subseteq\subseteqInclusion
\cup\cupUnion
\cap\capIntersection
\emptyset\emptysetEmpty set
\mathbb{N}N\mathbb{N}Natural numbers
\mathbb{Z}Z\mathbb{Z}Integers
\mathbb{Q}Q\mathbb{Q}Rational numbers
\mathbb{R}R\mathbb{R}Real numbers
\mathbb{C}C\mathbb{C}Complex numbers

Adaptive Delimiters

Delimiters \left and \right automatically adapt to the content size:

$\displaystyle( \frac{a}{b} )$ vs $\displaystyle\left( \frac{a}{b} \right)$

(ab)\displaystyle( \frac{a}{b} ) vs (ab)\displaystyle\left( \frac{a}{b} \right)

DelimiterStaticAdaptive
Parentheses( )\left( \right)
Brackets[ ]\left[ \right]
Braces\{ \}\left\{ \right\}
Bars| |\left| \right|

Advanced Math Environments

align Environment

To align multiple equations on the & symbol:

\begin{align}
f(x) &= x^2 + 2x + 1 \\
&= (x + 1)^2
\end{align}
f(x)=x2+2x+1=(x+1)2\begin{align} f(x) &= x^2 + 2x + 1 \\ &= (x + 1)^2 \end{align}
Numbering

align automatically numbers each line. To avoid numbering, use align* or add \nonumber on a specific line.

cases Environment: Piecewise Functions

Ideal for defining conditional functions:

f(x) = \begin{cases}
x^2 & \text{if } x \geq 0 \\
-x & \text{if } x < 0
\end{cases}
f(x)={x2if x0xif x<0f(x) = \begin{cases} x^2 & \text{if } x \geq 0 \\ -x & \text{if } x < 0 \end{cases}

Application in probability:

P(X = k) = \begin{cases}
\binom{n}{k} p^k (1-p)^{n-k} & \text{if } k \in \{0,\ldots,n\} \\
0 & \text{otherwise}
\end{cases}
P(X=k)={(nk)pk(1p)nkif k{0,,n}0otherwiseP(X = k) = \begin{cases} \binom{n}{k} p^k (1-p)^{n-k} & \text{if } k \in \{0,\ldots,n\} \\ 0 & \text{otherwise} \end{cases}

Matrices: Types and Variants

LaTeX offers several matrix environments with different delimiters:

EnvironmentDelimitersCodeRendering
matrixNone\begin{matrix} a & b \\ c & d \end{matrix}abcd\begin{matrix} a & b \\ c & d \end{matrix}
pmatrix( )\begin{pmatrix} a & b \\ c & d \end{pmatrix}(abcd)\begin{pmatrix} a & b \\ c & d \end{pmatrix}
bmatrix[ ]\begin{bmatrix} a & b \\ c & d \end{bmatrix}[abcd]\begin{bmatrix} a & b \\ c & d \end{bmatrix}
Bmatrix{ }\begin{Bmatrix} a & b \\ c & d \end{Bmatrix}{abcd}\begin{Bmatrix} a & b \\ c & d \end{Bmatrix}
vmatrix| |\begin{vmatrix} a & b \\ c & d \end{vmatrix}abcd\begin{vmatrix} a & b \\ c & d \end{vmatrix}
smallmatrixNone\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}abcd\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}
Practical usage: smallmatrix

smallmatrix is perfect for vectors and small matrices in inline mode, avoiding disruption of paragraph line spacing. Use it with \left( and \right) to add delimiters.

The matrix $\left(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right)$ is invertible if $ad - bc \neq 0$.

The matrix (abcd)\left(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right) is invertible if adbc0ad - bc \neq 0.

Complete example: system of linear equations:

\begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{bmatrix}
\begin{pmatrix}
x \\
y \\
z
\end{pmatrix}
=
\begin{pmatrix}
b_1 \\
b_2 \\
b_3
\end{pmatrix}
[123456789](xyz)=(b1b2b3)\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} \begin{pmatrix} x \\ y \\ z \end{pmatrix} = \begin{pmatrix} b_1 \\ b_2 \\ b_3 \end{pmatrix}

Speed Up Input with Snippets

Snippets transform a shortcut into predefined code. Example: fr + Tab\frac{•}{•}

Snippets in VS Code

Initial Setup

  1. Ctrl/Cmd + Shift + PPreferences: Configure User Snippets
  2. Choose latex.json (or markdown.json for Markdown)
  3. Add your snippets in JSON format

Anatomy of a VS Code Snippet

"sum": {
"prefix": "sum", // Shortcut to type
"body": "\\sum_{i=$1}^{$2} $3", // Generated code
"description": "Sum" // Description (optional)
}

Properties of a snippet:

  • prefix: The shortcut to type (e.g., sum)
  • body: The generated LaTeX code (escape backslashes: \\)
  • description: Description displayed in autocompletion

Smart navigation (tab stops): $1, $2, $3 define where the cursor is positioned. After insertion, your cursor is placed on $1, then Tab moves to $2, etc.

Default values: ${1:value} defines a default value for the tab stop.

Examples of Useful Snippets

Add these snippets to your latex.json or markdown.json configuration:

{
"fraction": {
"prefix": "fr",
"body": "\\frac{$1}{$2}$0",
"description": "Fraction a/b"
},
"sum": {
"prefix": "sum",
"body": "\\sum_{${1:i}=${2:0}}^{${3:n}} $0",
"description": "Sum with bounds"
},
"integral": {
"prefix": "int",
"body": "\\int_{${1:a}}^{${2:b}} ${3:f(x)} \\,\\mathrm{d}${4:x}$0",
"description": "Definite integral"
},
"limit": {
"prefix": "lim",
"body": "\\lim_{${1:x} \\to ${2:0}} $0",
"description": "Limit"
},
"matrix": {
"prefix": "mat",
"body": [
"\\begin{${1|matrix,pmatrix,bmatrix,vmatrix|}}",
"\t$2",
"\\end{${1|matrix,pmatrix,bmatrix,vmatrix|}}"
],
"description": "Matrix with choice of delimiters"
},
"cases": {
"prefix": "cas",
"body": [
"\\begin{cases}",
"\t$1 & \\text{if } $2 \\\\",
"\t$3 & \\text{if } $4",
"\\end{cases}$0"
],
"description": "Piecewise function (2 cases)"
},
"align": {
"prefix": "ali",
"body": [
"\\begin{align${1:*}}",
"\t$2 &= $3 \\\\",
"\t&= $4",
"\\end{align${1:*}}$0"
],
"description": "Equation alignment"
}
}
Advanced technique: multiple choices

The ${1|option1,option2,option3|} syntax displays a dropdown menu upon insertion. Used in the mat snippet above to choose the matrix type.

Snippets in Obsidian with Latex Suite

Latex Suite is the reference extension for writing mathematics quickly in Obsidian. It transforms the writing experience by enabling input as fluid as with pen on paper.

Installation:

  1. Open Obsidian → SettingsCommunity plugins options
  2. Search for "Latex Suite" in the plugin browser
  3. Click Install, then Enable

Key features:

FeatureDescriptionExample
Auto-expansionAutomatic expansion without pressing Tab//\frac{•}{•}
Visual modeTransformation of selected textSelect x+y then S\sqrt{x+y}
Context-awareSnippets active only in math mode@a works in $$...$$ only
Regex triggersAdvanced patterns for auto-formattingx2x_{2} automatically
VariablesPredefined symbols to simplify regex${GREEK} for all Greek letters

Anatomy of a Latex Suite Snippet

A Latex Suite snippet is a JavaScript object with 4 main properties:

{
trigger: "//", // Trigger: text that activates the snippet
replacement: "\\frac{$0}{$1}$2", // Replacement: generated LaTeX code
options: "mA", // Options: context and behavior
priority: 1, // Execution priority. In case of conflict, the snippet with the highest priority will be preferred (optional)
description: "Quick fraction", // Description (optional)
flags: "i" // Regex flags (optional)
}

Options explained:

OptionMeaningDescription
tText modeSnippet active outside math environments
mMath modeSnippet active in $$...$$ or $...$
MBlock math modeSnippet active in $$...$$
nInline math modeSnippet active in $...$
cCode modeSnippet active in ``` ```
AAuto-expandAutomatic expansion without Tab
vVisualSnippet active when text is selected
wWord boundaryRequires a space or line start before the trigger
rRegexThe trigger is a regular expression

Tab stops with $0, $1, $2:

As in VS Code, tab stops define where the cursor is positioned after expansion:

{ trigger: "sum", replacement: "\\sum_{${0:k}=${1:0}}^{${2:n}} $3", options: "mA" }
  • ${0:k}: First stop with default value "k"
  • ${1:0}: Second stop with default value "0"
  • $3: Last stop without default value
{ trigger: "begin", replacement: "\\begin{$1}\n  $2\n\\end{$1}", options: "MA" }

Here, the first stop $1 is automatically reused in \end{...}.

Advanced regex triggers:

Snippets can use regex to detect patterns:

{
trigger: "([A-Za-z])(\\d)", // Detects letter + digit
replacement: "[[0]]_{[[1]]}", // [[0]] = letter, [[1]] = digit
options: "rmA" // r = regex
}

Example: x2x_{2} automatically

Predefined variables:

The default_snippet_variables.js file defines constants to simplify regex:

{
"${GREEK}": "alpha|beta|gamma|delta|...",
"${SYMBOL}": "parallel|nabla|hbar|...",
"${UNITS}": "V|A|Hz|kg|m|..."
}

Usage in a snippet:

{
trigger: "\\\\(${GREEK})([A-Za-z])",
replacement: "\\[[0]] [[1]]", // Inserts a space after Greek letters
options: "rmA"
}

My Personal Configuration

I've created a complete configuration with over 250 snippets optimized for mathematics and physics. This configuration is the result of two years of daily note-taking.

Complete configuration

The snippets cover:

  • Automatic Obsidian callouts
  • Advanced mathematical symbols
  • Physical units (automatic detection: 10Hz10\text{Hz})
  • Shortcuts for sequences and series: uu(un)nN(u_{n})_{n\in\mathbb{N}}, ff(fn)nN(f_{n})_{n\in\mathbb{N}}, SS(Sn)nN(S_{n})_{n\in\mathbb{N}}
  • Automatic conversion of isolated letters to math mode

I advise you to read the document even diagonally to discover them and know that they exist, so you can come back later with a ctrl+f.

Installation:

  1. Copy the contents of the two files above
  2. In Obsidian: Settings → Latex Suite → Snippets
  3. Paste the contents of default_snippets.js in the text area
  4. Go to Snippets variables
  5. Paste the contents of default_snippet_variables.js
Recommended configuration

I highly recommend using my configuration as a base, then customize it according to your needs.

Examples of snippets from my configuration
1. Quick entry to math mode
ShortcutResultDescription
ml$\displaystyle •$Inline math mode with displaystyle
dm$$\n•\n$$Display math mode (block)
2. Fractions and operations
ShortcutResultDescription
//\frac{•}{•}Quick fraction
sr^{2}Square
cb^{3}Cube
sq\sqrt{•}Square root
eee^{•}Exponential
3. Greek letters with @

The @ prefix allows quick insertion of Greek letters:

ShortcutResultShortcutResult
@a\alpha@G\Gamma
@b\beta@D\Delta
@g\gamma@T\Theta
@d\delta@L\Lambda
@e\varepsilon@S\Sigma
@t\theta@O\Omega
@l\lambda@P\Pi
@p\varphi
@o\omega
@s\sigma
4. Sets and symbols with double capitals
ShortcutResultDescription
RRR\mathbb{R}Real numbers
CCC\mathbb{C}Complex numbers
NNN\mathbb{N}Natural numbers
ZZZ\mathbb{Z}Integers
KKK\mathbb{K}Generic field
LLL\mathcal{L}Function space
MMM\mathcal{M}Matrix space
5. Automatic indices

Snippets automatically detect common indices:

x2     →  x_{2}           (numeric indices)
xii → x_{i} (indices i, j, k, n, m)
xip1 → x_{i+1} (indices with +)
xim2 → x_{i-2} (indices with -)
6. Accents and modifiers

Type a letter followed by:

SuffixResultExamplePreview
vec\vec{•}xvec\vec{x}x\vec{x}
dot\dot{•}xdot\dot{x}x˙\dot{x}
ddot\ddot{•}xddot\ddot{x}x¨\ddot{x}
bar\bar{•}xbar\bar{x}xˉ\bar{x}
hat\hat{•}xhat\hat{x}x^\hat{x}
7. Derivatives and integrals
ShortcutResultExample previewDescription
par\frac{\partial •}{\partial •}fx\frac{\partial f}{\partial x}Partial derivative
pa2\frac{\partial^{2} •}{\partial •^{2}}2fx2\frac{\partial^{2} f}{\partial x^{2}}Second derivative
ddt\frac{d}{dt}ddt\frac{d}{dt}Time derivative
int\int_{0}^{\infty} • \, d•0f(x)dx\int_{0}^{\infty} f(x) \, dxIntegral with bounds
oinf\int_{0}^{\infty} • \, d•0f(x)dx\int_{0}^{\infty} f(x) \, dxIntegral from 0 to infinity
infi\int_{-\infty}^{+\infty} • \, d•+f(x)dx\int_{-\infty}^{+\infty} f(x) \, dxIntegral over R
8. Quick matrices
ShortcutResultExample previewDescription
pmat\begin{pmatrix}\n•\n\end{pmatrix}(abcd)\begin{pmatrix} a&b\\c&d \end{pmatrix}Matrix with parentheses
bmat\begin{bmatrix}\n•\n\end{bmatrix}[abcd]\begin{bmatrix} a&b\\c&d \end{bmatrix}Matrix with brackets
vmat\begin{vmatrix}\n•\n\end{vmatrix}abcd\begin{vmatrix} a&b\\c&d \end{vmatrix}Determinant
cases\begin{cases}\n•\n\end{cases}{0if x=01otherwise\begin{cases} 0&\text{if } x=0\\1&\text{otherwise} \end{cases}Piecewise function
9. Visual mode: selection transformations

Select text then press:

KeyTransformationExamplePreview
S\sqrt{...}x+1\sqrt{x+1}x+1\sqrt{x+1}
U\underbrace{...}_{•}a+b\underbrace{a+b}_{•}a+b\underbrace{a+b}_{•}
O\overbrace{...}^{•}a+b\overbrace{a+b}^{•}a+b\overbrace{a+b}^{•}
C\cancel{...}x\cancel{x}x\cancel{x}

Practical Exercises

#TaskEducational objective
1Write a sheet containing A1 level differentiation formulasMaster align and cases environments
2Create a VS Code snippet for the matrix environmentAutomate input
3Import the document on Overleaf and share it with a classmateDiscover revision mode

Resources

Documentation and Online Tools

Obsidian Configuration Files

tip

These files contain over 250 snippets optimized for quick input of mathematics, physics, and chemistry in Obsidian. See the Snippets in Obsidian section for installation instructions.