Q:

Alignment of entries in matrix

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}?

A:

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}

A:

This uses array environment

\documentclass[a4paper]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{amsmath}

\begin{document}

\begin{equation}
\begin{pmatrix}
\begin{array}{rrrr}
\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{array}
\end{pmatrix}
\end{equation}

\end{document}

A:

One way would be to use a \makebox to ensure that each column was set in the same size box and set the alignment to be right aligned:

Notes:

Using a simple hphantom{} to insert sufficient space to right align all the entries mostly works, except that it is difficult to get the very last entry correct and still have the unary negative spacing as opposed to a binary minus spacing.
I left the constants with centered spacing -- this could be adjusted as well if desired.

The second problem (mentioned in the comments) about centering the 0 within the parenthesis requires a bit more crafting in terms of where \phantom spaces are introduced and how large the \makebox is made:

Hopefully you can study the code below and be able to achieve any alignments you desire.  I don't believe that there is a generic solution that works in all cases, each case needs to be conditioned on a case by case basis.
Notes:

I added some vertical spacing of below the first row via \\[0.5ex].

Code:
\documentclass{article}
\usepackage{amsmath}
\usepackage{calc}

\newcommand*{\MakeBox}[2]{\makebox[\widthof{#1}][r]{#2}}%

\newcommand{\ColumnA}[1]{\MakeBox{$d + c - \frac{1}{b}$}{$#1$}}%
\newcommand{\ColumnD}[1]{\MakeBox{$1 - \frac{1}{b}$}{$#1$}}%

\newcommand{\PhantomFrac}{\hphantom{\frac{1}{2}}}%
\newcommand{\PhantomRe}{\hphantom{\Re\text{e}}}%
\newcommand{\PhantomIm}{\hphantom{\Im\text{m}}}%
\newcommand{\CenterInAPlusB}[1]{\PhantomFrac\makebox[\widthof{$(a+b)$}][c]{$#1$}}%
\newcommand{\CenterInATimesB}[2]{#1\makebox[\widthof{$(ab)$}][c]{$#2$}}%

\begin{document}

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

\begin{equation}
\begin{pmatrix} 
\frac{1}{2}(a+b)   & \frac{1}{2}(a-b)   & \CenterInATimesB{\PhantomRe}{0} & \CenterInATimesB{\PhantomIm}{0} \\[0.5ex] 
\frac{1}{2}(a-b)   & \frac{1}{2}(a+b)   & \CenterInATimesB{\PhantomRe}{0} & \CenterInATimesB{\PhantomIm}{0} & 0 & \Im\text{m}(a b) & \Re\text{e}(a b)\\ 
\CenterInAPlusB{0} & \CenterInAPlusB{0} & \Re\text{e}(a b)                & \Im\text{m}(a b) 
\end{pmatrix}
\end{equation}

\end{document} 

