26

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.

2
  • 3
    Using array to typeset matrices is of course possible, but introduces gratuitous whitespace when enclosed in parentheses. Therefore it is preferred to use the designated matrix environment and its descendants pmatrix (parentheses), bmatrix (brackets), vmatrix (vertical lines), etc. May 23, 2016 at 13:31
  • This is a great question. I have never thought about "centering" the elements of a matrix this way.
    – Matsmath
    May 23, 2016 at 15:42

3 Answers 3

37

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

4
  • Very much nicer than mine, +1
    – Au101
    May 23, 2016 at 13:30
  • Can you make this work with two-digit numbers? Let's say that the bottom right 2 is replaced by 12. Then I would still align the digits 2, and on the left there would be a digit 1, while on the right there would be an i.
    – Matsmath
    May 23, 2016 at 15:46
  • @Matsmath Better use dcolumn to align at the i then. See updated answer for that. May 23, 2016 at 16:25
  • 1
    Please explain your downvote. Where do you find my answer to be lacking? Sep 29, 2018 at 23:10
7

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

6
  • Is it possible to set your \minuslength to the negative width of a minus sign?
    – A Feldman
    May 23, 2016 at 13:17
  • @AFeldman I don't know, but it would be trivial to do \newlength{\minuslength} \newlength{\negminuslength} \settowidth{\minuslength}{$-$} \setlength{\negminuslength}{-\minuslength}. This is quite obviously a humble solution :P
    – Au101
    May 23, 2016 at 13:22
  • 3
    Why so complicated with measuring the length and stuff instead of using \phantom{-}? May 23, 2016 at 13:24
  • @HenriMenke I tried it with \phantom{-} initially, or I thought I did. What I did was accidentally use \vphantom{-} which obviously didn't work, so I tried a very naive approach ... \phantom{-} works well :)
    – Au101
    May 23, 2016 at 13:28
  • To achieve the same spacing between matrix and parentheses for array as for pmatrix use this: \left(\hskip-\arraycolsep\begin{array}...\end{array}\hskip-\arraycolsep\right) May 24, 2016 at 19:18
5

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.

5
  • 1
    Please never use a hyphen instead of a minus sign! May 23, 2016 at 13:26
  • How do I do that.
    – A Feldman
    May 23, 2016 at 13:31
  • 1
    \llap{$-$}. This is still not a good idea, because the spacing between columns is too tight then. May 23, 2016 at 13:32
  • 1
    Your answer serves as a counter example to a MWE, because you load the completely unnecessary packages parskip, amssymb, and amsmath (yes, you don't even need amsmath, because array is a LaTeX builtin as well as eqnarray, which you really shouldn't use). The \pagestyle{plain} has no effect, because it is the default. The option 12pt could be discussed, but is not needed as well. I realize that the geometry package was loaded for cropping, but such details should be hidden from a MWE. May 23, 2016 at 13:39
  • One more thing: In your revised answer you have \llap{{$-$}1}2}i which includes many levels of grouping. You can make it more readable by only having \llap{$-$1}2i. (Also there is one closing brace too many after the 2) May 23, 2016 at 18:52

You must log in to answer this question.

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