# Display square matrix as square

First off, before anyone jumps over with the 'duplicate' attacks: I've searched the forum a lot, but none of the solutions I found actually work anymore (they seem to be outdated or the reason is otherwise beyond my comprehension). My biggest hopes and most thorough attempts were related to the suggestions here.

Anyways, I'm currently using pdfLaTex with Overleaf.

The title pretty much describes what I'm trying to do, but here is a MWE for visualization:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{mathdots}
\usepackage{amsmath}

\begin{document}
Initial and good looking:
$\Delta_9 = \begin{vmatrix} \frac{8}{9} & 8 & \dots&\dots&\dots & 8 & -63\\ \frac{8}{8} & \vdots & & & \iddots & -56 & 8\\ \frac{8}{7} & \vdots & & \iddots & -49 & \iddots & \vdots\\ \vdots&\vdots&\iddots&\iddots&\iddots& &\vdots\\ \frac{8}{3} & 8 & -21 & \iddots & & & \vdots\\ \frac{8}{2} & -14 & \iddots & & & & \vdots\\ -7 & 8 & \dots & \dots & \dots & \dots & 8\\ \end{vmatrix}$

After some elementary operations and ugly looking:
$\begin{vmatrix} -7.1 & 8 & 8 & \dots & \dots & \dots & 8\\ \frac{8}{2} +7 & -7.2 - 8 & 0 & \dots & \dots & \dots & 0\\ \frac{8}{3} +7 & 0 & -7.3 - 8 & \ddots & & & \vdots\\ \vdots&\vdots&\ddots&\ddots&\ddots& &\vdots\\ \frac{8}{n-2} +7 & \vdots & & \ddots & -7(n-2) - 8 & \ddots & \vdots\\ \frac{8}{n-1} +7 & \vdots & & & \ddots & -7(n-1) - 8 & 0\\ \frac{8}{n} +7 & 0 & \dots&\dots&\dots & 0 & -7n - 8 \end{vmatrix}$
\end{document}


Very important for me is in this case, that this square matrix is actually displayed as a square. That is, every entry inside to get formatted to a square (and not a rectangle as it is happening by default) with respect to the biggest of the two dimensions (either height or length) of the highest/widest entry. I really don't mind the whole thing becoming too gigantic as long as it's nicely visually represented and comprehensible.

I'd love for the formatting to happen dynamically, since I've got a lot of matrices and I really wouldn't want to format each one separately with custom spacing values. (which is among the suggestions I've come across most often)

I need this because of the dots, which I use to intuitively visualize the structure of the matrix. So, if you've got any better suggestions for approaching comprehensible 'dotting' of rows, columns and diagonals, that's also an alternative solution.

And in general, maybe there's a 'best' way of tackling this need for dynamic comprehensive dotting of matrices, which I'm unaware of, so any clarification in this regard is also welcome!

That seems to be it. Thank you a lot in advance!

Try this code.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{mathdots}
\usepackage{amsmath}

\begin{document}
Initial and good looking:
$\Delta_9 = \begin{vmatrix} \frac{8}{9} & 8 & \dots&\dots&\dots & 8 & -63\\ \frac{8}{8} & \vdots & & & \iddots & -56 & 8\\ \frac{8}{7} & \vdots & & \iddots & -49 & \iddots & \vdots\\ \vdots&\vdots&\iddots&\iddots&\iddots& &\vdots\\ \frac{8}{3} & 8 & -21 & \iddots & & & \vdots\\ \frac{8}{2} & -14 & \iddots & & & & \vdots\\ -7 & 8 & \dots & \dots & \dots & \dots & 8\\ \end{vmatrix}$

After some elementary operations and better looking:

$\begin{vmatrix} -7.1 & 8 & 8 & \dots & \dots & \dots & 8\\ \frac{8}{2} +7 & -7.2 - 8 & 0 & \dots & \dots & \dots & 0\\ \frac{8}{3} +7 & 0 & -7.3 - 8 & \ddots & & & \vdots\\ \vdots&\vdots&\ddots&\ddots&\ddots& &\vdots\\ \frac{8}{n-2} +7 & \vdots & & \ddots & -7(n-2) - 8 & \ddots & \vdots\\ \frac{8}{n-1} +7 & \vdots & & & \ddots & -7(n-1) - 8 & 0\\ \frac{8}{n} +7 & 0 & \dots&\dots&\dots & 0 & -7n - 8 \end{vmatrix}$
\end{document} UPDATE

I wrote a macro to squarefy the matrix without manual adjustments (\arraystretch is calculated). Requires the mathtools package.

\documentclass{article}

\usepackage{calc}
\usepackage{mathdots}

\newcommand{\getlengthratio}{%
\number\numexpr
\dimexpr#1\relax
/
\dimexpr#2\relax
\relax}

\newcommand{\GetMatrix}{}
\newlength{\MWi}
\newlength{\MWii}
\newcounter{MWiii}

\newcommand{\squarefy}{%
\renewcommand{\GetMatrix}{}
\renewcommand{\arraystretch}{1}
\renewcommand{\GetMatrix}{#1}
\setlength{\MWi}{\widthof{$\displaystyle\GetMatrix$}}
\setlength{\MWii}{\heightof{$\displaystyle\GetMatrix$}}
\setcounter{MWiii}{\getlengthratio{0.5\MWi}{\MWii}}
$#2#1$}

\begin{document}

\squarefy{%
\begin{vmatrix}
\frac{8}{9} & 8 & \dots&\dots&\dots & 8 & -63\\
\frac{8}{8} & \vdots &  &  & \iddots & -56 & 8\\
\frac{8}{7} & \vdots &  & \iddots & -49 & \iddots & \vdots\\
\vdots&\vdots&\iddots&\iddots&\iddots& &\vdots\\
\frac{8}{3} & 8 & -21 & \iddots &  &  & \vdots\\
\frac{8}{2} & -14 & \iddots &  &  &  & \vdots\\
-7 & 8 & \dots & \dots & \dots & \dots & 8\\
\end{vmatrix}
}{\Delta_9 = }

\squarefy{%
\begin{vmatrix}
-7.1 & 8 & 8 & \dots & \dots & \dots & 8\\
\frac{8}{2} +7 & -7.2 - 8 & 0 & \dots & \dots & \dots & 0\\
\frac{8}{3} +7 & 0 & -7.3 - 8 & \ddots &  &  & \vdots\\
\vdots&\vdots&\ddots&\ddots&\ddots& &\vdots\\
\frac{8}{n-2} +7 & \vdots &  & \ddots & -7(n-2) - 8 & \ddots & \vdots\\
\frac{8}{n-1} +7 & \vdots &  &  & \ddots & -7(n-1) - 8 & 0\\
\frac{8}{n} +7 & 0 & \dots&\dots&\dots & 0 & -7n - 8
\end{vmatrix}
}{}

\end{document} • I tried playing around with this approach, but doesn't it actually require a certain constant stretching factor (in your MWE, that's 3) which depends on the context and I'll have to set it manually for every matrix? Or is there some other magic happening behind the scenes that I'm unaware of? Nov 19, 2020 at 16:01
• I don't know other magic. On the other hand setting arraystretch manually is something that can be done case by case during the final tuning of the document. Other solutions, as measuring the matrix width and calculate the stretching involve much more typing. But maybe another expert can provide a macro command ... Nov 19, 2020 at 18:10
• @D. Petrov Please see the updated answer, Nov 19, 2020 at 22:43