2

I'm trying to make cubes that are next to one another in a grid like pattern. I figured the best way to do this would be to use nodes and then just place my drawings there. I found from this post how to write a custom command to make arbitrary cubes (in a formatting I want). What I would like to end up with is something like

\begin{tikzpicture}
   \node[\tikzcube{3}{1}] (c0) {};
   \node[\tikzcube{4}{1}, right=10cm] (c1) {};
   \node[\tikzcube{2}{1}, below=10cm] (c2) {};
   ... so on ...

Where \tikzcube is a custom command that performs drawings (as seen in the linked post).

1 Answers1

5

I just copied the definition of the cube from your link and made them `pic's. The syntax is very similar to what you are suggesting.

\documentclass[tikz,border=3.14mm]{standalone}
\newcommand{\tikzcuboid}[3]{% width, height, depth, scale
\foreach \x in {0,...,#1}
{   \draw (\x ,0  ,#3 ) -- (\x ,#2 ,#3 );
    \draw (\x ,#2 ,#3 ) -- (\x ,#2 ,0  );
}
\foreach \x in {0,...,#2}
{   \draw (#1 ,\x ,#3 ) -- (#1 ,\x ,0  );
    \draw (0  ,\x ,#3 ) -- (#1 ,\x ,#3 );
}
\foreach \x in {0,...,#3}
{   \draw (#1 ,0  ,\x ) -- (#1 ,#2 ,\x );
    \draw (0  ,#2 ,\x ) -- (#1 ,#2 ,\x );
}
}

\newcommand{\tikzcube}[1]{% length, scale
\tikzcuboid{#1}{#1}{#1}
}
\begin{document}
 \begin{tikzpicture}[pics/cube/.style={code={\tikzcube{#1}}}]
  \pic (c0) {cube=3} ;
  \pic[right=10cm] (c1) {cube=4};
  \pic[below=10cm] (c2) {cube=2};
 \end{tikzpicture}
\end{document}

enter image description here

  • Awesome! This is exactly what I was looking for. (btw, I misread the intention of your above comment and didn't realize you mean the \pic command, which I didn't know about) – MaybeALlama Sep 22 '19 at 00:00
  • If I wanted to in turn use cuboid then how do I modify it. I'm trying \begin{tikzpicture}[pic/cuboid/.style={code={\tikzcuboid{#1}{#2}{#3}}}] and getting an illegal parameter number error – MaybeALlama Sep 22 '19 at 00:10
  • 2
    @StevenWalton It needs to be \begin{tikzpicture}[pic/cuboid/.style n args={3}{code={\tikzcuboid{#1}{#2}{#3}}}] but I personally do not like to use pics with so many mandatory arguments. Rather, I prefer to store them in pgf keys like e.g. in https://tex.stackexchange.com/a/489719. –  Sep 22 '19 at 00:13