0

I would like to draw block matrices with special structures like shown in the picture below. What's the easiest way to do that?

block matrices

3
  • Exactly as you're showing them? There are various ways: a matrix of nodes, pgfplotstable, even a regular tabular if you know what you're doing.
    – Alenanno
    Jan 23, 2016 at 19:01
  • 3
    Requesting a solution that is easiest is subjective. Instead, please provide the community with some specifics that should be covered in an answer. What about your package preference? What have you tried thus far?
    – Werner
    Jan 23, 2016 at 19:09
  • Of course, you're right, Werner. I'm sorry. I tried to modify an example I found in the web. But I didn't get along, because I'm new to Tikz. From my standpoint, Giorgi's answer is the one I would prefer.
    – Rorschach
    Jan 24, 2016 at 12:27

4 Answers 4

7

Well, when I needed to do similar task, I wrote \Rect function

\documentclass{article}
\usepackage{tikz}

\newcommand{\Rect}[5]{
    \draw[#1] (#2,#3) rectangle(#2+#4,#3-#5);
}

\begin{document}

\begin{tikzpicture}
\Rect{}{0}{4}{6}{4}

\Rect{fill=red}{0}{4}{2}{1}
\Rect{fill=red}{2}{4}{2}{1}
\Rect{fill=red}{4}{4}{2}{1}

\Rect{fill=gray}{0}{3}{2}{1}
\Rect{fill=gray}{2}{2}{2}{1}
\Rect{fill=gray}{4}{1}{2}{1}
\end{tikzpicture}

\end{document}

It draws the first image.

first image

[Screenshot added by cfr.]

1
  • 1
    I've added a screenshot. I hope you don't mind, but please roll back if you do ;).
    – cfr
    Jan 24, 2016 at 2:32
5

An ordinary tabular and colortbl will do:

\documentclass[11pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[x11names]{xcolor}
\usepackage{array, colortbl}

\begin{document}

\centering
\begin{tabular}{|*{4}{p{0.6cm}|}}
  \hline
  \rowcolor{SlateGray4!60} & & & \\
  \hline
                           & \cellcolor{SlateGray3!40} & & \\
  \hline
                           & & \cellcolor{SlateGray3!40} & \\
  \hline
                           & & & \cellcolor{SlateGray3!40} \\
  \hline
\end{tabular}
\qquad
\begin{tabular}{|*{4}{p{0.6cm}|} >{\columncolor{SlateGray4!60}}p{0.6cm} |}%
  \hline
  \cellcolor{SlateGray3!40} & & & & \\
  \hline
                            & \cellcolor{SlateGray3!40} & & & \\
  \hline
                            & & \cellcolor{SlateGray3!40} & & \\
  \hline
                            & & & \cellcolor{SlateGray3!40} & \\
  \hline
\end{tabular}
\vskip1cm
\begin{tabular}{|*{4}{p{0.6cm}|} >{\columncolor{SlateGray4!60}}p{0.6cm} |}%
  \hline
  \rowcolor{SlateGray4!60} & & & & \\
  \hline
  \cellcolor{SlateGray3!40} & & & & \\
  \hline
                            & \cellcolor{SlateGray3!40} & & & \\
  \hline
                            & & \cellcolor{SlateGray3!40} & & \\
  \hline
\end{tabular}

\end{document} 

enter image description here

If you do not want horizontal lines for empty cells, it is easy to do wih hhline:

\documentclass[11pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[x11names]{xcolor}
\usepackage{array, colortbl, hhline}
\usepackage{xparse}
\DeclareExpandableDocumentCommand\emptycell{O{|c|}m}{\multicolumn{#2}{#1}{}}

\begin{document}

\centering
\begin{tabular}{|*{4}{p{0.6cm}|}}
  \hline
  \rowcolor{SlateGray4!60} & & & \\
  \hline
     & \cellcolor{SlateGray3!40} & \emptycell[c|]{2} \\
  \hhline{|~|--|~|}
    \emptycell{2}& \cellcolor{SlateGray3!40} & \\
  \hhline{|~~|--|}
    \emptycell{3} & \cellcolor{SlateGray3!40} \\
  \hline
\end{tabular}
\qquad
\begin{tabular}{|*{4}{p{0.6cm}|} >{\columncolor{SlateGray4!60}}p{0.6cm} |}%
  \hline
  \cellcolor{SlateGray3!40} & \emptycell[c|]{3}& \\
  \hhline{--~~|-}
                            & \cellcolor{SlateGray3!40} & \emptycell[c|]{2} & \\
  \hhline{|~|--~|-}
       \emptycell{2} & \cellcolor{SlateGray3!40} & & \\
  \hhline{|~~|---}
   \emptycell{3} & \cellcolor{SlateGray3!40} & \\
  \hline
\end{tabular}
\vskip1cm
\begin{tabular}{|*{3}{p{0.6cm}|} >{\columncolor{SlateGray4!60}}p{0.6cm} |}%
  \hline
  \rowcolor{SlateGray4!60} & & & \\
  \hline
  \cellcolor{SlateGray3!40} & \emptycell[c|]{2} & \\
  \hhline{--~|-}
    & \cellcolor{SlateGray3!40} & & \\
  \hhline{|~|--|-}
     \emptycell{2} & \cellcolor{SlateGray3!40} & \\
  \hline
\end{tabular}

\end{document} 

enter image description here

0
2

A solution with TikZ matrix was missing!

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

\begin{document}
\begin{tikzpicture}[%
    mynode/.style={minimum width=1cm, minimum height=0.5cm,
        draw, fill=#1, outer sep=0pt},
    mynode/.default={black!60},
    N1/.style={mynode},
    N2/.style={mynode=black!30},
    mymatrix/.style={draw, column sep=-\pgflinewidth, row sep=-\pgflinewidth, matrix of nodes, inner sep=0pt}
    ]

    \matrix[mymatrix] (A) 
    {|[N1]|&|[N1]|&|[N1]|\\
     |[N2]|&       &     \\
           &|[N2]| &     \\
           &       &|[N2]|\\};

    \matrix[mymatrix, right=of A] (B) 
    {|[N2]|&      &      &      &|[N1]|\\
           &|[N2]|&      &      &|[N1]|\\
           &      &|[N2]|&      &|[N1]|\\
           &      &      &|[N2]|&|[N1]|\\};

    \matrix[mymatrix, right=of B] (C) 
    {|[N1]|&|[N1]|&|[N1]|&|[N1]|\\
     |[N2]|&      &      &|[N1]|\\
           &|[N2]|&      &|[N1]|\\
           &      &|[N2]|&|[N1]|\\};
\end{tikzpicture}
\end{document}

enter image description here

1

Another solution based on the psmatrix environment, from pstricks:

\documentclass[11pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{geometry}
\usepackage[x11names]{xcolor}
\usepackage{pst-node, auto-pst-pdf}

\begin{document}

\centering\noindent
\begin{pspicture}
  \begin{psmatrix}[mnode=p, emnode=p, colsep1.5cm, rowsep=0.4cm]
    [name=A0] & [name=A1] & [name=A2] & [name=A3] & [name=A4] \\
    [name=B0] & [name=B1] & [name=B2] & [name=B3] & \\
    & [name=C1] & [name=C2] & [name=C3] & \\
    & & [name=D2] & [name=D3] & [name=D4] \\
    [name=E0] & & & [name=E3] & [name=E4]
  \end{psmatrix}
  \psset{dimen =middle}
  \psframe(E0)(A4)
  \psset{fillstyle=solid}
  \psframe[fillcolor=SlateGray4!60](B0)(A4)
  \pspolygon[fillcolor=SlateGray3!40](B1)(B2)(C2)(C3)(D3)(D4)(E4)%
  (E3)(D3)(D2)(C2)(C1)(B1)
  \psline(A1)(B1)\psline(A2)(B2)\psline(A3)(B3)
  \hskip1.5cm
  \begin{psmatrix}[mnode=p, emnode=p, colsep1.5cm, rowsep=0.4cm]
    [name=A0] & [name=A1] & & & & [name=A5] \\
    [name=B0] & [name=B1] & [name=B2] & & [name=B4] & [name=B5] \\
    & [name=C1] & [name=C2] & [name=C3] & [name=C4] & [name=C5] \\
    & & [name=D2] & [name=D3] & [name=D4] & [name=D5] \\
    & & & [name=E3] & [name=E4] & [name=E5]
  \end{psmatrix}
  \psframe(A0)(E5)
  \psset{dimen=middle}
  \psframe(E4)(A5)
  \psset{fillstyle=solid}
  \psframe[fillcolor=SlateGray4!60](E4)(A5)
  \pspolygon[fillcolor=SlateGray3!40](A0)(A1)(B1)(B2)(C2)(C3)(D3)(D4)(E4)%
  (E3)(D3)(D2)(C2)(C1)(B1)(B0)(A0)
  \psline(B4)(B5)\psline(C4)(C5)\psline(D4)(D5)
\end{pspicture}
\vskip2cm
\begin{pspicture}
  \begin{psmatrix}[mnode=p, emnode=p, colsep1.5cm, rowsep=0.4cm]
    [name=A0] & [name=A1] & [name=A2] & [name=A3] & [name=A4] \\
    [name=B0] & [name=B1] & [name=B2] & [name=B3] & [name=B4] \\
    [name=C0] & [name=C1] & [name=C2] & [name=C3] & [name=C4] \\
    & [name=D1] & [name=D2] & [name=D3] & [name=D4] \\
    [name=E0] & & [name=E2] & [name=E3] & [name=E4]
  \end{psmatrix}
  \psframe(E0)(A4)
  \psset{fillstyle=solid}
  \pspolygon[fillcolor=SlateGray4!60](A0)(A4)(E4)(E3)(B3)(B0)(A0)
  \pspolygon[fillcolor=SlateGray3!40](B0)(B1)(C1)(C2)(D2)(D3)(E3)(E2)(D2)(D1)(C1)(C0)(B0)%
  \psline(A1)(B1)\psline(A2)(B2)\psline(A3)(B3)
  \psline(B3)(B4)\psline(C3)(C4)\psline(D3)(D4)
\end{pspicture}
\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 .