Everything on the site.

Big-O notationAlgorithms · Count the steps in real loops, then compare n, n log n, n² and 2ⁿ on a live chart.RecursionAlgorithms · Watch frames push and pop on the call stack for factorial and Fibonacci.BFS vs DFSAlgorithms · A queue against a stack: draw a graph and see which order each search visits it in.Sorting algorithmsAlgorithms · Seven sorts on the same input, with comparison, swap and write counts side by side.Binary searchAlgorithms · Halve the range, probe the middle, repeat: with lo, mid and hi drawn on the array.Sets, relations and functionsDiscrete maths · Shade any set expression on a Venn diagram and test a relation for its properties.Proof by inductionDiscrete maths · Four worked proofs revealed one step at a time, and a broken one to diagnose.CombinatoricsDiscrete maths · An exact nPr and nCr calculator, and a Pascal’s triangle that shows its own addition rule.Boolean algebraDiscrete maths · The laws, De Morgan, and normal forms, with a truth-table generator built in.Finite-state machinesTheory · Build a DFA in a table, see it drawn, and run strings through it one symbol at a time.Domain theoryTheory · Why recursion needs least fixed points, and how CPOs, Scott continuity and powerdomains supply them.Truth table generatorTools · Expression in, truth table out: with DNF, CNF, a minimal form and LaTeX.Graph traversal visualiserTools · Draw a graph, then animate BFS, DFS and Dijkstra with the frontier shown.Recursion tree visualiserTools · Paste a recursive function or a recurrence and see its call tree and call count.Gradient descentML maths · Drag the starting point, set the learning rate, and watch it converge or blow up.Vectors and embeddingsML maths · Bend the plane with a 2×2 matrix and measure how close toy word vectors are.Softmax and cross-entropyML maths · Edit the logits and watch the probabilities, the loss and the gradient update live.Attention mechanismML maths · Q, K and V for three tokens, a live score heatmap, and the weighted sum of values.Image to video attentionML maths · One photo, 16 frames: switch reference attention on or off and watch the colours hold or drift.Formatting a CS paper in LaTeXLaTeX · Page limits, \documentclass, front matter, theorem environments and the bibliography.Camera-ready checklistLaTeX · Fonts embedded, PDF/A, copyright form, source bundle, and why final versions get rejected.LaTeX templatesLaTeX · Where the official classes live, plus a minimal article template you can copy.Boolean algebra calculatorTools · A Boolean expression simplifier that names the law used at every step.K-map solverTools · Fill in a Karnaugh map and see the groups, the minimal SOP and POS, and the essential terms.Logic gate simulatorTools · Wire up gates, flip the switches, and get the truth table and expression of your circuit.Regex to NFA converterTools · Type a regular expression and watch Thompson’s construction build its NFA, fragment by fragment.NFA to DFA converterTools · Enter an NFA or a regex and step through the subset construction, then minimise the DFA.DFA simulatorTools · Edit a DFA, run strings through it step by step, batch-test them and share the machine as a link.Regular expressions to automataTheory · Regex to NFA to DFA to minimal DFA, worked through on one example with every set shown.The halting problemTheory · Why no program can tell whether every program halts, with the paradox program played out.Pigeonhole principleDiscrete maths · n pigeons, k holes: why some hole gets ⌈n/k⌉, and the surprising things that follows from it.Dijkstra’s algorithmAlgorithms · Shortest paths with a priority queue: a worked table, a proof, and a graph you can redraw.Hash functionsAlgorithms · Keys in, buckets out: hash a word four ways, then watch chaining and probing handle collisions.Sorting algorithm visualizerTools · Up to seven sorts race on the same data, then step through any one of them line by line.LaTeX table generatorTools · Type or paste a table, merge cells, pick booktabs, and copy clean tabular code.Combination calculatorTools · Exact nCr with the cancelling shown, combinations with repetition, and a full list for small n.Permutation calculatorTools · nPr, nʳ, anagrams like MISSISSIPPI and round-table seatings, with exact working.AVL tree visualizerTools · Insert, delete and search, then replay every balance factor and rotation step by step.IEEE 754 converterTools · Decimal to float bits and back, with the exact stored value and why 0.1 isn’t exact.Two’s complement calculatorTools · Signed integers to bits and back, with the working, other encodings and overflow checks.Power set calculatorTools · Every subset of your set, sorted by size or bitmask, with 2ⁿ counted exactly.Huffman coding calculatorTools · Text or frequencies in, Huffman tree and codes out, with every merge shown in order.LaTeX matricesLaTeX · pmatrix, bmatrix, augmented and large matrices, each with copyable code and its result.AlgorithmsSection · Step-through visualisations of sorting, searching, recursion, graph traversal, Dijkstra’s algorithm, hash functions and Big-O growth, with pseudocode that follows along.Discrete mathsSection · Sets, relations and functions, proof by induction, counting, the pigeonhole principle and Boolean algebra, explained with worked examples and interactive checkers.TheorySection · Finite-state machines, regular expressions and automata, the halting problem, and the mathematical semantics of programming languages, explained from first principles.ML mathsSection · Gradient descent, vectors and embeddings, softmax, attention and image to video models, each with a formula, a worked numeric example and a live version.ToolsSection · Free browser tools: truth tables, K-maps, logic gates, nCr, power sets, regex to DFA, sorting, graph and AVL tree visualisers, IEEE 754, two’s complement and Huffman codes.LaTeXSection · How to format a CS conference or workshop paper in LaTeX, a camera-ready checklist, a minimal template you can adapt, and how to typeset matrices.
Guide

