7

I am trying to recreate below table in tikz (I need because I could use tikz in my jupyter notebook to render embedded output but not yet latex)

enter image description here

When I try via tikz I only get this far..

enter image description here

My MWE:

\begin{tikzpicture}

\tikzset{square matrix/.style={
    matrix of nodes,
    column sep=-\pgflinewidth, row sep=-\pgflinewidth,
    nodes={draw,
      minimum height=#1,
      anchor=center,
      text width=#1,
      align=center,
      inner sep=0pt
    },
  },
  square matrix/.default=1.2cm
}

\matrix[square matrix]
{
1 & &  & & & & \\ 
2 & x & & & & & \\ 
3 & x & x & & & & \\ 
4 & x & x & x & & & \\ 
5 & x & x & x & x & & \\ 
6 & x & x & x & x & x & \\ 
};

\end{tikzpicture}
8

Sebastiano provided the solution for missing cells and how to fill each one, but it's also possible to define styles for complet rows and columns. And with some extra works draw the top left cell.

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{matrix, positioning}

\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=cyan!30}},
    row 1/.style={nodes={fill=cyan!30}},
  },
  square matrix/.default=1.2cm
}

\matrix[square matrix] (A)
{
  & 1 & 2 & 3 & 4 & 5 & 6\\ 
1 & & & & & & \\ 
2 & x & & & & & \\ 
3 & x & x & & & &   \\ 
4 & x & x & x & & & \\ 
5 & x & x & x & x & & \\ 
6 & x & x & x & x & x & \\ 
};

\draw (A-1-1.north west)--(A-1-1.south east);
\node[below left=2mm and 2mm of A-1-1.north east] {$i$};
\node[above right=2mm and 2mm of A-1-1.south west] {$j$};
\end{tikzpicture}
\end{document}

enter image description here

Update:

To obtain some special color distribution like in your link

enter image description here

you can mix some automating filling for regular rows and columns and some manual filling on background:

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{matrix, positioning, backgrounds}

\tikzset{%
square matrix/.style={
    matrix of math nodes,
    column sep=-\pgflinewidth, 
    row sep=-\pgflinewidth,
    nodes in empty cells,
    nodes={draw,
      minimum width=#1,
      minimum height=.5*#1,
      anchor=center,
%      align=center,
      inner sep=0pt
    },
    column 1/.style={nodes={fill=gray!30}},
    row 1/.style={nodes={fill=gray!30}},
  },
  square matrix/.default=1.2cm
}

\begin{document}
\begin{tikzpicture}
\matrix (A) [square matrix]
{
j\backslash i  & x_1 & x_2 & x_3 & x_4 & x_5 & x_6\\    
y_1 & R_{11} & R_{12} & R_{13} & R_{14} & R_{15} & R_{16} \\ 
y_2 & R_{21} & R_{22} & R_{23} & R_{24} & R_{25} & R_{26} \\ 
y_3 & R_{31} & R_{32} & R_{33} & R_{34} & R_{35} & R_{36} \\ 
y_4 & R_{41} & R_{42} & R_{43} & R_{44} & R_{45} & R_{46} \\ 
y_5 & R_{51} & R_{52} & R_{53} & R_{54} & R_{55} & R_{56} \\ 
y_6 & R_{61} & R_{62} & R_{63} & R_{64} & R_{65} & R_{66} \\ 
};
\begin{scope}[on background layer]
\fill[cyan!15] (A-3-2.north west)-|(A-4-3.north west)-|(A-5-4.north west)-|(A-6-5.north west)-|(A-7-6.north west)-|(A-7-6.south east)-|cycle;
\fill[red!15] (A-2-3.north west)|-(A-3-4.north west)
|-(A-4-5.north west)|-(A-5-6.north west)|-(A-6-7.north west)|-(A-6-7.south east)
|-cycle;
\foreach \i in {2,...,7}
    \fill[gray!30] (A-\i-\i.north west) rectangle (A-\i-\i.south east);
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

2nd Update:

You could construct an automatic solution:

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{matrix, positioning, backgrounds}

