12

I am having trouble centering the elements of a matrix such that the negative sign is not taken into account during alignment. Additionally, I would also like the space between the left parenthesis and the left edge of the leftmost column to equal to space between the right parenthesis and the right edge of the rightmost column. How would I typeset a matrix such that these visual constraints are satisfied?

Here is a link to a document that describes the problem. The source follows below:

\documentclass[11pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}

\noindent This document provides some examples of matrices for which I cannot
get the elements to line up as I would like.

\bigskip

\noindent\textbf{Example.} I would like the numbers in the matrix $A$ to be
centered, but I do not want the negative sign to cause numbers to be shifted. In
other words, I am looking for something along the lines of a ``center, but
ignore the negative sign'' environment. For example, consider the matrix
\[
    A = \begin{pmatrix*}
    -1 & -10 & 1 \\
    100 & 5 & 16 \\
    13 & 7 & 7
    \end{pmatrix*}.
\]
One common suggestion I have read advises to instead use the array environment,
along with the right-align option. However, there are still problems. Consider
the matrices
\[
    A = \left(\begin{array}{rrr}
    -1 & -2 & -3 \\
    4 & 5 & 6 \\
    -7 & -8 & -9
    \end{array}\right), \quad\text{and}\quad
    B = \left(\begin{array}{rrr}
    -1 & -10 & 1 \\
    100 & 5 & 16 \\
    13 & 7 & 7
    \end{array}\right).
\]
Since the elements of $A$ are all single digits, the alignment is satisfactory. Using the right-alignment hack does not do much good for
matrix $B$, where the elements have different digit lengths.

\bigskip

\noindent How can I center the elements of a matrix so that the negative sign
does not have any influence on alignment, and so that the space between the left
parenthesis and left edge the leftmost column of numbers equals the space
between the right parenthesis and the right edge of the rightmost column of
numbers?

\end{document}

Thanks for your help!

2
  • 1
    Can you point to some resource showing that this alignment is used?
    – egreg
    Oct 6, 2012 at 16:31
  • @egreg Are you referring to the type of the alignment that I would like to achieve? I am actually not aware of any examples that use this alignment, but I will post references if I come across any. The way my matrices are being aligned by default is not very satisfying. Oct 6, 2012 at 18:52

1 Answer 1

11

I think, the problem is rather that the minus sign has a large width, larger than the digits. Because of this the minus sign becomes more weight in centered alignments.

The following example only uses the digit width of the minus sign for the aligment and reduces the total width of the minus sign to 110% of the digit width. Usually there is enough white space at the left of the number thus that the protruding does not hurt to much.

\documentclass[11pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{mathtools}

\usepackage{graphicx}
\newcommand*{\matminus}{%
  \leavevmode
  \hphantom{0}%
  \llap{%
    \settowidth{\dimen0 }{$0$}%
    \resizebox{1.1\dimen0 }{\height}{$-$}%
  }%
}

\begin{document}

\[
    A = \begin{pmatrix*}
    \matminus1 & \matminus10 & 1 \\
    100 & 5 & 16 \\
    13 & 7 & 7
    \end{pmatrix*}.
\]

\end{document}

Result

The suggestion above is a compromise between different requirements. Of course, also the numbers can also be centered without taking into account the minus sign without changing the column margins. But then the cell entries are not independent any more, the typesetting depends on the values in the other rows:

  • \llap{$-$} sets the minus sign and lets TeX think, the width is zero. That matches the centering requirement. But depending on the other values in the other rows, additional white space needs to be inserted at the left of the column.
  • \hphantom{-} does not set a minus sign, but only the horizontal space. It can help for the proper alignment of positive and negative numbers, but there might be the need to reduce the white space at the left side of the column depending on the other values in the column.
2
  • Brilliant -- this looks very pleasing. You are right in that the problem is caused by the minus sign being too wide. Thank you very much! Oct 6, 2012 at 18:56
  • 1
    On a somewhat pedantic note, the "ends" of the minus signs are not as rounded anymore (elliptic and half-discs). Oh, and the side bearing become smaller, but this might be desirable. Apr 8, 2013 at 14:16

You must log in to answer this question.

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