Open In App

matrices in LaTeX

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Matrices are an essential tool in mathematics and science for organizing and manipulating data. LaTeX provides a powerful set of tools for creating matrices that can be customized to fit your specific needs. In this article, we will explore how to create matrices in LaTeX and some of the formatting options available.

Create a Matrix in LaTeX

To create a matrix in LaTeX, we use the matrix environment. The matrix environment creates a square or rectangular matrix with no brackets or delimiters. Here is an example of a 2 x 2 matrix using the matrix environment:

\begin{matrix}   % Start of the matrix environment
 1 & 2 \\        % First row of the matrix, separated by ampersands (&)
 3 & 4           % Second row of the matrix
\end{matrix}     % End of the matrix environment

Output:

*** QuickLaTeX cannot compile formula:
\begin{matrix}   % Start of the matrix environment 1 & 2 \\        % First row of the matrix, separated by ampersands (&) 3 & 4           % Second row of the matrix \end{matrix}     % End of the matrix environment

*** Error message:
\begin{matrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Improper \prevdepth.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \cr inserted.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
You can't use `\end' in internal vertical mode.
leading text: \end{document}
\begin{matrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Emergency stop.

Spacing in a Matrix

By default, the matrix environment separates columns with a small amount of space. However, you can adjust the spacing between columns by specifying the column separation with the \setlength command. For example, to set the column separation to 1cm, we would use the following code:

\setlength{\arraycolsep}{1cm}

Example: 

 \[
\setlength{\arraycolsep}{1cm}
\begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{bmatrix}
\]

This will create a matrix environment where the space between the columns is set to 1cm, resulting in a wider spacing between the elements of the matrix.

Output:

 

The code \setlength{\arraycolsep}{1cm} changes the space between columns in arrays and matrices to 1cm. However, it does not produce any visible output on its own.

LateX pmatrix, bmatrix, vmatrix, Vmatrix

The matrix environment provides no brackets or delimiters around the matrix. However, LaTeX provides various environments for creating matrices with different types of brackets or delimiters. Here are some examples:

1. pmatrix

This creates a matrix with parentheses.

Example: 

\begin{pmatrix}  % Start of the pmatrix environment
 1 & 2 \\         % First row of the matrix, separated by ampersands (&)
 3 & 4            % Second row of the matrix
\end{pmatrix}      % End of the pmatrix environment

Output:

*** QuickLaTeX cannot compile formula:
\begin{pmatrix}  % Start of the pmatrix environment 1 & 2 \\         % First row of the matrix, separated by ampersands (&) 3 & 4            % Second row of the matrix \end{pmatrix}      % End of the pmatrix environment

*** Error message:
\begin{pmatrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Improper \prevdepth.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \cr inserted.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
You can't use `\end' in internal vertical mode.
leading text: \end{document}
\begin{pmatrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \right. inserted.
leading text: \end{document}
Emergency stop.

2. bmatrix

This creates a matrix with brackets.

Example: 

\begin{bmatrix}   % Start of the bmatrix environment
 1 & 2 \\          % First row of the matrix, separated by ampersands (&)
 3 & 4             % Second row of the matrix
\end{bmatrix}      % End of the bmatrix environment

Output: 

*** QuickLaTeX cannot compile formula:
\begin{bmatrix}   % Start of the bmatrix environment 1 & 2 \\          % First row of the matrix, separated by ampersands (&) 3 & 4             % Second row of the matrix \end{bmatrix}      % End of the bmatrix environment

*** Error message:
\begin{bmatrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Improper \prevdepth.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \cr inserted.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
You can't use `\end' in internal vertical mode.
leading text: \end{document}
\begin{bmatrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \right. inserted.
leading text: \end{document}
Emergency stop.

3. vmatrix

This creates a matrix with vertical bars.

Example: 

\begin{vmatrix}   % Start of the vmatrix environment
 1 & 2 \\          % First row of the matrix, separated by ampersands (&)
 3 & 4             % Second row of the matrix
\end{vmatrix}      % End of the vmatrix environment

Output: 

*** QuickLaTeX cannot compile formula:
\begin{vmatrix}   % Start of the vmatrix environment 1 & 2 \\          % First row of the matrix, separated by ampersands (&) 3 & 4             % Second row of the matrix \end{vmatrix}      % End of the vmatrix environment

*** Error message:
\begin{vmatrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Improper \prevdepth.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \cr inserted.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
You can't use `\end' in internal vertical mode.
leading text: \end{document}
\begin{vmatrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \right. inserted.
leading text: \end{document}
Emergency stop.

4. Vmatrix

This creates a matrix with double vertical bars.

Example:  

\begin{Vmatrix}   % Start of the Vmatrix environment
 1 & 2 \\          % First row of the matrix, separated by ampersands (&)
 3 & 4             % Second row of the matrix
\end{Vmatrix}      % End of the Vmatrix environment

Output: 

*** QuickLaTeX cannot compile formula:
\begin{Vmatrix}   % Start of the Vmatrix environment 1 & 2 \\          % First row of the matrix, separated by ampersands (&) 3 & 4             % Second row of the matrix \end{Vmatrix}      % End of the Vmatrix environment

*** Error message:
\begin{Vmatrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Improper \prevdepth.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \cr inserted.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
You can't use `\end' in internal vertical mode.
leading text: \end{document}
\begin{Vmatrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \right. inserted.
leading text: \end{document}
Emergency stop.

Write an m x n Matrix in LaTeX

To create an m x n matrix in LaTeX, we use the matrix environment with m rows and n columns. 

Example 1: 

Below is an example of a 3 x 2 matrix.

\begin{matrix}     % Start of the matrix environment
 1 & 2 \\            % First row of the matrix, separated by ampersands (&)
 3 & 4 \\            % Second row of the matrix
 5 & 6              % Third row of the matrix
\end{matrix}        % End of the matrix environment

Output: 

*** QuickLaTeX cannot compile formula:
\begin{matrix}     % Start of the matrix environment 1 & 2 \\            % First row of the matrix, separated by ampersands (&) 3 & 4 \\            % Second row of the matrix 5 & 6              % Third row of the matrix \end{matrix}        % End of the matrix environment

*** Error message:
\begin{matrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Improper \prevdepth.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \cr inserted.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
You can't use `\end' in internal vertical mode.
leading text: \end{document}
\begin{matrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Emergency stop.

Example 2: 

Below is an example of a 3 x 3 matrix with brackets.

\begin{bmatrix}     % Start of the bmatrix environment
 1 & 2 & 3 \\            % First row of the matrix, separated by ampersands (&)
 4 & 5 & 6 \\            % Second row of the matrix
 7 & 8 & 9              % Third row of the matrix
\end{bmatrix}        % End of the bmatrix environment

Output: 

*** QuickLaTeX cannot compile formula:
\begin{bmatrix}     % Start of the bmatrix environment 1 & 2 & 3 \\            % First row of the matrix, separated by ampersands (&) 4 & 5 & 6 \\            % Second row of the matrix 7 & 8 & 9              % Third row of the matrix \end{bmatrix}        % End of the bmatrix environment

*** Error message:
\begin{bmatrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Improper \prevdepth.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \cr inserted.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
You can't use `\end' in internal vertical mode.
leading text: \end{document}
\begin{bmatrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \right. inserted.
leading text: \end{document}
Emergency stop.

LateX Matrix with No Bracket

If you want to create a matrix with no brackets, you can use the smallmatrix environment. 

Example: 

Below is an example of a 2 x 2 matrix with no brackets.

$\begin{smallmatrix}     % Start of the smallmatrix environment within inline math mode
 1 & 2 \\                  % First row of the matrix, separated by ampersands (&)
 3 & 4                     % Second row of the matrix
\end{smallmatrix}$        % End of the smallmatrix environment within inline math mode

Output: 

 

Note:  The small matrix is displayed within the text and is smaller in size than a regular matrix. The elements are aligned to fit within the available space without disrupting the text flow.

Latex Matrix Determinant / Vertical Bar Bracket

To create a matrix with vertical bars around it (used for calculating determinants), you can use the vmatrix environment. 

Example:

\begin{vmatrix}     % Start of the vmatrix environment
 1 & 2 \\            % First row of the matrix, separated by ampersands (&)
 3 & 4              % Second row of the matrix
\end{vmatrix}        % End of the vmatrix environment

Output:

*** QuickLaTeX cannot compile formula:
\begin{vmatrix}     % Start of the vmatrix environment 1 & 2 \\            % First row of the matrix, separated by ampersands (&) 3 & 4              % Second row of the matrix \end{vmatrix}        % End of the vmatrix environment

*** Error message:
\begin{vmatrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Improper \prevdepth.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \cr inserted.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
You can't use `\end' in internal vertical mode.
leading text: \end{document}
\begin{vmatrix} on input line 8 ended by \end{document}.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \right. inserted.
leading text: \end{document}
Emergency stop.

Explanation:

The value of the determinant can be calculated using the formula:

| a  b |
| c  d | = ad - bc

where a, b, c, and d are the elements of the 2×2 matrix. In this case, the determinant is:

| 1  2 |
| 3  4 | = (1 * 4) - (2 * 3) = -2

Result:

|-2|

Note: 

The determinant is enclosed in vertical bars and the output is aligned to fit within the available space.

Latex Matrix with Curly Brackets

To create a matrix with curly brackets around it, you can use the cases environment. 

Example: 

\begin{cases}                % Start of the cases environment
 1 & \text{if } i=j \\        % First case, returns 1 if i equals j
 0 & \text{otherwise}        % Second case, returns 0 otherwise
\end{cases}                  % End of the cases environment

The cases environment is used to define a piecewise function that returns different values depending on the input. In this case, the function returns 1 if i equals j, and 0 otherwise. The \text command is used to display text within the function definition.

Output:

*** QuickLaTeX cannot compile formula:
\begin{cases}                % Start of the cases environment 1 & \text{if } i=j \\        % First case, returns 1 if i equals j 0 & \text{otherwise}        % Second case, returns 0 otherwise \end{cases}                  % End of the cases environment

*** Error message:
\begin{cases} on input line 8 ended by \end{document}.
leading text: \end{document}
Improper \prevdepth.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \cr inserted.
leading text: \end{document}
Missing $ inserted.
leading text: \end{document}
You can't use `\end' in internal vertical mode.
leading text: \end{document}
\begin{cases} on input line 8 ended by \end{document}.
leading text: \end{document}
Missing } inserted.
leading text: \end{document}
Missing \right. inserted.
leading text: \end{document}
Emergency stop.

Note:

The curly braces {} are automatically generated by the cases environment to 
denote the function definition. The text is aligned to fit within the available space.

Example: 

If ‘i’ equals ‘j’, the function will return 1:

{i = j: } \begin{cases} 1 & \text{if } i=j \\ 0 & \text{otherwise} \end{cases} = 1
If i does not equal j, the function will return 0:

If i does not equal j, the function will return 0:

{i ≠ j: } \begin{cases} 1 & \text{if } i=j \\ 0 & \text{otherwise} \end{cases} = 0

Latex Matrix with Double Vertical Bar Brackets

To create a matrix with double vertical bars around it, you can use the ‘matrix*’ environment provided by the mathtools package. 

Example: 

Here is an example of a 2 x 2 matrix with double vertical bars:

\begin{matrix*}[r]      % Start of the matrix* environment, with right-aligned columns
 1 & 2 \\              % First row of the matrix
 3 & 4                 % Second row of the matrix
\end{matrix*}           % End of the matrix* environment

The ‘[r]’ option inside the ‘matrix*’ environment specifies that the columns should be right-aligned. Other options include ‘[l]’ for left-aligned columns, and ‘‘ for centered columns.

Output:

The output of this code will be a 2×2 matrix with the values ‘1’,’ 2′,’ 3′, and ‘4’ aligned to the right:

 


Note that the brackets ‘⎡’,’⎣’, ‘⎤’, and ‘⎦’ are automatically generated by the matrix* environment to denote the matrix. The values are separated by spaces, and each row is separated by ‘\\’.

This type of matrix alignment is often used in mathematics to align decimal points or other numerical values in tables.

Latex Small Inline Matrix

If you want to create a small inline matrix, you can use the smallmatrix environment within the math mode. Here is an example of a 2 x 2 matrix within a sentence:

The matrix $\begin{smallmatrix}
 1 & 2 \\
 3 & 4
\end{smallmatrix}$ is invertible.

Output: 

 

In this example, the statement about the matrix is typeset in regular text, but the matrix itself is typeset using the ‘smallmatrix’ environment. The ‘smallmatrix’ environment is similar to the matrix environment but produces a smaller matrix that is suitable for use inline with text. The matrix is enclosed in dollar signs ‘$’ to indicate that it should be typeset inline with the text.

Amsmath Package

The amsmath package provides additional tools for working with matrices in LaTeX. One such tool is the bsmallmatrix environment, which creates a matrix with small brackets. 

Example: 

Here is an example of a 2 x 2 matrix with small brackets.

\begin{bsmallmatrix}
 a & b \\
 c & d
\end{bsmallmatrix}

Output:

 

In this example, the ‘bsmallmatrix’ environment is used instead of ‘bmatrix’, which produces a smaller matrix suitable for use inline with text. The matrix entries are separated by ‘&’ and the rows are separated by ‘\\’. The matrix is enclosed in double dollar signs ‘$$’ to display it in a centered equation format.

Matrix with Text

You can add text within a matrix by using the \text command. 

Example: 

Here is an example of a matrix with text.

\begin{bmatrix}
 1 & \text{two} \\
 \text{three} & 4
\end{bmatrix}

Output: 

\begin{bmatrix} 1 & \text{two} \\ \text{three} & 4 \end{bmatrix}

In this example, the matrix entries contain text, which is typeset using the ‘\text{}’ command. The matrix entries are separated by ‘&’ and the rows are separated by ‘\\’. The matrix is enclosed in double dollar signs ‘$$’ to display it in a centered equation format.

Border Matrix

You can add a border around a matrix by using the \bordermatrix command. 

Example: 

Here is an example of a 2 x 2 matrix with a border.

\bordermatrix{ & x & y \cr
              A & 1 & 2 \cr
              B & 3 & 4 \cr}

Output:

 

In this example, the first row and column of the matrix are labeled with the letters “x” and “y”, and the letters “A” and “B”, respectively. The matrix entries are separated by ‘&’ and the rows are separated by ‘\\’. The ‘\cr’ command is used to end each row.

Fraction Matrix

You can create a matrix with fractions by using the \dfrac command within the matrix environment. Here is an example of a 2 x 2 matrix with fractions:

Example: 

\begin{bmatrix}
\dfrac{1}{2} & \dfrac{3}{4} \\
\dfrac{5}{6} & \dfrac{7}{8}
\end{bmatrix}

Output:

\begin{bmatrix} \dfrac{1}{2} & \dfrac{3}{4} \\ \dfrac{5}{6} & \dfrac{7}{8} \end{bmatrix}

In this example, the fractions in the matrix are typeset using the ‘\dfrac{}{}’ command. The matrix entries are separated by ‘&’ and the rows are separated by ‘\\’. The matrix is enclosed in double dollar signs ‘$$’ to display it in a centered equation format.

Arrays

Arrays provide a way to create matrices with more customization options than the matrix environment. 

Example: 

Here is an example of a 2 x 2 matrix created using an array.

\[
\left[
\begin{array}{cc}
a & b \\
c & d
\end{array}
\right]
\]

Output: 

 

In this example, the array environment is used with two columns and two rows specified by the argument ‘{cc}’. The entries in the matrix are the variables ‘a’,’ b’, ‘c’, and ‘d’. The matrix is enclosed in the ‘\left[‘ and ‘\right]’ commands to automatically adjust the size of the brackets to fit the matrix. Finally, the ‘\[‘ and ‘\]’ delimiters are used to center the matrix and remove the equation number.

Conclusion

Matrices are a fundamental tool in mathematics and science, and LaTeX provides a powerful set of tools for creating and customizing matrices. Whether you need to create a simple matrix or a complex array, LaTeX has the tools you need to get the job done. With the information provided in this article, you should be well-equipped to create and customize matrices in LaTeX to fit your specific needs.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads