Tikzpicture:如何在数组中对字段进行颜色分离并使用调整大小框?

Tikzpicture:如何在数组中对字段进行颜色分离并使用调整大小框?

我想只给分离的节点上色?我该怎么做?我还想调整 tikzfigure 的大小,但我收到了错误消息Undefined control sequence. }

 \documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz}
\usetikzlibrary{intersections,matrix}

\begin{document}
\begin{figure}  
\resizebox {\textwidth} {!} {
\begin{tikzpicture} [nodes in empty cells, nodes={minimum width=0.2cm, minimum height=0.2cm}, row sep=-\pgflinewidth, column sep=-\pgflinewidth] 

    border/.style={draw}

\matrix(vector)[matrix of nodes, nodes={draw}] at (3,10) { & & & & & & & \\
};
\matrix(vector)[matrix of nodes, nodes={draw}] at (10,10) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[matrix of nodes, nodes={draw}] at (10,9) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[matrix of nodes, nodes={draw}] at (10,8) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[matrix of nodes, nodes={draw}] at (10,7) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};

\matrix(vector)[matrix of nodes, nodes={draw}] at (3,4) { & & & & & & & \\
};
\matrix(vector)[matrix of nodes, nodes={draw}] at (10,4) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[matrix of nodes, nodes={draw}] at (10,3) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[matrix of nodes, nodes={draw}] at (10,2) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[matrix of nodes, nodes={draw}] at (10,1) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[matrix of nodes, nodes={draw}] at (10,0) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};

\draw [thick,->] (10.3,9.8) -- (10.3,9.2);
\node[] at(10.7,9.5) {$\oplus$};
\draw [thick,->] (10.3,8.8) -- (10.3,8.2);
\node[] at(10.7,8.5) {$\oplus$};
\draw [thick,->] (10.3,7.8) -- (10.3,7.2);
\node[] at(10.7,7.5) {$T$};

\draw [thick,->] (10.3,3.8) -- (10.3,3.2);
\node[] at(10.7,3.5) {$\oplus$};
\draw [thick,->] (10.3,2.8) -- (10.3,2.2);
\node[] at(10.7,2.5) {$\oplus$};
\draw [thick,->] (10.3,1.8) -- (10.3,1.2);
\node[] at(10.7,1.5) {$\oplus$};
\draw [thick,->] (10.3,0.8) -- (10.3,0.2);
\node[] at(10.7,0.5) {$T$};

\node[] at (0.5,10) {$B_4^0\leftarrow\mathcal{B}_{7,\$}$:};
\draw[thick,->] (4.5,10) -- node[above] {$R_0$}  (5.5,10);

