latex

How to Create Matrices in LaTeX

A matrix is a rectangular array of values whose size depends on the number of rows and columns. When working with scientific and other technical documents, we encounter instances where we need to use a matrix.

This tutorial discusses how to write matrices using LaTeX.

How to Write and Render Matrices in LaTeX

To write and render matrices in LaTeX, you need to import the amsmath package. You do this in the preamble as:

\usepackage{amsmath}

LaTex Matrices Environments

Once you import the amsmath package, you get access to various types of environments to create matrices.

The following are the matrices’ environment variables.

  • Matrix – Does not include any enclosing symbols
  • pmatrix – Uses parenthesis to enclose values in the matrix
  • bmatrix – this environment uses square brackets to enclose the matrix
  • Bmatrix – Uses curly braces to enclose the matrix.
  • vmatrix – the small v environment uses single pipes to enclose the values.
  • Vmatrix – Use double pipes.

How to Write Various LaTex Matrices

The following shows how to write various matrix types in LaTeX.

How to Create the No Braces Matrix

To write a matrix with no braces, we use the matrix environment as shown in the example code below:

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath}

\begin{document}

$$\begin{matrix}

3 & 0 & 0 \\

-1 & 0 & 3 \\

a & b & c \\

\end{matrix}$$

\end{document}

The example code above creates a 3 x 3 matrix as shown in the image below:

How to Create the Parenthesis Matrix

To enclose a matrix with parenthesis, use the pmatrix variable. Here is an example code:

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath}

\begin{document}

$$\begin{pmatrix}

3 & 0 & 0 \\

-1 & 0 & 3 \\

a & b & c \

\end{pmatrix}$$

\end{document}

The result of the matrix code above is:

How to Create the Square Brackets Matrix

Using the bmatrix environment, you can create a square bracket matrix as shown in the example code below:

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath}

\begin{document}

$$\begin{bmatrix}

3 & 0 & 0 \\

-1 & 0 & 3 \\

a & b & c \

\end{bmatrix}$$

\end{document}

How to Create Curly Braced Matrix

You can also use the Bmatrix environment to create a curly braced matrix. Here is an example code:

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath}

\begin{document}

$$\begin{Bmatrix}

3 & 0 & 0 \\

-1 & 0 & 3 \\

a & b & c \

\end{Bmatrix}$$

\end{document}

How to Create Single Pipes Matrix

To create a matrix with single pipes as the delimiters, use the vmatrix environment. Here is an example code for that:

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath}

\begin{document}

$$\begin{vmatrix}

3 & 0 & 0 \\

-1 & 0 & 3 \\

a & b & c \

\end{vmatrix}$$

\end{document}

How to Create Double Pipes Matrix

To use double pipes uses the Vmatrix environment. Example:

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath}

\begin{document}

$$\begin{Vmatrix}

3 & 0 & 0 \\

-1 & 0 & 3 \\

a & b & c \

\end{Vmatrix}$$

\end{document}

How to Create Custom Delimiters

You can also use LaTex delimiters to create a custom matrix. For example, to use angles as the delimiters,

\langle for left angle and \rangle for the right angle.

An example code is illustrated below:

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath}

\begin{document}

\left\langle

\begin{matrix}

3 & 0 & 0 \\

-1 & 0 & 3 \\

a & b & c

\end{matrix}

\right\rangle

\end{document}

Once you compile the code, you should get a matrix in the form:

Working With Inline Matrices

If you try to include an inline matrix using the regular matrix environments, you will notice that it does not render correctly.

To resolve this, you can use a small matrix.

For example:

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath}

\begin{document}

Maths is awesome with small matrices $\big(\begin{smallmatrix} a & b\\ c & d \end{smallmatrix}\big)$ as that one.

\end{document}

Once compiled, it should fit in line with other content as:

Assigning Matrices

Suppose you want to assign a value to a matrix. To do this, you can use the equation environment as shown:

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath}

\begin{document}

\begin{equation*}

y = \begin{bmatrix}

3 & 0 & 0 \\

-1 & 0 & 3 \\

a1 & b_{{22}} & c4

\end{bmatrix}

\end{equation*}

\end{document}

Once you compile the code above, you should get an equation matrix as shown.

Conclusion

Matrices are very useful in technical documentation. Hence, having a simple way to create them in LaTex can help save time and create easy-to-understand documentation.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list