Aligning the minus sign in a matrix

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


Well, you've set up an array with four columns left-aligned \begin{array}{llll} - that's what your four ls do.

In order to align the 2s, I suggest a hack

\documentclass[12pt]{article}
\pagestyle{plain}
\usepackage[margin=1.8cm]{geometry}
\geometry{a4paper}
\usepackage[parfill]{parskip}
\usepackage{amsmath}
\usepackage{amssymb}

\newlength{\minuslength}
\settowidth{\minuslength}{$-$}

\begin{document}

\begin{equation*}
  \left(
  \begin{array}{llll}
    1 & \hspace{\minuslength}0 & 0 & 0 \\
    0 & \hspace{\minuslength}2 & 2i & 0 \\
    0 & -2i & 2 & 0 \\
    0 & \hspace{\minuslength}0 & 0 & 1
  \end{array}
  \right)
\end{equation*}

\end{document}

enter image description here

What I've done here is set up a length called \minuslength:

\newlength{\minuslength}

I then give this length the width of a minus sign:

\settowidth{\minuslength}{$-$}

I can then insert a space the width of a minus sign \settowidth{\minuslength}{$-$} before each of the elements.

This is a bit inelegant though. There might be nicer ways of doing this, but are you sure centring wouldn't be preferable.

In order for the columns to be centred, you must specify that you want four centred columns \begin{array}{cccc}

\begin{equation*}
  \left(
  \begin{array}{cccc}
    1 & 0 & 0 & 0 \\
    0 & 2 & 2i & 0 \\
    0 & -2i & 2 & 0 \\
    0 & 0 & 0 & 1
  \end{array}
  \right)
\end{equation*}

enter image description here

But in all honesty, it would be easier to just use a pmatrix (p for parentheses, you can use bmatrix for matrices with square brackets ([]) Bmatrix for matrices with braces ({}) and there are others):

\begin{equation*}
  \begin{pmatrix}
    1 & 0 & 0 & 0 \\
    0 & 2 & 2i & 0 \\
    0 & -2i & 2 & 0 \\
    0 & 0 & 0 & 1
  \end{pmatrix}
\end{equation*}

enter image description here


Using \llap As noted in comments, when using \llap{$-$}2i alone, the column spacing is too tight.

However, building on the other answers idea of using a \phantom, instead of using it in every row other than the "minus" row, here it is used once to correct the column spacing by use in the first column, with a single \llap on the row with the minus used to flip the minus to the left:

enter image description here

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

This also works well with 2 digit numbers:

enter image description here

and by manipulating the grouping, eg, \llap{$-$1}2i can align on any digit:

enter image description here

And thanks to Henri Menke for his considerable patience in correcting my initial answer. I copied his use of pmatrix* and implemented the advice he gave in the comments.

Tags:

Matrices