Matrices with alignment

The entries of matrix columns are centered by default. Let’s see an example:

\[
  \begin{pmatrix}
    1 &  2 &  1 \\
    0 & -2 & -3 \\
    0 & 3 &  -2
  \end{pmatrix}
\]

Output:

Matrix with centered columns

For right alignment you could use an array environment instead of pmatrix. I will show a possibility to get left, right or centered alignment by redefining the internal amsmath macro \env@matrix. Its original definition in version 2.13 is:

\def\env@matrix{\hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{*\c@MaxMatrixCols c}}

I introduce an optional parameter, its default value is c:

\makeatletter
\renewcommand*\env@matrix[1][c]{\hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{*\c@MaxMatrixCols #1}}
\makeatother

Now, if you write \begin{pmatrix}[r] … above you will get:

Matrix with right aligned columns

See complete source code example.

This topic was discussed in the Matheplanet Forum.

05. May 2008 by stefan
Categories: Uncategorized | 3 comments

Comments (3)

  1. Thanks for this — I wish that it were in amslatex already!

  2. You can also use the pmatrix* environment provided by the mathtools package like this:
    begin{pmatrix*}[r]…

  3. By a chance I found a small piece of LaTeX code that allows horizontal alignment in matrix-environments of the package “amsmath” (bmatrix, pmatrix, etc.) for each column separately. If you put the following lines in the preambule:

    \usepackage{amsmath}
    \makeatletter
    \renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
    \hskip -\arraycolsep
    \let\@ifnextchar\new@ifnextchar
    \array{#1}}
    \makeatother %%% for getting a separeting vertical line in bmatrix

    then you can write things like this in the LaTeX document:

    $$
    [A|I]=
    \begin{bmatrix}[rrr|rrr]
    1 & 5 & 1 & -1 & 0 & 0\\
    2 & 5 & 0 & 0 & 12 & 0 \\
    2 & 7 & 1 & 0 & 0 & 1 \\
    \end{bmatrix}
    $$

Leave a Reply to Neil Cancel reply

Required fields are marked *