1

Here is the table that I'm trying to build. How can it be done with TikZ?

enter image description here


UPDATE

Here is the code I'm using. But now I need to remove the outer border (as in the picture above). How to remove the outer border?

\begin{tikzpicture}
\tikzset{%
square matrix/.style={
    matrix of nodes,
    column sep=-\pgflinewidth, 
    row sep=-\pgflinewidth,
    nodes in empty cells,
    nodes={draw,
      minimum size=#1,
      anchor=center,
      align=center,
      inner sep=0pt
    },
    column 1/.style={nodes={fill=lightgray!30}},
    row 1/.style={nodes={fill=lightgray!30}},
  },
  square matrix/.default=1.2cm
}
\matrix[square matrix] (A)
{
         & 0        & 2         & 4         & 6         & 8         & $\cdots$\\ 
0        & 0        & 2         & 4         & 6         & 8         & $\ddots$\\ 
2        & 2        & 4         & 6         & 8         & $\ddots$  &         \\ 
4        & 4        & 6         & 8         & $\ddots$  &           &         \\ 
6        & 6        & 8         & $\ddots$  &           &           &         \\ 
8        & 8        & $\ddots$  &           &           &           &         \\ 
$\vdots$ & $\ddots$ &           &           &           &           &         \\ 
};
\draw (A-1-1.north west)--(A-1-1.south east);
\node[below left=2mm and 2mm of A-1-1.north east] {$k$};
\node[above right=2mm and 2mm of A-1-1.south west] {$j$};
        \node[
            inner sep=-4pt,
            draw=black, 
            very thick,
            dashed,                        
            rounded corners=0.5em, 
            rotate fit=45, 
            fit={(A-3-2) (A-2-3)}] {};
        \node[
            inner sep=-4pt,
            draw=black, 
            very thick,
            dashed,                        
            rounded corners=0.5em, 
            rotate fit=45, 
            fit={(A-4-2) (A-2-4)}] {};            
        \node[
            inner sep=-4pt,
            draw=black, 
            very thick,        
            dashed,            
            rounded corners=0.5em, 
            rotate fit=45, 
            fit={(A-5-2) (A-2-5)}] {};                
        \node[
            inner sep=-4pt,
            draw=black, 
            very thick,
            dashed,                        
            rounded corners=0.5em, 
            rotate fit=45, 
            fit={(A-6-2) (A-2-6)}] {};                
            
\end{tikzpicture}

Here is the result.

enter image description here

7
  • 1
    could you upload your code highlighting what you have tried - data entry is the most tiring part
    – js bibra
    Commented Aug 29, 2023 at 11:09
  • The problematic part are those diagonals.
    – Neves
    Commented Aug 29, 2023 at 11:18
  • ...and the top left corner...
    – Neves
    Commented Aug 29, 2023 at 11:19
  • 4
    @Neves If the diagonal lines and the left corner are the problem, then please show some code without them. I'm sure people can then use this code as a starting point and help you with the diagonal lines and the corner. Commented Aug 29, 2023 at 11:28
  • 1
    Take a look at the package nicematrix
    – pascal974
    Commented Aug 29, 2023 at 12:44

1 Answer 1

3

Quick hack to remove the outer border: draw a rectangle in your background colour above it:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix,positioning,fit}

\begin{document}


\begin{tikzpicture}
\tikzset{%
square matrix/.style={
    matrix of nodes,
    column sep=-\pgflinewidth, 
    row sep=-\pgflinewidth,
    nodes in empty cells,
    nodes={draw,
      minimum size=#1,
      anchor=center,
      align=center,
      inner sep=0pt
    },
    column 1/.style={nodes={fill=lightgray!30}},
    row 1/.style={nodes={fill=lightgray!30}},
  },
  square matrix/.default=1.2cm
}
\matrix[square matrix] (A)
{
         & 0        & 2         & 4         & 6         & 8         & $\cdots$\\ 
0        & 0        & 2         & 4         & 6         & 8         & $\ddots$\\ 
2        & 2        & 4         & 6         & 8         & $\ddots$  &         \\ 
4        & 4        & 6         & 8         & $\ddots$  &           &         \\ 
6        & 6        & 8         & $\ddots$  &           &           &         \\ 
8        & 8        & $\ddots$  &           &           &           &         \\ 
$\vdots$ & $\ddots$ &           &           &           &           &         \\ 
};
\draw (A-1-1.north west)--(A-1-1.south east);
\node[below left=2mm and 2mm of A-1-1.north east] {$k$};
\node[above right=2mm and 2mm of A-1-1.south west] {$j$};
        \node[
            inner sep=-4pt,
            draw=black, 
            very thick,
            dashed,                        
            rounded corners=0.5em, 
            rotate fit=45, 
            fit={(A-3-2) (A-2-3)}] {};
        \node[
            inner sep=-4pt,
            draw=black, 
            very thick,
            dashed,                        
            rounded corners=0.5em, 
            rotate fit=45, 
            fit={(A-4-2) (A-2-4)}] {};            
        \node[
            inner sep=-4pt,
            draw=black, 
            very thick,        
            dashed,            
            rounded corners=0.5em, 
            rotate fit=45, 
            fit={(A-5-2) (A-2-5)}] {};                
        \node[
            inner sep=-4pt,
            draw=black, 
            very thick,
            dashed,                        
            rounded corners=0.5em, 
            rotate fit=45, 
            fit={(A-6-2) (A-2-6)}] {};                
        \draw[white,ultra thick] (A-1-1.north west)  rectangle (A-7-7.south east);   
\end{tikzpicture}


\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .