我怎样才能在 中生成如下图形TikZ
?我还想在所有单元格中叠加所需的数字。到目前为止,我已经能够生成类似的图形,但在所有背景矩阵中具有相同的颜色,并且无法控制单元格条目:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\pgfmathdeclarerandomlist{choices}{{U}{0}}
\foreach \z in {0,2.5,5}{
\begin{scope}[xshift=\z*0.02cm]
\foreach \x in {0,...,3}{
\foreach \y in {0,...,3}{
\node[fill=yellow,minimum size=1cm-\pgflinewidth,draw] (n-\x-\y-\z) at (\x+.5,\y+.5,\z) {\pgfmathrandomitem{\mynum}{choices}\mynum}; } } \end{scope} }
\end{tikzpicture}
\end{document}
提前致谢!
答案1
使用matrix
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\pgfmathdeclarerandomlist{choices}{{U}{0}}
\foreach \z/\c in {0/blue,2.5/blue!66,5/blue!33}{
\begin{scope}[xshift=\z]
\foreach \x in {0,...,3}{
\foreach \y in {0,...,3}{
\node[fill=\c,minimum size=1cm-\pgflinewidth,draw]
(n-\x-\y-\z) at
(\x,\y,\z) {\pgfmathrandomitem{\mynum}{choices}\mynum}; } }
\end{scope} }
\end{tikzpicture}
\begin{tikzpicture}
\foreach \s/\c in {0cm/blue,1cm/blue!66,2cm/blue!33}{
\begin{scope}[xshift=-\s,yshift=-\s]
\matrix [matrix of nodes, ampersand replacement=\&,
nodes={rectangle, anchor=center, draw=black!40, fill=\c,
minimum width=1cm, minimum height=1cm},
row sep=-\pgflinewidth, column sep=-\pgflinewidth,
nodes in empty cells]
{
1 \& 2 \& \& \\
\& \& \& \\
\& \& \& \\
\& \& \& \\
};
\end{scope}
}
\end{tikzpicture}
\end{document}