88

I'd like to left align the -1 entry in the following matrix so that the 1s line up. Is there a way to get matrices to ignore signs in that way?

\begin{matrix}
    1 & 1  \\
    1 & -1 \\
\end{matrix}

Using \flushright{-1} doesn't seem to work.

1
  • 3
    mathtools allows you to l/r align entries in matrices. In your example, putting an \hfill before the upper 1 should also work.
    – anon
    Feb 19, 2012 at 5:38

7 Answers 7

128
+100

I know that this is an old question, but to whoever stumbles upon this from a Google search (like I just did) another very elegant solution for this is using the starred matrix environments of mathtools:

  • \begin{matrix*} [〈col〉] 〈contents〉 \end{matrix*}: simple matrix,
  • \begin{pmatrix*}[〈col〉] 〈contents〉 \end{pmatrix*}: matrix surrounded by matching parenthesis,
  • \begin{bmatrix*}[〈col〉] 〈contents〉 \end{bmatrix*}: matrix surrounded by matching square brackets,
  • \begin{Bmatrix*}[〈col〉] 〈contents〉 \end{Bmatrix*}: matrix surrounded by matching curly brackets (braces),
  • \begin{vmatrix*}[〈col〉] 〈contents〉 \end{vmatrix*}: matrix surrounded by matching vertical lines (like for determinant),
  • \begin{Vmatrix*}[〈col〉] 〈contents〉 \end{Vmatrix*}: matrix surrounded by matching double vertical lines.

The <col> optional argument specifies the column alignment, and should be c, l or r for centered (default), left-aligned and right-aligned, respectively. Consider the following example,

\documentclass{article}
\usepackage{mathtools}
\begin{document}

\[
\begin{pmatrix*}[r]
  -1 & 3 \\
  2 & -4
\end{pmatrix*}
\]

\[
\begin{Bmatrix*}[l]
  1.001 &\hdots & 3  \\
  2.3 & \hdots & 4.2
\end{Bmatrix*}
\]

\[
\begin{Vmatrix*}[c]
  1 &  3  \\
  \tfrac{1}{2} & 4.2
\end{Vmatrix*}
\]

\end{document}

which yields

Example

The mathtools package is an extension of amsmath that fixes various bugs/deficiencies and adds some useful tools (like the starred matrix environments).

6
  • 3
    This is definitely the way to do it! Feb 1, 2014 at 0:56
  • Thanks, this seems like the most elegant solution. Updated the accepted answer.
    – Will
    Feb 3, 2014 at 18:03
  • 2
    @Will I put a bounty to award this answer to make it more visible since it is the way to go. And now here you are, making it the accepted one :)
    – yo'
    Feb 4, 2014 at 8:58
  • I just improved the examples and the answer a bit, since there is this new bounty in place.
    – Dimas
    Feb 4, 2014 at 16:57
  • 3
    mathtools has the same *ed variants of the family of smallmatrix environments. Mar 20, 2014 at 19:39
38

A quick way to do this is by adding phantom characters:

\begin{matrix}
    1 & \phantom{-}1  \\
    1 & -1 \\
\end{matrix}

enter image description here

Although for simple arrays like this it's probably simpler just to use a standard array environment

\begin{array}{rr}
    1 & 1  \\
    1 & -1 \\
\end{array}

for the same result. Note that if you need control over alignment, then array is the preferred way to typeset matrices (you can simply wrap in \left( ... \right) etc. for brackets and lines).

3
27

If you using LaTeX2e out of the box, you can use the array environment, which is similar to a tabular:

\documentclass{article}
\begin{document}
\[
\begin{array}{rr}
    1 &  1  \\
    1 & -1 \\
\end{array}
\]
\end{document}
0
1

Simplest solution:

\begin{matrix}
    1 & \hfill 1  \\
    1 & -1 \\
\end{matrix}
1

In the environments of nicematrix, you have an option r for that.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

$\begin{pNiceMatrix}[r]
1 & -1 \\
1 & 1
\end{pNiceMatrix}$

\end{document}

Output of the above code

0

The question of @Dimas is fine. The \begin{pmatrix*}[r], however, does not work well when the numbers have a different number of digits. Let me add this answer, which defines a new column type [C]. It requires the tabularx package. Note: This answer is based on another answer from StackExchange (I could not identify the link).

Here is the code and the output:

\documentclass{article}

\usepackage{mathtools}
\usepackage{tabularx}

\makeatletter
\def\zz\ignorespaces{\@ifnextchar-{}{\phantom{-}}}
\newcolumntype{C}{>{\zz}{c}}
\makeatother

\begin{document}

\begin{equation}
\begin{bmatrix}
1 & 1234\\
-2 & 2
\end{bmatrix}
\end{equation}

\begin{equation}
\begin{bmatrix*}[r]
1 & 1234\\
-2 & 2
\end{bmatrix*}
\end{equation}

\begin{equation}
\begin{bmatrix*}[C]
1 & 1234\\
-2 & 2
\end{bmatrix*}
\end{equation}

\end{document}

Output

0

With the spalign package of the October 5, 2016, using the command \spalignmat option right r it is possible to have easily the alignment of the signs of the matrix's elements. There is also the option c (center) and l (left).

\documentclass[a4paper,12pt]{article}
\usepackage{spalign}
\begin{document}
\[ \spalignmat[r]{-5 -1 +2; -1 -5 +7; +1 -6 -3} \]
\end{document}

enter image description here

You must log in to answer this question.

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