绘制块矩阵最简单的方法是什么?

绘制块矩阵最简单的方法是什么?

我想绘制具有特殊结构的块矩阵,如下图所示。最简单的方法是什么?

块矩阵

答案1

好吧,当我需要做类似的任务时,我编写了\Rect函数

\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}

它绘制了第一幅图像。

第一张图片

[截图由 cfr 添加]

答案2

一个普通的表格就colortbl可以了:

\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} 

在此处输入图片描述

如果您不想在空单元格中使用水平线,则可以轻松做到这一点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} 

在此处输入图片描述

答案3

TikZ matrix缺少解决方案!

\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}

在此处输入图片描述

答案4

另一种基于psmatrix环境的解决方案来自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} 

在此处输入图片描述

相关内容