13

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.

  • 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 '14 at 23:41
7

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

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

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.