Math & ScienceMatrix Alignment of negative Numbers

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
Singularity
Posts: 156
Joined: Sat Jan 22, 2011 9:55 pm

Matrix Alignment of negative Numbers

Post by Singularity »

Is there an easy way to get this matrix to line up so that the numbers are flush right in each column? As it stands now, the negative numbers "stick out" to the right of the positive numbers.

Code: Select all

\begin{equation*}
	C=
	\begin{pmatrix}
    -4      &  1     &  0 & 0 & 0 & \cdots & 1      \\
     1      & -4     &  1 & 0 & 0 & \cdots & 0      \\
     0      &  1     & -4 & 1 & 0 & \cdots & 1      \\
     \vdots &        &    &   &   & \ddots & \vdots \\
     0      & \cdots &    &   & 1 & -4     &  1     \\
     1      & \cdots &    &   & 0 &  1     & -4
	\end{pmatrix}
,\end{equation*}
Thanks.
-S
Last edited by Singularity on Tue Nov 15, 2011 7:27 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Singularity
Posts: 156
Joined: Sat Jan 22, 2011 9:55 pm

Matrix Alignment of negative Numbers

Post by Singularity »

Ah, nevermind. I decided the tabular environment isn't all that hard after all and went with doing it this way

Code: Select all

\documentclass[12pt,fleqn,reqno]{article}
%fleqn: left align equations
%reqno: equation numbers on right
\usepackage{amsmath,amssymb,amsthm}
\usepackage{amsfonts,graphics,epsfig,cite}
\usepackage{array}

\begin{document}
\begin{equation*}
	C=\left(
	\begin{tabular}{>{$}r<{$}>{$}r<{$}>{$}r<{$}>{$}r<{$}>{$}r<{$}>{$}r<{$}>{$}r<{$}}
    -4      &  1     &  0 & 0 & 0 & \cdots & 1      \\
     1      & -4     &  1 & 0 & 0 & \cdots & 0      \\
     0      &  1     & -4 & 1 & 0 & \cdots & 1      \\
     \vdots &        &    &   &   & \ddots & \vdots \\
     0      & \cdots &    &   & 1 & -4     &  1     \\
     1      & \cdots &    &   & 0 &  1     & -4
	\end{tabular}\right)
,\end{equation*}
\end{document}
I like the easy to use bmatrix, pmatrix, etc. environments, so I wanted an easy solution to use with them. But this is easy enough once you get the hang of it.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Matrix Alignment of negative Numbers

Post by localghost »

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{mathtools}

\begin{document}
  \[
    C=
    \begin{pmatrix*}[r]
      -4     &  1     &  0 & 0 & 0 & \cdots & 1      \\
      1      & -4     &  1 & 0 & 0 & \cdots & 0      \\
      0      &  1     & -4 & 1 & 0 & \cdots & 1      \\
      \vdots &        &    &   &   & \ddots & \vdots \\
      0      & \cdots &    &   & 1 & -4     &  1     \\
      1      & \cdots &    &   & 0 &  1     & -4
    \end{pmatrix*}
  \]

  \[
    C=\left(
    \begin{array}{@{}*{7}{r}@{}}
      -4     &  1     &  0 & 0 & 0 & \cdots & 1      \\
      1      & -4     &  1 & 0 & 0 & \cdots & 0      \\
      0      &  1     & -4 & 1 & 0 & \cdots & 1      \\
      \vdots &        &    &   &   & \ddots & \vdots \\
      0      & \cdots &    &   & 1 & -4     &  1     \\
      1      & \cdots &    &   & 0 &  1     & -4
    \end{array}
    \right)
  \]
\end{document}

Thorsten
Post Reply