How to write a matrix in LaTeX, with copyable examples

How to write matrices in LaTeX: the amsmath environments for round, square, curly and vertical brackets, augmented matrices, large m × n matrices with dots, block matrices, small inline matrices and determinants. Every example below shows the code with a copy button and the typeset result, and the generator builds the code for any size you need.

Interactive: Matrix code generator with live previewUpdated

The short answer

Load amsmath in the preamble, then write the entries inside a matrix environment, with & between columns and \\ at the end of each row:

\usepackage{amsmath}  % in the preamble

\[
  A = \begin{pmatrix}
    1 & 2 & 3 \\
    4 & 5 & 6
  \end{pmatrix}
\]
A=(123456)A = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix}

The environment name sets the brackets, and the matrix must sit inside maths mode: \[ … \] or equation for a displayed matrix, or $ … $ inline. The number of columns is worked out from the rows, so you never declare it.

Matrix generator

Choose the size and brackets, type or paste the entries, and copy the code. Paste a block of cells from Excel, Google Sheets or a CSV file into the first cell to fill the whole grid.

LaTeX matrix generator

Edit any cell below, or paste rows from a spreadsheet into the first cell.

The six bracket styles

amsmath provides one environment per bracket style. They all take the same body.

EnvironmentBracketsResultTypical use
matrixnoneabcd\begin{matrix} a & b \\ c & d \end{matrix}Inside your own delimiters
pmatrix( )(abcd)\begin{pmatrix} a & b \\ c & d \end{pmatrix}Most textbooks and papers
bmatrix[ ][abcd]\begin{bmatrix} a & b \\ c & d \end{bmatrix}Engineering, many linear algebra courses
Bmatrix{ }{abcd}\begin{Bmatrix} a & b \\ c & d \end{Bmatrix}Rare; sets of vectors
vmatrix| |∣abcd∣\begin{vmatrix} a & b \\ c & d \end{vmatrix}Determinants
Vmatrix‖ ‖∥abcd∥\begin{Vmatrix} a & b \\ c & d \end{Vmatrix}Matrix norms

Pick one style for a whole document. Round brackets are the most common in mathematics and computer science.

Column vectors and row vectors

A vector is just a matrix with one column or one row:

\mathbf{v} = \begin{pmatrix} x \\ y \\ z \end{pmatrix}, \qquad
\mathbf{w}^\top = \begin{pmatrix} w_1 & w_2 & w_3 \end{pmatrix}
v=(xyz),w⊤=(w1w2w3)\mathbf{v} = \begin{pmatrix} x \\ y \\ z \end{pmatrix}, \qquad \mathbf{w}^\top = \begin{pmatrix} w_1 & w_2 & w_3 \end{pmatrix}