\tikzset{%
myfill/.code={%
    \ifnum\pgfmatrixcurrentcolumn<\pgfmatrixcurrentrow
        \pgfkeysalso{fill=cyan!30}
    \else
        \ifnum\pgfmatrixcurrentcolumn>\pgfmatrixcurrentrow
            \pgfkeysalso{fill=red!30}
        \else
            \pgfkeysalso{fill=gray!20}
        \fi
    \fi
    },
square matrix/.style={
    matrix of math nodes,
    column sep=-\pgflinewidth, 
    row sep=-\pgflinewidth,
    nodes in empty cells,
    nodes={draw,
        myfill,
      minimum width=#1,
      minimum height=.5*#1,
      anchor=center,
      inner sep=0pt,
      node contents={
        R_{\the\numexpr\pgfmatrixcurrentcolumn-1\relax%
        \the\numexpr\pgfmatrixcurrentrow-1\relax%
      }}
    },
    column 1/.style={
        nodes={fill=gray!30,
        node contents={y_\the\numexpr\pgfmatrixcurrentrow-1\relax}
        }},
    row 1/.style={
        nodes={fill=gray!30,
        node contents={x_\the\numexpr\pgfmatrixcurrentcolumn-1\relax}
        }},
     row 1 column 1/.style={
        nodes={fill=gray!30,
        node contents={j\backslash i}
        }},
  },
  square matrix/.default=1.2cm
}

\begin{document}
\begin{tikzpicture}
\matrix (A) [square matrix]
{
&&&&&&\\
&&&&&&\\
&&&&&&\\
&&&&&&\\
&&&&&&\\
&&&&&&\\
&&&&&&\\
};
\end{tikzpicture}
\end{document}

enter image description here

  • I upvoted quickly you. Excellent. – Sebastiano Oct 31 '18 at 9:15
  • great, can you also please explain what is happening? how are the colors filled for entire row and column? – Parthiban Rajendran Oct 31 '18 at 9:33
  • @PaariVendhan with declarations row 1/.style and column 1/.style. – Ignasi Oct 31 '18 at 9:53
  • @Ignasi Please, could you help the OP to create a macro for the image that have put into my comments? – Sebastiano Oct 31 '18 at 9:54
  • @Ignasi column 1/.style={nodes={fill=cyan!30}}, means, every 1st cell of all columns? – Parthiban Rajendran Oct 31 '18 at 9:59
5

I have added to the your LaTeX code the option:

\matrix[square matrix, nodes in empty cells]
{
&   &   &   &\\

Surely adding or deleting & you can create (not create) others empty cells.

enter image description here

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\usetikzlibrary{matrix}
\tikzset{square matrix/.style={
    matrix of nodes,
    column sep=-\pgflinewidth, row sep=-\pgflinewidth,
    nodes={draw,
      minimum height=#1,
      anchor=center,
      text width=#1,
      align=center,
      inner sep=0pt
    },
  },
  square matrix/.default=1.2cm
}

\matrix[square matrix, nodes in empty cells]
{
&   &   &   &\\
1 & &  & & & & \\ 
2 & x & & & & & \\ 
3 & x & x & & & & \\ 
4 & x & x & x & & & \\ 
5 & x & x & x & x & & \\ 
6 & x & x & x & x & x & \\ 
};

\end{tikzpicture}
\end{document}

ADDENDUM:

I have fill one only cell. With the same structure you can fill one or more cells.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{xcolor}


\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\tikzset{square matrix/.style={
    matrix of nodes,
    column sep=-\pgflinewidth, row sep=-\pgflinewidth,
    nodes={draw,
      minimum height=#1,
      anchor=center,
      text width=#1,
      align=center,
      inner sep=0pt
    },
  },
  square matrix/.default=1.2cm
}

\matrix[square matrix, nodes in empty cells]
{
|[fill=green]|&   &   &   &\\
1 & &  & & & & \\ 
2 & x & & & & & \\ 
3 & x & x & & & & \\ 
4 & x & x & x & & & \\ 
5 & x & x & x & x & & \\ 
6 & x & x & x & x & x & \\ 
};

\end{tikzpicture}
\end{document}
  • nodes in empty cells fixed the issue. However is it possible to recreate as my table 1 as shown? esp, that diagonally cut top left cell? – Parthiban Rajendran Oct 31 '18 at 8:40
  • Do you prefer fill the cells? – Sebastiano Oct 31 '18 at 8:43
  • 1
    filling the cell with color? yes – Parthiban Rajendran Oct 31 '18 at 8:44
  • Done! I have added the your request. – Sebastiano Oct 31 '18 at 8:52
  • 1
    great, can you please show how do I make a macro out of this, as I have special needs to color the cells.. something like this – Parthiban Rajendran Oct 31 '18 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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