[Tex/LaTex] Alignment of entries in matrix

horizontal alignmentmatrices

I have this pmatrix (amsmath package):

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{equation}
\begin{pmatrix}
\frac{a}{b}     & \frac{a}{b}     & 0  & 1-\frac{a}{b} \\ 
0               & 0               & -1 & 0             \\ 
c-\frac{a}{b}   & c-\frac{a}{b}   & 0  & \frac{a}{b}   \\ 
d+c-\frac{a}{b} & d+c-\frac{a}{b} & 0  & -\frac{a}{b}
\end{pmatrix}
\end{equation}
\end{document}

I'd like to align entries of the matrix so all fractions are aligned. Is there a way to to this within the pmatrix environment, or do I have to use \begin{array}?

Best Answer

The mathtools package allows to adjust the alignment of matrix elements:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}
\begin{equation}
\begin{pmatrix*}[r]
\frac{a}{b}     & \frac{a}{b}     & 0  & 1-\frac{a}{b} \\
0               & 0               & -1 & 0             \\
c-\frac{a}{b}   & c-\frac{a}{b}   & 0  & \frac{a}{b}   \\
d+c-\frac{a}{b} & d+c-\frac{a}{b} & 0  & -\frac{a}{b}
\end{pmatrix*}
\end{equation}
\end{document}
Related Question