To save vertical space in running text, write a column vector as a transposed row: $(x, y, z)^\top$.

A general m × n matrix with dots

Use \cdots (horizontal), \vdots (vertical) and \ddots (diagonal) for the entries you leave out. Choose “General m × n with dots” in the generator to get this layout at any size.

A = \begin{pmatrix}
  a_{11} & a_{12} & \cdots & a_{1n} \\
  a_{21} & a_{22} & \cdots & a_{2n} \\
  \vdots & \vdots & \ddots & \vdots \\
  a_{m1} & a_{m2} & \cdots & a_{mn}
\end{pmatrix}
A=(a11a12⋯a1na21a22⋯a2n⋮⋮⋱⋮am1am2⋯amn)A = \begin{pmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{pmatrix}

Write a_{ij} with braces: a_ij puts only the i in the subscript. For two-digit or computed indices, add a comma: a_{i,j+1}.

Identity, zero and diagonal matrices

I_3 = \begin{pmatrix}
  1 & 0 & 0 \\
  0 & 1 & 0 \\
  0 & 0 & 1
\end{pmatrix}, \qquad
D = \begin{pmatrix}
  \lambda_1 &        &           \\
            & \ddots &           \\
            &        & \lambda_n
\end{pmatrix}
I3=(100010001),D=(λ1⋱λn)I_3 = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{pmatrix}, \qquad D = \begin{pmatrix} \lambda_1 & & \\ & \ddots & \\ & & \lambda_n \end{pmatrix}

Empty cells are allowed, which is the usual way to show that the off-diagonal entries of a diagonal matrix are zero. To make the zeros explicit but quiet, write a large 0 in one corner, or use \mathbf{0} for a zero block.

Augmented matrices

amsmath has no environment with a vertical bar, so use array with a | in the column specification and add brackets with \left[ … \right]. The {cc|c} here means two centred columns, a vertical rule, then one more column:

\left[\begin{array}{cc|c}
  2 & 1 & 5 \\
  1 & -3 & -1
\end{array}\right]
[2151−3−1]\left[\begin{array}{cc|c} 2 & 1 & 5 \\ 1 & -3 & -1 \end{array}\right]

The same trick draws a matrix with a horizontal rule (\hline) or splits off several columns, for example {ccc|ccc} for the [A∣I][A \mid I] layout used when inverting a matrix by row reduction:

\left[\begin{array}{ccc|ccc}
  1 & 2 & 3 & 1 & 0 & 0 \\
  0 & 1 & 4 & 0 & 1 & 0 \\
  5 & 6 & 0 & 0 & 0 & 1
\end{array}\right]
[123100014010560001]\left[\begin{array}{ccc|ccc} 1 & 2 & 3 & 1 & 0 & 0 \\ 0 & 1 & 4 & 0 & 1 & 0 \\ 5 & 6 & 0 & 0 & 0 & 1 \end{array}\right]

Row operations are often written beside an arrow: \xrightarrow{R_2 - 2R_1} gives →R2−2R1\xrightarrow{R_2 - 2R_1}.

Determinants

\det A = \begin{vmatrix} a & b \\ c & d \end{vmatrix} = ad - bc
det⁡A=∣abcd∣=ad−bc\det A = \begin{vmatrix} a & b \\ c & d \end{vmatrix} = ad - bc

Use vmatrix rather than typing | characters around a matrix: the bars then grow with the content and get the right spacing.

Small matrices inside a line of text

A full pmatrix inside a paragraph pushes the lines apart. smallmatrix sets the entries at script size so the matrix fits the line height, like (1001)\left(\begin{smallmatrix} 1 & 0 \\ 0 & 1 \end{smallmatrix}\right) here. It has no brackets of its own:

$\left(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right)$
(abcd)\left(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right)

With the mathtools package you can write psmallmatrix, bsmallmatrix and the others instead.

Block matrices

Treat each block as an entry. Bold or capital letters keep blocks distinct from scalars:

M = \begin{pmatrix}
  A & B \\
  C & D
\end{pmatrix}, \qquad
\begin{pmatrix}
  A & \mathbf{0} \\
  \mathbf{0} & I_k
\end{pmatrix}
M=(ABCD),(A00Ik)M = \begin{pmatrix} A & B \\ C & D \end{pmatrix}, \qquad \begin{pmatrix} A & \mathbf{0} \\ \mathbf{0} & I_k \end{pmatrix}

To draw the partition lines, use array as for augmented matrices, with | in the column specification and \hline between rows:

\left(\begin{array}{c|c}
  A & B \\ \hline
  C & D
\end{array}\right)
(ABCD)\left(\begin{array}{c|c} A & B \\ \hline C & D \end{array}\right)

Aligning entries and adjusting spacing

Matrix entries are centred. Columns of numbers with minus signs look ragged, so right-align them with the starred environments from mathtools, which take an optional alignment:

\usepackage{mathtools}  % in the preamble

\begin{pmatrix*}[r]
  -1 & 3 \\
  2 & -10
\end{pmatrix*}
(−132−10)\begin{pmatrix*}[r] -1 & 3 \\ 2 & -10 \end{pmatrix*}

To loosen a crowded matrix, for example one full of fractions, increase the row spacing locally and widen the gap between columns:

{\renewcommand{\arraystretch}{1.4}  % rows 40% taller
 \setlength{\arraycolsep}{8pt}       % default is 5pt
 \begin{pmatrix}
   \frac{1}{2} & \frac{1}{3} \\
   \frac{1}{4} & \frac{1}{5}
 \end{pmatrix}}
(12131415)\def\arraystretch{1.4}\begin{pmatrix} \frac{1}{2} & \frac{1}{3} \\ \frac{1}{4} & \frac{1}{5} \end{pmatrix}

The outer braces keep the change local to this matrix. In fraction-heavy matrices, \tfrac or a slash (1/2) often reads better than \frac.

Matrices with more than 10 columns

amsmath matrices allow at most 10 columns by default. An 11th column gives the error “Extra alignment tab has been changed to \cr”. Raise the limit in the preamble:

\setcounter{MaxMatrixCols}{20}

For very wide matrices, consider whether the reader needs every entry: a general pattern with dots, or a description in words, is usually clearer.

Labelling rows and columns

To label rows and columns, for example to name the states of a transition matrix, the simplest portable method is an array with the labels in an extra row and column:

\begin{array}{c|ccc}
      & s_1 & s_2 & s_3 \\ \hline
  s_1 & 0.5 & 0.5 & 0   \\
  s_2 & 0   & 0.2 & 0.8 \\
  s_3 & 1   & 0   & 0
\end{array}
s1s2s3s10.50.50s200.20.8s3100\begin{array}{c|ccc} & s_1 & s_2 & s_3 \\ \hline s_1 & 0.5 & 0.5 & 0 \\ s_2 & 0 & 0.2 & 0.8 \\ s_3 & 1 & 0 & 0 \end{array}

For true bordered matrices, with brackets around the entries but not the labels, the blkarray and kbordermatrix packages on CTAN do the job; check that your venue’s class allows them.

Common errors

  • “Missing $ inserted”: the matrix is outside maths mode. Wrap it in \[ … \].
  • “Environment pmatrix undefined”: \usepackage{amsmath} is missing.
  • “Extra alignment tab has been changed to \cr”: a row has more & than columns allowed (more than 10, or more than the array specification lists).
  • A blank row at the bottom: there is a \\ after the last row. Remove it.
  • Wrong brackets size: you typed ( … ) around a matrix. Use pmatrix, or \left( … \right).
Tables are not matrices For data in rows and columns with text and headings, use a table environment, not a matrix. The LaTeX table generator builds tabular code with booktabs rules.

Related guides

References

  1. American Mathematical Society, User’s Guide for the amsmath Package, version 2.1, 2020. Section 4.1, matrices.
  2. M. Høgholm, L. Madsen, W. Robertson and J. Wright, The mathtools package, package documentation.
  3. F. Mittelbach and U. Fischer, The LaTeX Companion, 3rd ed., Addison-Wesley, 2023. Chapter 11, mathematics.