Math Mode – Force LaTeX to Show a Square Matrix as Square

amsmathmath-modematrices

How can i force latex to print out square matrix as a square matrix with equal width and height in math-mode?

I also read amsmath documentation, but don't found any useful help on it.

For example please for this matrix:

\documentclass{article}

\usepackage{amsmath}

\begin{document}
\[
\left[
\begin{matrix}
\times & \times & \times &&&&&&&\\
\times & \times &  & \times & \times & & & &\\
\times &  & \times & & \times & & & & \\
 & \times & & \times & & \times &&& \\
 & \times & \times & & \times & \times & \times & &&\\
 &&& \times & \times & \times && \times & \times\\
 &&&& \times && \times & \times & \\
 &&&&& \times & \times & \times & \times \\
 &&&&& \times && \times & \times 
\end{matrix}
\right]
\]
\end{document}

result is:

enter image description here

that is a rectangle not an square!

Best Answer

If you are willing to live under the constraint that all matrix elements occupy the same width (though your question implies that such a constraint may actually be a desirable requirement), then this \sqmatrix[alignment]{content} macro will do the "squaring" automatically.

Inter-element spacing (default 0pt) may be specified with \setstacktabbedgap{length}.

\documentclass{article}
\usepackage{tabstackengine}
\stackMath
\makeatletter
\newcommand\sqmatrix[2][c]{%
  \fixTABwidth{T}%
  \setbox0=\hbox{$\tabbedCenterstack{#2}$}%
  \setstackgap{L}{\dimexpr\maxTAB@width+\tabbed@gap}%
  \tabbedCenterstack[#1]{#2}%
}
\makeatother
\begin{document}
\def\mymatrix{\sqmatrix{
\XXX & \times & \times &&&&&&\\
\times & \times &  & \times & \times & & & &\\
\times &  & \times & & \times & & & & \\
 & \times & & \times & & \times &&& \\
 & \times & \times & & \times & \times & \times & &&\\
 &&& \times & \times & \times && \times & \times\\
 &&&& \times && \times & \times & \\
 &&&&& \times & \times & \times & \times \\
 &&&&& \times && \times & \times%
}}
\def\XXX{\times}
$\left[ \mymatrix \right]$%
\setstacktabbedgap{1ex}%HORIZONTAL GAP BETWEEN ENTRIES
$\left[ \mymatrix \right]$

\small\def\XXX{(x\times x)}
\setstacktabbedgap{0ex}%HORIZONTAL GAP BETWEEN ENTRIES
$\left[ \mymatrix \right]$
\end{document}

enter image description here