17

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:

enter image description here

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

1
  • 3
    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

5 Answers 5

9

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}

\begin{equation}
\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{equation}

\end{document}

enter image description here

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

7

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.

enter image description here

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}
\begin{equation}
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 ]
\end{equation}

\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}
5

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}
2

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

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{equation}
\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{equation}

\end{document}

Output of the above code

0

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}

\begin{equation}
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}
\end{equation}

\begin{equation}
\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{equation}

\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.

enter image description here

You must log in to answer this question.

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