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:

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:

<img data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-265.png" height="251" src="data:image/svg xml,” width=”242″>

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:

<img data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-266.png" height="204" src="data:image/svg xml,” width=”261″>

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

}

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-267.png" data-lazy- height="154" src="data:image/svg xml,” width=”326″>

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

}

<img data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-268.png" height="195" src="data:image/svg xml,” width=”221″>

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

}

<img data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-269.png" height="158" src="data:image/svg xml,” width=”289″>

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

}

<img data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-270.png" height="171" src="data:image/svg xml,” width=”211″>

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:

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

<img data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-271.png" height="243" src="data:image/svg xml,” width=”254″>

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:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-272.png" data-lazy- height="64" src="data:image/svg xml,” width=”551″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-273.png" data-lazy- height="165" src="data:image/svg xml,” width=”426″>

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

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/john-150×150.png60f5e5f37fd4a.jpg" height="112" src="data:image/svg xml,” width=”112″>

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