Math & ScienceMatrix with right-aligned columns of equal width

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
luredan
Posts: 2
Joined: Tue May 04, 2010 2:18 pm

Matrix with right-aligned columns of equal width

Post by luredan »

Hi. Given a matrix, I would like the columns to be equally wide, and also the content in each column to be right-aligned. Sounds quite straight-forward at first, but now I really can't find out how to do it, so any help or hints would be very welcome. If I use the array-environment, I can use either "r" for right-aligned or "p{...}" to get the columns equally wide, but not both at the same time.

For tables, tabularx does what I want to, but I haven't found a similar package that works in the math environment. mathtools allows for right-aligned matrices, but does not provide, afaik, an option for making columns equally wide.

I've provided a small example that looks best if the columns are both equally wide and right-aligned. Generally, I think many matrices are easier to read when the columns are equally wide. Of course, center-alignment is often ok, but not in all cases.

Code: Select all

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{array}
\begin{document}

% Alternative 1: Make columns equally wide:
\newcolumntype{K}{>{$}p{0.3cm}<{$}}

% Alternative 2: Make columns right-aligned:
%\newcolumntype{K}{r}

{\footnotesize 
\begin{align*}
  D(e) &= \left(
  \begin{array}{*{6}K}
     1 & 0 & 0 & 0 & 0 & 0 \\
     0 & 1 & 0 & 0 & 0 & 0 \\
     0 & 0 & 1 & 0 & 0 & 0 \\
     0 & 0 & 0 & 1 & 0 & 0 \\
     0 & 0 & 0 & 0 & 1 & 0 \\
     0 & 0 & 0 & 0 & 0 & 1
    \end{array}
  \right)
  \qquad
  D(C_{2x'}) = \left(
    \begin{array}{*{6}K}
     -1 & 0 & 0 & 0 & 0 & 0 \\
     0 & -1 & 0 & 0 & 0 & 0 \\
     0 & 0 & 1 & 0 & 0 & 0 \\
     0 & 0 & 0 & 1 & 0 & 0 \\
     0 & 0 & 0 & 0 & 1 & 0 \\
     0 & 0 & 0 & 0 & 0 & 1
    \end{array} 
  \right) \\
  D(C_{2y'}) &= \left(
  \begin{array}{*{6}K}
    0 & i & 0 & 0 & 0 & 0 \\
    -i & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & -1 & 0 & 0 & 0 \\
    0 & 0 & 0 & 1 & 0 & 0 \\
    0 & 0 & 0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 0 & 0 & -1
    \end{array}
  \right)
  \qquad
  D(C_{2z'}) = \left(
  \begin{array}{*{6}K}
    0 & -i & 0 & 0 & 0 & 0 \\
    i & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & -1 & 0 & 0 & 0 \\
    0 & 0 & 0 & 1 & 0 & 0 \\
    0 & 0 & 0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 0 & 0 & -1
    \end{array}
  \right)
\end{align*} 
}

\end{document}

Recommended reading 2024:

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

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

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Matrix with right-aligned columns of equal width

Post by gmedina »

Hi,

perhaps something like this?

Code: Select all

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{array}
\begin{document}

\newcolumntype{K}{>{\raggedleft\arraybackslash$}p{0.4cm}<{$}}

{\footnotesize 
\begin{align*}
  D(e) &= \left(
  \begin{array}{*{6}K}
     1 & 0 & 0 & 0 & 0 & 0 \\
     0 & 1 & 0 & 0 & 0 & 0 \\
     0 & 0 & 1 & 0 & 0 & 0 \\
     0 & 0 & 0 & 1 & 0 & 0 \\
     0 & 0 & 0 & 0 & 1 & 0 \\
     0 & 0 & 0 & 0 & 0 & 1
    \end{array}
  \right)
  \qquad
  D(C_{2x'}) = \left(
    \begin{array}{*{6}K}
     -1 & 0 & 0 & 0 & 0 & 0 \\
     0 & -1 & 0 & 0 & 0 & 0 \\
     0 & 0 & 1 & 0 & 0 & 0 \\
     0 & 0 & 0 & 1 & 0 & 0 \\
     0 & 0 & 0 & 0 & 1 & 0 \\
     0 & 0 & 0 & 0 & 0 & 1
    \end{array} 
  \right) \\
  D(C_{2y'}) &= \left(
  \begin{array}{*{6}K}
    0 & i & 0 & 0 & 0 & 0 \\
    -i & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & -1 & 0 & 0 & 0 \\
    0 & 0 & 0 & 1 & 0 & 0 \\
    0 & 0 & 0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 0 & 0 & -1
    \end{array}
  \right)
  \qquad
  D(C_{2z'}) = \left(
  \begin{array}{*{6}K}
    0 & -i & 0 & 0 & 0 & 0 \\
    i & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & -1 & 0 & 0 & 0 \\
    0 & 0 & 0 & 1 & 0 & 0 \\
    0 & 0 & 0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 0 & 0 & -1
    \end{array}
  \right)
\end{align*} 
}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
luredan
Posts: 2
Joined: Tue May 04, 2010 2:18 pm

Re: Matrix with right-aligned columns of equal width

Post by luredan »

Yes, that's perfect!
Thank you so much! :)
Post Reply