Download for Win Download for macOS

Use MonsterWriter!

The most enjoyable app for writing a thesis or paper.

How to Write a Matrix in Latex

Table of Contents

  1. How to Write a Matrix in Latex
    1. 1. Denoting a Matrix with Dots (cdots, ddots, vdots)
    2. 2. Matrices With the Array Command
    3. 3. A Matrix with Sections

You can write a matrix in LaTeX as inline equation (in the middle of a floating text) or as block equation (as a new section). Listing [1] shows the LaTeX code of both variations. The resulting PDF is shown in figure [1]. Pay attention to the second line of the example, it includes the amsmath package which is required to use the matrix commands. The pmatrix key word is just one possible matrix form. See table [1] for all supported matrices styles. To use a different style you just have to replace pmatrix in the \begin{pmatrix} and \end{pmatrix} command.

For inline matrices you might also consider using the smallmatrix command (see listing [2] for a complete example). Which behaves the same just makes the matrix smaller.


Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt ut labore et dolore
magna aliquyam erat, \(\begin{pmatrix} 1a & 1b \\ 2a & 2b\end{pmatrix} \) sed
diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.

    1a & 1b \\
    2a & 2b

Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt ut labore et dolore.

Listing 1 - A LaTeX example of a matrix in inline mode as well as block mode.
Figure 1 - The resulting PDF of listing [1] containing one matrix as an inline equation and one matrix within a block equation.
a & b \\
c & d
Listing 2 - The smallmatrix command is suited for inline equations.
LaTeX Code Result
\begin{matrix} 1a & 1b \\ 2a & 2b\end{matrix}  
\begin{pmatrix} 1a & 1b \\ 2a & 2b\end{pmatrix}  
\begin{bmatrix} 1a & 1b \\ 2a & 2b\end{bmatrix}  
\begin{Bmatrix} 1a & 1b \\ 2a & 2b\end{Bmatrix}  
\begin{vmatrix} 1a & 1b \\ 2a & 2b\end{vmatrix}  
\begin{Vmatrix} 1a & 1b \\ 2a & 2b\end{Vmatrix}  
Table 1 - Different styles of matrices available in LaTeX (via amsmath package).

Denoting a Matrix with Dots (cdots, ddots, vdots)

Often you have to describe large matrices or matrices with an undefined end. For this purpose you can use the LaTeX commands \cdots\vdots and \ddots. See equation [eq-1], [eq-2] and the [3] for an example. In table [2] you see a description of the LaTeX dot commands.

Equation 1 - A Matrix with dots indicating that some values have been skipped because there are no many values to explicitly show them all within the matrix. See listing [3] to see the LaTeX code which renders this matrix.
M = \begin{pmatrix}
    x_{11} & \cdots  & x_{1j} \\
    \vdots & \ddots & \vdots \\
    x_{i1} & \cdots  & x_{ij}
Listing 3 - The LaTeX code for the matrix shown in equation [eq-1].
Equation 2 - A matrix with empty fields.
LaTeX Command Type
\cdots Horizontal dots above the line
\ldots Horizontal dots on the line
\vdots Vertical dots
\ddots Diagonal dots
Table 2 - Different types of dots which can be used within a matrix or outside a matrix.

Matrices With the Array Command

As an alternative to the matrix command you can also use the array command to achieve similar results. If you want to have parentheses around the array you can wrap the array between \left[ and \right] for square brackets or \left( and \right) for normal brackets. Listing [4] shows and example with square brackets.

\left[ \begin{array}{rrr}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
Listing 4 - Writing a matrix with the array command.

Using the array command gives you the possibility to control the spacing between the matrix components/cells. This is handy if you have more complicated terms in your matrix (e.g. fractions). Increasing the spacing makes it look better and the symbols of the matrix cells do not overlap. Listing [5] illustrates a working example. Pay attention to the begingroup and endgroup command. If you do not specify them it will change the strech for the all arrays in the document which are placed after this command.

\left[ \begin{array}{rrr}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
Listing 5 - Increasing the space between the matrix cells to make sure symbols do not overlap and have enough space.

A Matrix with Sections

If you use the matrix command which does not render parentheses around the matrix, you can combine two matrices and have a vertical dividing line between them (via \left| and \right. command). You can then wrap the whole construct with \left( and \right) again to have parentheses around the combined matrix.

  a_1 & b_1 \\
  a_2 & b_2 \\
  a_3 & b_3
    c_1 \\
    c_2 \\
Listing 6 - Dividing a matrix into a left and a right section by combining two matrices. The resulting matrix is shown in figure [eq-3].
Equation 3 - A matrix with a left and a right part