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 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 2for a complete example). Which behaves the same just makes the matrix smaller.
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} |
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 1, 2 and 3 for an example. In table 2 you see a description of the LaTeX dot commands.
LaTeX Command | Type |
---|---|
\cdots | Horizontal dots above the line |
\ldots | Horizontal dots on the line |
\vdots | Vertical dots |
\ddots | Diagonal dots |
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.
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 stretch 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.