TransWikia.com

Display square matrix as square

TeX - LaTeX Asked by D. Petrov on January 26, 2021

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}

Which produces:
Rectangle instead of square matrix

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!

One Answer

Try this code.

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

usepackage{array}% added

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:

renewcommand{arraystretch}{3} % added
    [
    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}

square

UPDATE

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

documentclass{article}

usepackage{mathtools} % added
usepackage{calc}
usepackage{mathdots}

newcommand{getlengthratio}[2]{%
    numbernumexpr
    dimexpr#1relax
    /
    dimexpr#2relax
    relax} 

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

newcommand{squarefy}[2]{%
renewcommand{GetMatrix}{} 
renewcommand{arraystretch}{1}
renewcommand{GetMatrix}{#1}
setlength{MWi}{widthof{$displaystyleGetMatrix$}}
setlength{MWii}{heightof{$displaystyleGetMatrix$}}
setcounter{MWiii}{getlengthratio{0.5MWi}{MWii}}
renewcommand{arraystretch}{theMWiii} %% added
[#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}

newsquarefy

Correct answer by Simon Dispa on January 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP