TransWikia.com

Issues drawing a matrix of 25x25 with Tikz

TeX - LaTeX Asked on August 9, 2021

I need to draw a matrix of 25×25 as the one showed below:
enter image description here

Since I’ll need to add colors and arrows (it is an schematic diagram), I’ve decided to use Tikz package to draw it. I’ve tried in many ways, but the better approach I’ve found is:

documentclass[10pt, a4paper]{article}
usepackage{tikz}
begin{document}

begin{figure}
    begin{center}
        begin{tikzpicture}
        draw (0,0) -- (0, 15);
        draw (0,0) -- (15, 0);
        draw (15,0) -- (15, 15);
        draw (0,15) -- (15, 15);
        
        foreach i in {14.4, 13.8, 13.2, 12.6, 12, ..., 0.0}
            ifnum i<>11.4  {node at (-i + 15, 0.6) {0};} else {node at (-i + 15, 0.6) {-1};}fi;
        end{tikzpicture}

        caption{} label{}
    end{center}
end{figure}

end{document}

which outputs the following matrix:

enter image description here

As you may grasp, my intention was to recreate the matrix row per row, from the lower to the upper part of the canvas, filling each place with its respective number. It is obvious that, in order to do so, is necessary to check the position of i in order to place a 0, a 4 or a -1. For example, in the first row, I would need to place a -1 when i = 13.8 and i = 11.4. The problem is that I don’t know how to properly use the if sentences, it would be convenient to use nested ifs or several conditions but I could not code that part, all I have is what I post here. Any approach to generate the matrix will be appreciated.

Note: The box has dimensions of 15 per 15 and each node is separated by the distance 0.6, that guarantee that will fit 25×25 elements.

2 Answers

This could be a starting point

documentclass[border=9,tikz]{standalone}
begin{document}

pgfkeys{
    /big toeplitz/-2/.code={a},
    /big toeplitz/-1/.code={b},
    /big toeplitz/0/.code={c},
    /big toeplitz/1/.code={d},
    /big toeplitz/2/.code={e},
    /big toeplitz/.unknown/.code={0}
}
tikz{
    draw[scale=.5]
        foreach x in {1,...,15}{
            foreach y in {1,...,15}{
                (x,y)node{
                    pgfmathtruncatemacrodifxy{x - y}
                    pgfkeys{/big toeplitz/difxy}
                }
            }
        }
    ;
}

end{document}

Correct answer by Symbol 1 on August 9, 2021

I see a neat solution by @Symbol 1 using TikZ's pgfkeys. If one is willing to use Asymptote, then I suppose the following solution is simplest, and easier to fine-turning like adding colors, scaling, .... One can embbed into LaTeX as usual. Moreover, there are several advantages: simple code, fixed size.

enter image description here

// http://asymptote.ualberta.ca/
size(10cm);
int n=25;

for (int i=1; i<n+1; ++i)
for (int j=1; j<n+1; ++j)
if (i==j) label(scale(.8)*"4",(i,-j),red);
else if (abs(i-j)==1) label(scale(.8)*"-1",(i,-j),blue);
else if (abs(i-j)==5) label(scale(.8)*"-1",(i,-j),orange);
else label(scale(.8)*"0",(i,-j));

real a=.5;
draw((1-a,-1+a)--(1-a,-n-a)^^(n+a,-1+a)--(n+a,-n-a),linewidth(1pt));
shipout(bbox(5mm,invisible)); 

Update As OP's request, there are some 0s in the superdiagonal and the subdiagonal.

enter image description here

size(10cm);
int n=15;

for (int i=1; i<n+1; ++i)
for (int j=1; j<n+1; ++j)
if (i==j) label("$4$",(i,-j),red);
else if ((i-j==1) & (j%5 != 0) ) label("$-1$",(i,-j),blue);
else if ((i-j==-1) & (i%5 != 0)) label("$-1$",(i,-j),blue);
else if (abs(i-j)==5) label("$-1$",(i,-j),purple);
else label("$0$",(i,-j));

Answered by Black Mild on August 9, 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