Windows, MacOS

## Use MonsterWriter!

The most enjoyable app for writing a thesis or paper.

# How to Write a Matrix in Latex

1. How to Write a Matrix in Latex

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  shows the LaTeX code of both variations. The resulting PDF is shown in figure . 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  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  for a complete example). Which behaves the same just makes the matrix smaller. Listing 1 - A LaTeX example of a matrix in inline mode as well as block mode.
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  for an example. In table  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  to see the LaTeX code which renders this matrix. 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  shows and example with square brackets.

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  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.

## 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. Equation 3 - A matrix with a left and a right part