# Force latex to show a square matrix as square

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:

that is a rectangle not an square!

-
This question is quite similar. – jlv Apr 19 '14 at 14:00

## 3 Answers

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}


-

Adjust \arraystretch to an appropriate factor:

\documentclass{article}

\usepackage{amsmath}

\begin{document}
$\renewcommand{\arraystretch}{1.3} \begin{bmatrix} \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{bmatrix}$
\end{document}

-
how did you get the 1.3 factor? Just testing or there is some math to do? – Sigur Apr 19 '14 at 15:07
@Sigur: Just a test-value, since it depends on the contents' height relative to the value of \baselineskip. – Werner Apr 19 '14 at 16:40

You could set the height of each line manually providing the line height in square brackets after each line end:

\times & \times & ... & \times\$1ex]  To give all lines the same height, you can use a macro: \documentclass{article} \usepackage{amsmath} \newcommand{\squarelineheight}{0.9ex} \begin{document} \[ \left[ \begin{matrix} \times & \times & \times &&&&&&&\\[\squarelineheight] \times & \times & & \times & \times & & & &\\[\squarelineheight] \times & & \times & & \times & & & & \\[\squarelineheight] & \times & & \times & & \times &&& \\[\squarelineheight] & \times & \times & & \times & \times & \times & &&\\[\squarelineheight] &&& \times & \times & \times && \times & \times\\[\squarelineheight] &&&& \times && \times & \times & \\[\squarelineheight] &&&&& \times & \times & \times & \times \\[\squarelineheight] &&&&& \times && \times & \times \end{matrix} \right]$
\end{document}

-
Instead of adding \squarelineheight of extra vertical space at the end of each row, one could load the array package and specify -- either globally in the preamble or locally, i.e., inside a TeX group that includes the matrix in question -- \setlength{\extrarowheight}{0.9ex}, or whatever the desired amount of vertical whitespace may be. – Mico Apr 19 '14 at 15:29