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).

  • Welcome! This is precisely what so-called pic's are for. – Schrödinger's cat Sep 21 at 23:16
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) – Steven Walton Sep 22 at 0: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 – Steven Walton Sep 22 at 0: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 tex.stackexchange.com/a/489719. – Schrödinger's cat Sep 22 at 0:13

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.