# 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, 2014 at 23:41

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

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}

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}

The environment {pNiceMatrix} of nicematrix has tools designed to address that kind of problem.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

$$\setlength{\arraycolsep}{0pt} U = \begin{pNiceMatrix}[columns-width=auto] 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{pNiceMatrix}$$

\end{document}

With a recent version of the array package you can do as follows, using the w{c}{<length>} column type:

\documentclass{article}

\usepackage{amsmath,array}

\makeatletter
\newenvironment{fwpmatrix}[2][\arraycolsep]
{%
\left(
\settowidth{\dimen@}{$#2$}%
\setlength{\arraycolsep}{#1}%
\let\@ifnextchar\new@ifnextchar
\hskip -\arraycolsep
\array{ *{\value{MaxMatrixCols}}{w{c}{\dimen@}} }
}
{%
\endarray
\hskip -\arraycolsep
\right)
}
\makeatother

\begin{document}

$$U = \begin{fwpmatrix}[-2pt]{e^{-\frac{i}{\hbar}|A|t}} 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{fwpmatrix}$$

$$\begin{fwpmatrix}{e^{-\frac{i}{\hbar}|A|t}} e^{-\frac{i}{\hbar}|A|t} & 0 & 0 & 0 & 0 & e^{-\frac{i}{\hbar}|A|t} \\ 0 & e^{-\frac{i}{\hbar}|A|t} & 0 & 0 & 0 & 0 \\ 0 & 0 & e^{-\frac{i}{\hbar}|A|t} & 0 & 0 & 0 \\ 0 & 0 & 0 & e^{-\frac{i}{\hbar}|A|t} & 1 & 1 \end{fwpmatrix}$$

\end{document}

The mandatory argument is the widest entry, the optional argument sets \arraycolsep. In the first case, where there are no adjacent columns with the widest entry, it makes sense to use a negative \arraycolsep, which is not the case for the second example.