[The Correct Way!] How to Typeset Matrix in Latex

Learn how to typeset and create matrices in LaTeX using the `matrix` environment under the `amsmath` package.

In mathematics, matrices are used to represent linear transformations — such as rotations, translations, and scaling — and systems of linear equations. They are represented as arrays of numbers and are used to perform operations such as matrix multiplication and matrix inversion.

In this article, we will learn how to typeset matrices — the right way — using the matrix environment provided by the amsmath package. Our goal is to create matrices that are clear, concise, and easy to read.

How to typeset matrix in LaTeX

We can create a beautiful matrix in LaTeX with the help of the matrix environment provided by the amsmath package. This matrix environment provides us with a wide range of formatting options, including the ability to specify the alignment and spacing of the matrix elements.

To use the amsmath package in your environment, you can include the following in the preamble of your LaTeX document:

\usepackage{amsmath}

To use the matrix environment, we enclose the matrix definition within a \begin{matrix} and \end{matrix} block. Here’s an example:

\begin{equation*}
A_{m,n} = 
\begin{matrix}
a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
\vdots  & \vdots  & \ddots & \vdots  \\
a_{m,1} & a_{m,2} & \cdots & a_{m,n} 
\end{matrix}
\end{equation*}

This will produce a matrix with three rows and three columns, as shown below:

Plain Matrix in LaTeX
A matrix in LaTeX without a delimiter.

Matrix with parenthesis delimiter

If you want to enclose your matrix with a parenthesis, you can change \begin{matrix} and \end{matrix} in Lines 3 and 8 with \begin{pmatrix} and \end{pmatrix} respectively.

\begin{pmatrix}
a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
\vdots  & \vdots  & \ddots & \vdots  \\
a_{m,1} & a_{m,2} & \cdots & a_{m,n} 
\end{pmatrix}

The above code will render the following:

Matrix in LaTex with Parenthesis
A matrix in LaTex with parenthesis delimiter.

Matrix with square brackets delimiter

The \begin{bmatrix} command creates a matrix with square brackets or boundaries.

\begin{bmatrix}
a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
\vdots  & \vdots  & \ddots & \vdots  \\
a_{m,1} & a_{m,2} & \cdots & a_{m,n} 
\end{bmatrix}

The above code will render the following:

A matrix in LaTeX with square brackets delimiter.

Matrix with curly brackets delimiter

The \begin{Bmatrix} command creates a matrix with square brackets boundaries.

\begin{Bmatrix}
a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
\vdots  & \vdots  & \ddots & \vdots  \\
a_{m,1} & a_{m,2} & \cdots & a_{m,n} 
\end{Bmatrix}

The above code will render a matrix with a curly brackets delimiter.

A matrix in LaTeX with curly brackets delimiter.

Matrix with a pipe delimiter

The \begin{vmatrix} command creates a matrix with a pipe delimiter.

\begin{vmatrix}
a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
\vdots  & \vdots  & \ddots & \vdots  \\
a_{m,1} & a_{m,2} & \cdots & a_{m,n} 
\end{vmatrix}

The above code will render a matrix with a pipe delimiter.

A matrix in LaTeX with a pipe delimiter

Matrix with a double-pipe delimiter

The \begin{Vmatrix} command creates a matrix with a double pipe delimiter.

\begin{Vmatrix}
a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
\vdots  & \vdots  & \ddots & \vdots  \\
a_{m,1} & a_{m,2} & \cdots & a_{m,n} 
\end{Vmatrix}

The above code will render the following:

A matrix in LaTeX with a double-pipe delimiter

Custom delimiter

If you need to create matrices with different delimiters, you can add them manually to a plain matrix. For example:

\begin{equation*}
A_{m,n} = 
    \left\lceil
    \begin{matrix}
        a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
        a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
        \vdots  & \vdots  & \ddots & \vdots  \\
        a_{m,1} & a_{m,2} & \cdots & a_{m,n} 
    \end{matrix}
    \right\rceil
\end{equation*}

The above code will render the following:

\begin{equation*}
A_{m,n} = 
    \left\langle
    \begin{matrix}
        a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
        a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
        \vdots  & \vdots  & \ddots & \vdots  \\
        a_{m,1} & a_{m,2} & \cdots & a_{m,n} 
    \end{matrix}
    \right\rvert
\end{equation*}

The above code will render the following:

\begin{equation*}
A_{m,n} = 
    \left\langle
    \begin{matrix}
        a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
        a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
        \vdots  & \vdots  & \ddots & \vdots  \\
        a_{m,1} & a_{m,2} & \cdots & a_{m,n} 
    \end{matrix}
    \right\rangle	
\end{equation*}

The above code will render the following:

Inline matrices in LaTex

When typesetting inline math, the usual matrix environments above may look too big. It may be better to use smallmatrix in such situations, although you will need to provide your own delimiters.

\noindent Let $A=$
 $\begin{pmatrix}
  a & b\\ 
  c & d
\end{pmatrix}$ be a matrix. With the small version $A=$ 
$\big(\begin{smallmatrix}
  a & b\\
  c & d
\end{smallmatrix}\big)$.

The above code will render the following:

Using the `array` environment to typeset matrix in LaTeX

The array environment in LaTeX is a versatile tool for creating matrices and tables. It offers more flexibility and control over the formatting of the matrix. It provides column alignment (left, center, or right) and optional vertical lines separating the columns.

To use the array environment, you need to specify the number of columns and the alignment of the columns using the following syntax:

\begin{array}{col₁col₂…colₙ}
  item₁₁ & item₁₂ … & item₁ₙ \\
  item₂₁ & item₂₂ … & item₂ₙ \\
             ⋮
  itemₘ₁ & itemₘ₂ … & itemₘₙ
\end{array}

To write an m×n matrix in LaTeX using the array environment, we need to specify the number of columns in the \begin{array}{} command. That’s in Line 1 of the code above. Here’s an example:

\[
  A_{m\times n} =
  \left[ {\begin{array}{cccc}
    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{array} } \right]
\]

The above code should give you the following:

Summary

In this article, we have covered the basics of typesetting matrices in LaTeX using the matrix environment provided by the amsmath package. We showed how to use the pmatrix, bmatrix, vmatrix, and Vmatrix environments to create matrices with different types of delimiters.

We hope that this guide has been helpful and that you now feel more confident in your ability to create professional-quality matrices in LaTeX. Remember to refer back to this guide as you work on your own matrices, and don’t hesitate to experiment with different formatting options to see what works best for you.

As you continue to work with LaTeX, you will find that there are many other powerful tools and features available for typesetting math and scientific documents. We encourage you to explore these resources and to continue learning and growing as a LaTeX user. Good luck and happy typesetting!