4

How can I produce a figure like below in TikZ? I would also like to overlay desired numbers in all the cells. I have been able to produce till now a similar figure but with the same color in all the background matrices and with no control on the cell entries:

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

Thanks in advance!

enter image description here

  • Welcome to TeX.SE!. We suggest you to show a minimal working example MWE on what you have worked so far, so we can help you further. – Cragfelt Nov 12 '17 at 5:57
  • Please add the code to you question itself. You can edit it. – Cragfelt Nov 12 '17 at 6:01
  • What are the "desired numbers"? With your code you get 0 or U in each square, if you want empty squares you have to remove \pgfmathrandomitem{\mynum}{choices}\mynum. – Torbjørn T. Nov 12 '17 at 8:13
4

Using the 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}

enter image description here

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.