# Equal spacing in matrix

I'm trying to create a matrix that has equal spacing between each of the columns, but so far I have not been very succesful. The code I'm using right now is

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{align}
U &= \begin{pmatrix}
e^{\frac{i}{\hbar}|A|t} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & e^{-\frac{i}{\hbar}|A|t} & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & e^{-\frac{i}{\hbar}|A|t} & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & e^{\frac{i}{\hbar}|A|t}
\end{pmatrix}
\end{align}

\end{document}


This generates rather ugly output, with the columns containing the exponentials much wider than the others:

I suppose this is a very stupid and elementary question, but a quick search didn't get me anywhere sadly.

• With equal spacing, do you think the matrix will still fit within the bounds of \textwidth? You may consider defining a difference variable E (say) to shorten the column width... – Werner Feb 27 '14 at 23:41

## 3 Answers

Done as a TABstack. The package has a \fixTABwidth{T} option. I've also increased the vertical spacing between lines slightly, to give a more balanced look.

\documentclass{article}

\usepackage{tabstackengine}
\stackMath

\begin{document}

$$\setstackgap{L}{1.1\baselineskip} \fixTABwidth{T} U = \parenMatrixstack{ e^{\frac{i}{\hbar}|A|t} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & e^{-\frac{i}{\hbar}|A|t} & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & e^{-\frac{i}{\hbar}|A|t} & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & e^{\frac{i}{\hbar}|A|t} }$$

\end{document}


It is actually not a "very stupid and elementary question", as given by the many upvotes at this question: Writing a table with equally spaced columns, based on the widest column

You can pad the columns with something like this (but if you make it this wide, the equation number moves down a line)

\documentclass{article}

\usepackage{amsmath}

\newcommand\w[1]{\makebox[2.5em]{$#1$}}

\begin{document}

\begin{align}
U &= \begin{pmatrix}
e^{\frac{i}{\hbar}|A|t} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & e^{-\frac{i}{\hbar}|A|t} & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\
\w0 & \w0 & \w0 & \w0 & \w1 & \w0 & \w0 & \w0 & \w0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & e^{-\frac{i}{\hbar}|A|t} & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & e^{\frac{i}{\hbar}|A|t}
\end{pmatrix}
\end{align}

\end{document}


Use of array to define a newcolumntype C with settowidth command to determine the widest element in the math array. Same notion is applicable in pmatrix case. As a side note, the calc package provides \widthof{...} can be an alternative too.

Code

\documentclass{article}
\usepackage{amsmath,calc}
\usepackage{array}                                              % http://ctan.org/pkg/array

\newcolumntype{C}[1]{>{\centering\arraybackslash$}m{#1}<{$}}
\newlength{\mycolwd}                                         % array column width
\settowidth{\mycolwd}{$e^{-\frac{i}{\hbar}|A|t}$}% "width" of $e^{-\frac{i}{\hbar}|A|t$; largest element in array
\begin{document}
$$B=\left [ \begin{array}{*{9}{@{}C{\mycolwd}@{}}} e^{\frac{i}{\hbar}|A|t} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & e^{-\frac{i}{\hbar}|A|t} & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & e^{-\frac{i}{\hbar}|A|t} & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & e^{\frac{i}{\hbar}|A|t} \end{array} \right ]$$

\settowidth{\mycolwd}{$e^{-\frac{i}{\hbar}|A|t}$}
\newcommand\w[1]{\makebox[\mycolwd]{$#1$}}

\begin{align}
B &= \begin{pmatrix}
e^{\frac{i}{\hbar}|A|t} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & e^{-\frac{i}{\hbar}|A|t} & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & \w1 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0  & 1 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & e^{-\frac{i}{\hbar}|A|t} & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & e^{\frac{i}{\hbar}|A|t}
\end{pmatrix}
\end{align}

\end{document}