# How can improve my Tikz scripting? (3D Cube)

I've been learning LaTeX for a while, and today I'm trying to create graphic elements for the first time using Tikz.

This is what I'm trying to achieve:

And this is what I've made:

\documentclass{standalone}
\usepackage{tikz}

\tikzset{
cube/.pic={
\draw[inside](-1,-1,-1)--(-1,1,-1)--(-1,1,1)--(-1,-1,1)--(-1,-1,-1);
\draw[inside](-1,-1,-1)--(1,-1,-1)--(1,-1,1)--(-1,-1,1)--(-1,-1,-1);
\draw[inside](-1,-1,-1)--(1,-1,-1)--(1,1,-1)--(-1,1,-1)--(-1,-1,-1);
\draw[inside](1,1,1)--(1,-1,1)--(1,-1,-1)--(1,1,-1)--(1,1,1);
\draw[inside](1,1,1)--(-1,1,1)--(-1,1,-1)--(1,1,-1)--(1,1,1);
\draw[inside](1,1,1)--(-1,1,1)--(-1,-1,1)--(1,-1,1)--(1,1,1);
},

\pic at (0, -2.5, -2.5) {cube};
\pic at (2.5, -2.5, -2.5) {cube};
\pic at (0, 0, -2.5) {cube};
\pic at (2.5, 0, -2.5) {cube};
\pic at (5, -2.5, -2.5) {cube};
\pic at (5, 0, -2.5) {cube};

\pic at (0, -2.5, 0) {cube};
\pic at (2.5, -2.5, 0) {cube};
\pic at (0, 0, 0) {cube};
\pic at (2.5, 0, 0) {cube};
\pic at (5, -2.5, 0) {cube};
\pic at (5, 0, 0) {cube};
},
block/.pic={
\draw[outside](-5,-5,-5)--(-5,5,-5)--(-5,5,5)--(-5,-5,5)--(-5,-5,-5);%kiri
\draw[outside](-5,-5,-5)--(5,-5,-5)--(5,-5,5)--(-5,-5,5)--(-5,-5,-5);%bawah
\draw[outside](-5,-5,-5)--(5,-5,-5)--(5,5,-5)--(-5,5,-5)--(-5,-5,-5);%belakang
\pic at (-3.5, 0, 0) {threads};
\draw(5,5,5)--(5,-5,5)--(5,-5,-5)--(5,5,-5)--(5,5,5);%kanan
\draw(5,5,5)--(-5,5,5)--(-5,5,-5)--(5,5,-5)--(5,5,5);%atas
\draw(5,5,5)--(-5,5,5)--(-5,-5,5)--(5,-5,5)--(5,5,5);%depan
}
}

\begin{document}

\begin{tikzpicture}[scale=0.4, transform shape, inside/.style={fill=orange,thin},
outside/.style={fill=yellow,thick},
every node/.style={font=\small}, grid/.style={fill=green,thick}]

%%
\draw[grid](-15,-15,-15)--(15,-15,-15)--(15,8,-15)--(-15,8,-15)--(-15,-15,-15);
\pic at (0, 0, 0) {block};
\pic at (12, 0, 0) {block};
\end{tikzpicture}

\end{document}


Question:

1. As a beginner, I feel my TeX code is suboptimal, how can I improve?
2. How can I add texts just like in the reference image? I know it's silly, but I'm still clueless about it.

Thank you!

• One minor detail you can change is the last coordinate of every draw command to cycle instead of an identical value to the first coordinate. There are some very nice drawings in the pgf-manual that illustrate the difference, in your case it is easy to see in the bottom left corner of the green rectangle. You can see that there is a gap which disappears when you use cycle. Jun 2 at 9:01
• Also, you might want to think about using for-loops for the repetitive lines of code. Jun 2 at 9:02

This is a possibility, there are many more if you combine pics or \newcommand macros (you could make another one for block, for example) or \foreach loops.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\newcommand{\cube}[7]% position (center), size, color, front edges (1/0), back edges (1/0),
{%                      label, y shift for the label
\begin{scope}[shift={#1},scale=0.5]
\draw[fill=#3] (-#2,-#2,-#2) -- (-#2,-#2, #2) -- ( #2,-#2, #2) --
( #2, #2, #2) -- ( #2, #2,-#2) -- (-#2, #2,-#2)  -- cycle;
\ifnum #4 = 1% if we need front edges
\draw ( #2,-#2,-#2) -- (-#2,-#2,-#2);
\draw ( #2,-#2,-#2) -- ( #2, #2,-#2);
\draw ( #2,-#2,-#2) -- ( #2,-#2, #2);
\fi
\ifnum #5 = 1% if we need back edges
\draw (-#2, #2, #2) -- ( #2, #2, #2);
\draw (-#2, #2, #2) -- (-#2,-#2, #2);
\draw (-#2, #2, #2) -- (-#2, #2,-#2);
\fi
\node[blue] at (0,-2*#7,0) {#6};
\end{scope}
}

\begin{document}
\begin{tikzpicture}[line join=round, line cap=round,%
x={(1 cm,0 cm)}, y={(0 cm,-1cm)}, z={(0.5 cm,0.5 cm)}]
\foreach\i in {0,1}
{%
\cube{(5*\i,0,0)}{4}{yellow}{0}{1}{}{0}
\foreach\c in {1,0} \foreach\b in {1,0} \foreach\a in {0,1,2}
{%
\cube{(5*\i+1.2*\a-1.2,1.2*\b-0.6,1.2*\c-0.6)}{1}{orange}{1}{0}{$\a,\b,\c$}{0.5+3*\b}
}
\cube{(5*\i,0,0)}{4}{none}{1}{0}{Block $\i$}{2}
}
\end{tikzpicture}
\end{document}


Edit(s): I added the text nodes inside the macro \cube which reduced the code and made it more legible (I hope). I changed the axes too.

• Such a nice explanation.... Jun 2 at 13:24