# Draw 3D cube with additional rectangles

I'd like to draw a cube with additional rectangles in it (see pic below). The colors are secundary, as I will have to change them anyway. I've tried it in 2D just by meassuring the distances of the cube, but that didn't work. With other questions in the forum I was able to draw at least a cube with tikz, but my biggest problem is how I get the rectangles on the sides of the cube.

Welcome! This can be done with 3d library and e.g. tikz-3dplot.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{70}{130}
\begin{tikzpicture}[tdplot_main_coords,line join=round]
\begin{scope}[canvas is xy plane at z=1]
\draw[fill=green!60!black] (-1,-1) rectangle (1,1);
\draw[fill=green] (-1,-0.5) rectangle (1,0.5);
\end{scope}
\begin{scope}[canvas is xz plane at y=1]
\draw[fill=green!60!black] (-1,-1) rectangle (1,1);
\draw[fill=white] (0,0.6) rectangle (0.6,0);
\end{scope}
\begin{scope}[canvas is yz plane at x=1]
\draw[fill=green!60!black] (-1,-1) rectangle (1,1);
\draw[fill=green] (-0.5,0.6) rectangle (0.5,1);
\draw[fill=green] (-0.5,-0.6) rectangle (0.5,-1);
\end{scope}
\end{tikzpicture}
\end{document}


Or you can use the perspective library if you want more realistic projections.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{perspective}
\tikzset{xy plane at z/.style={insert path={
(tpp cs:x=\xmin,y=\ymin,z=#1) -- (tpp cs:x=\xmax,y=\ymin,z=#1)
-- (tpp cs:x=\xmax,y=\ymax,z=#1) --  (tpp cs:x=\xmin,y=\ymax,z=#1) -- cycle}},
xz plane at y/.style={insert path={
(tpp cs:x=\xmin,y=#1,z=\zmin) -- (tpp cs:x=\xmax,y=#1,z=\zmin)
-- (tpp cs:x=\xmax,y=#1,z=\zmax) --  (tpp cs:x=\xmin,y=#1,z=\zmax) -- cycle}},
yz plane at x/.style={insert path={
(tpp cs:x=#1,y=\ymin,z=\zmin) -- (tpp cs:x=#1,y=\ymin,z=\zmax)
-- (tpp cs:x=#1,y=\ymax,z=\zmax) --  (tpp cs:x=#1,y=\ymax,z=\zmin) -- cycle}},
x domain/.code args={#1:#2}{\def\xmin{#1}\def\xmax{#2}},x domain=-1:1,
y domain/.code args={#1:#2}{\def\ymin{#1}\def\ymax{#2}},y domain=-1:1,
z domain/.code args={#1:#2}{\def\zmin{#1}\def\zmax{#2}},z domain=-1:1}
\begin{document}
\begin{tikzpicture}[scale=2,isometric view,line join=round]
% draw the sides
\draw[fill=green!60!black,xy plane at z=1];
\draw[fill=green!60!black,xz plane at y=-1];
\draw[fill=green!60!black,yz plane at x=-1];
% draw rectangles on the sides
\draw[fill=green,y domain=-0.5:0.5,xy plane at z=1];
\draw[fill=green,y domain=-0.5:0.5,z domain=0.7:1,yz plane at x=-1];
\draw[fill=green,y domain=-0.5:0.5,z domain=-0.7:-1,yz plane at x=-1];
\draw[fill=white,x domain=-0.6:0,z domain=0:0.6,xz plane at y=-1];
\end{tikzpicture}
\end{document}


Then there is the flexibility to change the 3d view (this is the analogon of \tdplotsetmaincoords{70}{130} in the first example) and the perspective with perspective={...}, which has no analogon in the first example because this feature is not supported by tikz-3dplot. A perhaps exaggerated illustration of the perspective parameters do is

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{perspective}
\tikzset{xy plane at z/.style={insert path={
(tpp cs:x=\xmin,y=\ymin,z=#1) -- (tpp cs:x=\xmax,y=\ymin,z=#1)
-- (tpp cs:x=\xmax,y=\ymax,z=#1) --  (tpp cs:x=\xmin,y=\ymax,z=#1) -- cycle}},
xz plane at y/.style={insert path={
(tpp cs:x=\xmin,y=#1,z=\zmin) -- (tpp cs:x=\xmax,y=#1,z=\zmin)
-- (tpp cs:x=\xmax,y=#1,z=\zmax) --  (tpp cs:x=\xmin,y=#1,z=\zmax) -- cycle}},
yz plane at x/.style={insert path={
(tpp cs:x=#1,y=\ymin,z=\zmin) -- (tpp cs:x=#1,y=\ymin,z=\zmax)
-- (tpp cs:x=#1,y=\ymax,z=\zmax) --  (tpp cs:x=#1,y=\ymax,z=\zmin) -- cycle}},
x domain/.code args={#1:#2}{\def\xmin{#1}\def\xmax{#2}},x domain=-1:1,
y domain/.code args={#1:#2}{\def\ymin{#1}\def\ymax{#2}},y domain=-1:1,
z domain/.code args={#1:#2}{\def\zmin{#1}\def\zmax{#2}},z domain=-1:1}
\begin{document}
\begin{tikzpicture}[scale=2,3d view={-40}{30},line join=round,
perspective={p = {(16,0,0)}, q = {(0,10,0)},r={(0,0,14)}}
]
\draw[fill=green!60!black,xy plane at z=1];
\draw[fill=green!60!black,xz plane at y=-1];
\draw[fill=green!60!black,yz plane at x=-1];
%
\draw[fill=green,y domain=-0.5:0.5,xy plane at z=1];
\draw[fill=green,y domain=-0.5:0.5,z domain=0.8:1,yz plane at x=-1];
\draw[fill=green,y domain=-0.5:0.5,z domain=-0.8:-1,yz plane at x=-1];
\draw[fill=white,x domain=-0.6:0,z domain=0:0.6,xz plane at y=-1];
\end{tikzpicture}
\end{document}


• Thanks so much for the help. I'd like to implement this cube in a circuit with CircuiTiX. Do you know any way how to put it in a Circuit at different positions with different sizes? I have tried the \newcommand, which works fine, but not in the \begin{circuitix}-enviroment. In the \newcommand I've added the option to change the color. so mayby it is also possible to change the size. What I'm currently using are just the pdfs of the generated cubes in different colors, but mayby by implementing the code it is nicer?
– Bene
Dec 12, 2019 at 14:10
• @Bene Unfortunately I do not understand what you have tried. Could you please ask a new, separate question for that, which will be seen by circuitikz experts?
– user194703
Dec 12, 2019 at 14:17
• Yes I will do so!
– Bene
Dec 12, 2019 at 15:36