\node[] at (3.5,9) {$S_{127},S_0,S_{24},S_{50},S_{75},S_{102}$:};
\node[] at (1.7,8) {$S_{127}\oplus S_0\oplus S_{24}\oplus S_{50}\oplus S_{75}\oplus S_{102}\oplus R_0(B_0^0)$:};
\node[] at (5.3,7) {$S'_{127}$:};

\node[] at (0.5,4) {$B_5^0\leftarrow\mathcal{B}_{3,b}$:};
\draw[thick,->] (4.5,4) -- node[above] {$R_7$}  (5.5,4);

\node[] at (5.3,3) {$S'_{127}$:};
\node[] at (3.8,2) {$S_0,S_{24},S_{50},S_{75},S_{102}$:};
\node[] at (1.7,1) {$S'_{127}\oplus S_0\oplus S_{24}\oplus S_{50}\oplus S_{75}\oplus S_{102}\oplus R_0(B_0^0)$:};
\node[] at (5.3,0) {$S'_{126}$:};

\end{tikzpicture}
}
\end{figure}
\end{document}

输出

答案1

您的问题对我来说不是很清楚,因此我怀疑您正在寻找图像前两行中所看到的东西:

在此处输入图片描述

如果我错了,请告诉我。

编辑:

好吧,显然我误解了这个问题。只给一个单元格着色很简单。使用我之前的回答中的大部分代码(比你的更简洁、更短),你可以重写你的图片代码,如下所示:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
    \begin{tikzpicture}
    \tikzset{
    mtrx/.style={
    matrix of nodes,
    nodes in empty cells,
    nodes={draw, minimum size=2mm},
    row sep=-\pgflinewidth,
    column sep=-\pgflinewidth},
            }
\matrix (vector) [mtrx=red] at (3,10)    { & & & & & & & \\
};
\matrix (vector) [mtrx=blue!30] at (10,10)  { & |[fill=red]|  % <--- define color of this cell
                                              & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[mtrx=green] at (10,9) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[mtrx] at (10,8) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[mtrx] at (10,7) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};

\matrix(vector)[mtrx] at (3,4) { & & & & & & & \\
};
\matrix(vector)[mtrx] at (10,4) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[mtrx] at (10,3) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[mtrx] at (10,2) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[mtrx] at (10,1) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector)[mtrx] at (10,0) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};

\draw [thick,->] (10.3,9.8) -- (10.3,9.2);
\node[] at(10.7,9.5) {$\oplus$};
\draw [thick,->] (10.3,8.8) -- (10.3,8.2);
\node[] at(10.7,8.5) {$\oplus$};
\draw [thick,->] (10.3,7.8) -- (10.3,7.2);
\node[] at(10.7,7.5) {$T$};

\draw [thick,->] (10.3,3.8) -- (10.3,3.2);
\node[] at(10.7,3.5) {$\oplus$};
\draw [thick,->] (10.3,2.8) -- (10.3,2.2);
\node[] at(10.7,2.5) {$\oplus$};
\draw [thick,->] (10.3,1.8) -- (10.3,1.2);
\node[] at(10.7,1.5) {$\oplus$};
\draw [thick,->] (10.3,0.8) -- (10.3,0.2);
\node[] at(10.7,0.5) {$T$};

\node[] at (0.5,10) {$B_4^0\leftarrow\mathcal{B}_{7,\$}$:};
\draw[thick,->] (4.5,10) -- node[above] {$R_0$}  (5.5,10);

\node[] at (3.5,9) {$S_{127},S_0,S_{24},S_{50},S_{75},S_{102}$:};
\node[] at (1.7,8) {$S_{127}\oplus S_0\oplus S_{24}\oplus S_{50}\oplus S_{75}\oplus S_{102}\oplus R_0(B_0^0)$:};
\node[] at (5.3,7) {$S'_{127}$:};

\node[] at (0.5,4) {$B_5^0\leftarrow\mathcal{B}_{3,b}$:};
\draw[thick,->] (4.5,4) -- node[above] {$R_7$}  (5.5,4);

\node[] at (5.3,3) {$S'_{127}$:};
\node[] at (3.8,2) {$S_0,S_{24},S_{50},S_{75},S_{102}$:};
\node[] at (1.7,1) {$S'_{127}\oplus S_0\oplus S_{24}\oplus S_{50}\oplus S_{75}\oplus S_{102}\oplus R_0(B_0^0)$:};
\node[] at (5.3,0) {$S'_{126}$:};

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我发现的唯一简单解决方案是使用嵌套的 tikzpictures...但这真的很简单。还使用 svgnames 获得更多颜色:

代码:

    \documentclass[tikz,svgnames]{standalone}
    \usetikzlibrary{matrix}
    \usepackage{pgf}

    \newcommand{\myfill}[1]%This is the command manually adjusted to do the wholev job
    {\begin{tikzpicture}[overlay, remember picture]   
    \draw[draw=none,fill=#1,shift={(-0.11,-0.115)}](0,0)rectangle(0.22,0.225);
    \end{tikzpicture}}

    \begin{document}
    \begin{tikzpicture} [nodes in empty cells, nodes={minimum width=0.2cm, minimum height=0.2cm}, row sep=-\pgflinewidth, column sep=-\pgflinewidth] 

        border/.style={draw}

    \matrix(vector)[matrix of nodes, nodes={draw}] at (3,10) { & & & & & & & \\
    };
    \matrix(vector)[matrix of nodes, nodes={draw}] at (10,10) {\myfill{Red} & & &\myfill{Green} &\myfill{Brown} & & & \myfill{Blue!30!Yellow}& & & & & & & & & & & & & & & & & & & & & \myfill{Blue}\\
    };
    \matrix(vector)[matrix of nodes, nodes={draw}] at (10,9) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
    };
    \matrix(vector)[matrix of nodes, nodes={draw}] at (10,8) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
    };
    \matrix(vector)[matrix of nodes, nodes={draw}] at (10,7) { &\myfill{Green} & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
    };

    \matrix(vector)[matrix of nodes, nodes={draw}] at (3,4) { & & & & & & & \\
    };
    \matrix(vector)[matrix of nodes, nodes={draw}] at (10,4) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
    };
    \matrix(vector)[matrix of nodes, nodes={draw}] at (10,3) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
    };
    \matrix(vector)[matrix of nodes, nodes={draw}] at (10,2) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
    };
    \matrix(vector)[matrix of nodes, nodes={draw}] at (10,1) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
    };
    \matrix(vector)[matrix of nodes, nodes={draw}] at (10,0) { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
    };

    \draw [thick,->] (10.3,9.8) -- (10.3,9.2);
    \node[] at(10.7,9.5) {$\oplus$};
    \draw [thick,->] (10.3,8.8) -- (10.3,8.2);
    \node[] at(10.7,8.5) {$\oplus$};
    \draw [thick,->] (10.3,7.8) -- (10.3,7.2);
    \node[] at(10.7,7.5) {$T$};

    \draw [thick,->] (10.3,3.8) -- (10.3,3.2);
    \node[] at(10.7,3.5) {$\oplus$};
    \draw [thick,->] (10.3,2.8) -- (10.3,2.2);
    \node[] at(10.7,2.5) {$\oplus$};
    \draw [thick,->] (10.3,1.8) -- (10.3,1.2);
    \node[] at(10.7,1.5) {$\oplus$};
    \draw [thick,->] (10.3,0.8) -- (10.3,0.2);
    \node[] at(10.7,0.5) {$T$};

    \node[] at (0.5,10) {$B_4^0\leftarrow\mathcal{B}_{7,\$}$:};
    \draw[thick,->] (4.5,10) -- node[above] {$R_0$}  (5.5,10);

    \node[] at (3.5,9) {$S_{127},S_0,S_{24},S_{50},S_{75},S_{102}$:};
    \node[] at (1.7,8) {$S_{127}\oplus S_0\oplus S_{24}\oplus S_{50}\oplus S_{75}\oplus S_{102}\oplus R_0(B_0^0)$:};
    \node[] at (5.3,7) {$S'_{127}$:};

    \node[] at (0.5,4) {$B_5^0\leftarrow\mathcal{B}_{3,b}$:};
    \draw[thick,->] (4.5,4) -- node[above] {$R_7$}  (5.5,4);

    \node[] at (5.3,3) {$S'_{127}$:};
    \node[] at (3.8,2) {$S_0,S_{24},S_{50},S_{75},S_{102}$:};
    \node[] at (1.7,1) {$S'_{127}\oplus S_0\oplus S_{24}\oplus S_{50}\oplus S_{75}\oplus S_{102}\oplus R_0(B_0^0)$:};
    \node[] at (5.3,0) {$S'_{126}$:};

    \end{tikzpicture}
    \end{document}

输出:

在此处输入图片描述

编辑:OPS 后编辑:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz}
\usetikzlibrary{intersections,matrix}

\newcommand{\myfill}[1]%This is the command manually adjusted to do the wholev job
{\begin{tikzpicture}[overlay, remember picture]   
\draw[draw=none,fill=#1,shift={(-0.13,-0.13)}](0,0)rectangle(0.255,0.26);
\end{tikzpicture}}

\begin{document}
\begin{figure}  
\resizebox {\textwidth} {!} {
\begin{tikzpicture} [nodes in empty cells, nodes={minimum width=0.2cm, minimum height=0.2cm}, row sep=-\pgflinewidth, column sep=-\pgflinewidth] 

    border/.style={draw}

\matrix(vector)[matrix of nodes, nodes={draw},ampersand replacement=\&,] at (3,10) { \myfill{green}\& \myfill{red}\& \& \& \& \& \& \myfill{brown}\\
};
\matrix(vector)[matrix of nodes, nodes={draw},ampersand replacement=\&,] at (10,10) { \& \& \myfill{yellow}\& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \\
};
\matrix(vector)[matrix of nodes, nodes={draw},ampersand replacement=\&,] at (10,9) { \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \\
};
\matrix(vector)[matrix of nodes, nodes={draw},ampersand replacement=\&,] at (10,8) { \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \\
};
\matrix(vector)[matrix of nodes, nodes={draw},ampersand replacement=\&,] at (10,7) { \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \\
};

\matrix(vector)[matrix of nodes, nodes={draw},ampersand replacement=\&,] at (3,4) { \&\myfill{green} \& \& \& \& \& \& \\
};
\matrix(vector)[matrix of nodes, nodes={draw},ampersand replacement=\&,] at (10,4) { \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \\
};
\matrix(vector)[matrix of nodes, nodes={draw},ampersand replacement=\&,] at (10,3) { \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \\
};
\matrix(vector)[matrix of nodes, nodes={draw},ampersand replacement=\&,] at (10,2) { \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \\
};
\matrix(vector)[matrix of nodes, nodes={draw},ampersand replacement=\&,] at (10,1) { \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \\
};
\matrix(vector)[matrix of nodes, nodes={draw},ampersand replacement=\&,] at (10,0) { \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \& \\
};

\draw [thick,->] (10.3,9.8) -- (10.3,9.2);
\node[] at(10.7,9.5) {$\oplus$};
\draw [thick,->] (10.3,8.8) -- (10.3,8.2);
\node[] at(10.7,8.5) {$\oplus$};
\draw [thick,->] (10.3,7.8) -- (10.3,7.2);
\node[] at(10.7,7.5) {$T$};

\draw [thick,->] (10.3,3.8) -- (10.3,3.2);
\node[] at(10.7,3.5) {$\oplus$};
\draw [thick,->] (10.3,2.8) -- (10.3,2.2);
\node[] at(10.7,2.5) {$\oplus$};
\draw [thick,->] (10.3,1.8) -- (10.3,1.2);
\node[] at(10.7,1.5) {$\oplus$};
\draw [thick,->] (10.3,0.8) -- (10.3,0.2);
\node[] at(10.7,0.5) {$T$};

\node[] at (0.5,10) {$B_4^0\leftarrow\mathcal{B}_{7,\$}$:};
\draw[thick,->] (4.5,10) -- node[above] {$R_0$}  (5.5,10);

\node[] at (3.5,9) {$S_{127},S_0,S_{24},S_{50},S_{75},S_{102}$:};
\node[] at (1.7,8) {$S_{127}\oplus S_0\oplus S_{24}\oplus S_{50}\oplus S_{75}\oplus S_{102}\oplus R_0(B_0^0)$:};
\node[] at (5.3,7) {$S'_{127}$:};

\node[] at (0.5,4) {$B_5^0\leftarrow\mathcal{B}_{3,b}$:};
\draw[thick,->] (4.5,4) -- node[above] {$R_7$}  (5.5,4);

\node[] at (5.3,3) {$S'_{127}$:};
\node[] at (3.8,2) {$S_0,S_{24},S_{50},S_{75},S_{102}$:};
\node[] at (1.7,1) {$S'_{127}\oplus S_0\oplus S_{24}\oplus S_{50}\oplus S_{75}\oplus S_{102}\oplus R_0(B_0^0)$:};
\node[] at (5.3,0) {$S'_{126}$:};

\end{tikzpicture}
}
\end{figure}
\end{document}

答案3

这是一个利用的答案\resizebox。您需要做的就是将 tikzpicture 放入 lrbox 中。至于您的真正问题,您只需放入|[fill=red]|给定的单元格即可填充该单元格。我在代码中添加了一些示例。

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz}
\usetikzlibrary{intersections,matrix,positioning}
\newsavebox\picbox
\begin{document}
\begin{figure}  
\begin{lrbox}{\picbox}
\begin{tikzpicture} [nodes in empty cells, nodes={minimum width=0.2cm, minimum height=0.2cm}, row sep=-\pgflinewidth, column sep=-\pgflinewidth] 
    border/.style={draw}
\matrix(vector-1)[matrix of nodes, nodes={draw}] { & |[fill=red]| & & & & & & \\
};
\matrix(vector-2)[right=2cm of vector-1,matrix of nodes, nodes={draw}]  { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector-3)[below=1cm of vector-2,matrix of nodes, nodes={draw}]  { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector-4)[below=1cm of vector-3,matrix of nodes, nodes={draw}]  { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector-5)[below=1cm of vector-4,matrix of nodes, nodes={draw}]  { & &
|[fill=blue]| & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};

\matrix(vector-6)[below=6.5cm of vector-1,matrix of nodes, nodes={draw}]  { & & & & & & & \\
};
\matrix(vector-7)[right=2cm of vector-6,matrix of nodes, nodes={draw}]  { & & &
|[fill=yellow]| & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector-8)[below=1cm of vector-7,matrix of nodes, nodes={draw}]  { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector-9)[below=1cm of vector-8,matrix of nodes, nodes={draw}] { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector-10)[below=1cm of vector-9,matrix of nodes, nodes={draw}]  { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
\matrix(vector-11)[below=1cm of vector-10,matrix of nodes, nodes={draw}]  { & & & & & & & & & & & & & & & & & & & & & & & & & & & & \\
};
%
\foreach \X [evaluate=\X as \Y using {int(\X-1)}] in {3,4,8,9,10}
{\draw[thick,-latex] (vector-\Y) -- (vector-\X) node[midway,right]{$\oplus$};}
%
\foreach \X [evaluate=\X as \Y using {int(\X-1)}] in {5,11}
{\draw[thick,-latex] (vector-\Y) -- (vector-\X) node[midway,right]{$T$};}
%
\draw[thick,-latex] (vector-1) -- (vector-2) node[midway,above]{$R_0$};
\draw[thick,-latex] (vector-6) -- (vector-7) node[midway,above]{$R_7$};
%
\node[left=2mm of vector-1,anchor=east] {$B_4^0\leftarrow\mathcal{B}_{7,\$}$:};
%
\node[left=2mm of vector-3,anchor=east] {$S_{127},S_0,S_{24},S_{50},S_{75},S_{102}$:};
\node[left=2mm of vector-4,anchor=east] {$S_{127}\oplus S_0\oplus S_{24}\oplus S_{50}\oplus S_{75}\oplus S_{102}\oplus R_0(B_0^0)$:};
\node[left=2mm of vector-5,anchor=east] {$S'_{127}$:};
%
\node[left=2mm of vector-6,anchor=east]{$B_5^0\leftarrow\mathcal{B}_{3,b}$:};
%
\node[left=2mm of vector-8,anchor=east] {$S'_{127}$:};
\node[left=2mm of vector-9,anchor=east] {$S_0,S_{24},S_{50},S_{75},S_{102}$:};
\node[left=2mm of vector-10,anchor=east] {$S'_{127}\oplus S_0\oplus S_{24}\oplus S_{50}\oplus S_{75}\oplus S_{102}\oplus R_0(B_0^0)$:};
\node[left=2mm of vector-11,anchor=east] {$S'_{126}$:};
%
\end{tikzpicture}
\end{lrbox}
\resizebox{\textwidth}{!}{\usebox\picbox}
\end{figure}
\end{document}

在此处输入图片描述

需要注意的是:很有可能有人会复制我的代码,添加一些功能chains,然后声称这段代码变得更加“简洁”。我恳请您检查一下这是否属实。您的代码已经结构良好,因此易于理解和调整。我使用定位是因为我认为它在这里很有优势,并且还使用\foreach循环来重复操作。我个人不会在这里使用链,原因有几个,其中之一是您会失去调整位置的一些灵活性。同样,您可以定义重复操作的样式,但您的代码结构非常好,所以我认为这不会带来太大的区别。所以这只是说,如果有人声称更“简洁”,你应该认真问问自己这是否真的如此。

相关内容