[Tex/LaTex] Aligning the minus sign in a matrix

matrices

I would like to be able to align the minus sign in a matrix so that the minus is to the left of the numerical column. I have written my matrix as the following:

\begin{eqnarray}
  \left(
    \begin{array}{llll}
      1 & 0   & 0  & 0 \\
      0 & 2   & 2i & 0 \\
      0 & -2i & 2  & 0 \\
      0 & 0   & 0  & 1
    \end{array}
  \right),
  \label{eq:rhot}
\end{eqnarray}

An extracted form of that is as:

Enter image description here

But the desired shape should be as a format in which the [3,2] element (-2i) must be written as though: 2 below 2 of the [2,2] element, and the minus sign be at the left of the second column as:

Enter image description here

Albeit I made it by paint.

Best Answer

Using the mathtools package you can make use of enhanced versions of the matrix environments. In this example I use the starred version of pmatrix which accepts an optional argument which is the alignment of the cells. To reserve the space for the minus sign, I simply put \phantom{-} before the entries to be spaced out.

\documentclass{article}
\usepackage{mathtools} % loads amsmath
\begin{document}
\begin{equation*}
  \begin{pmatrix*}[l]
    1 & \phantom{-}0 & 0 & 0 \\
    0 & \phantom{-}2 & 2i & 0 \\
    0 & -2i & 2 & 0 \\
    0 & \phantom{-}0 & 0 & 1
  \end{pmatrix*}
\end{equation*}
\end{document}

enter image description here

To align at the i, just apply the above trick in reverse, i.e. add \phantom{i} where needed.

\documentclass{article}
\usepackage{mathtools} % loads amsmath
\usepackage{dcolumn}
\begin{document}
\begin{equation*}
  \begin{pmatrix*}[r]
    1 & 0\phantom{i} & 0\phantom{i} & 0 \\
    0 & 2\phantom{i} & 2i & 0 \\
    0 & -12i & 2\phantom{i} & 0 \\
    0 & 0\phantom{i} & 0\phantom{i} & 1
  \end{pmatrix*}
\end{equation*}
\end{document}

enter image description here

Using David's dcolumn package you can also achieve alignment at the i. This has the disadvantage, that it reserves space for the i even in columns where there is no i (like the first and last columns in the example).

\documentclass{article}
\usepackage{mathtools} % loads amsmath
\usepackage{dcolumn}
\newcolumntype{d}{D{i}{i}{0}}
\begin{document}
\begin{equation*}
  \begin{pmatrix*}[d]
    1 & 0 & 0 & 0 \\
    0 & 2 & 2i & 0 \\
    0 & -12i & 2 & 0 \\
    0 & 0 & 0 & 1
  \end{pmatrix*}
\end{equation*}
\end{document}

enter image description here