Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question
    
This question is quite similar. –  jlv Apr 19 at 14:00

3 Answers 3

up vote 5 down vote accepted

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

share|improve this answer

Adjust \arraystretch to an appropriate factor:

enter image description here

\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}
share|improve this answer
    
how did you get the 1.3 factor? Just testing or there is some math to do? –  Sigur Apr 19 at 15:07
1  
@Sigur: Just a test-value, since it depends on the contents' height relative to the value of \baselineskip. –  Werner Apr 19 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}
share|improve this answer
    
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 at 15:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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