8

I'm using pmatrix from amsmath package and would like the output to align all entries in the same column to be align on the right hand side, as shown here in the code:

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

by default, pmatrix will center.

Any way to do this?

1
  • 1
    from the amsmath users guide: "(If you need left or right alignment in a column or other special formats you must resort to array.)" (p.8) Feb 13, 2014 at 14:20

2 Answers 2

18

You can use the pmatrix* environment of the mathtools package:

\documentclass{article}
\usepackage{mathtools} % loads amsmath and some very useful complements
\begin{document}
\[
    \begin{pmatrix*}[r]
        -1 & 1 & -2\\
        0 & -1 & 4\\
        0 & 0 & 1
    \end{pmatrix*}
\]
\end{document}

enter image description here

1

The new tabstackengine package can also do this, including different column alignments, as in the 2nd example. EDITED to reflect updated package (bug workaround no longer needed).

\documentclass{article}
\usepackage{tabstackengine}
\stackMath
\begin{document}
\[ \setstacktabbedgap{1ex}\parenMatrixstack[r]{%
  -1 &  1 & {-}2\\
   0 & -1 &  4\\
   0 &  0 &  1
} \]
\[ \left(\tabularCenterstack{lcr}{%
  -1 &  1 & {-}2\\
   0 & -1 &  4\\
   0 &  0 &  1
}\right) \]
\end{document}

enter image description here

Not the answer you're looking for? Browse other questions tagged .