The following LaTeX code, which is modelled after this answer, was saved in ~/Test.tex.
\documentclass[tikz,border=1cm]{standalone}
\newcommand{\tikzcube}[2][1]{% scale, side length
\begin{tikzpicture}[scale=#1]
\foreach \x in {0,...,#2}
{
\draw (\x,0,#2) -- (\x,#2,#2);
\draw (\x,#2,#2) -- (\x,#2,0);
}
\foreach \x in {0,...,#2}
{
\draw (#2,\x,#2) -- (#2,\x,0);
\draw (0,\x,#2) -- (#2,\x,#2);
}
\foreach \x in {0,...,#2}
{
\draw (#2,0,\x) -- (#2,#2,\x);
\draw (0,#2,\x) -- (#2,#2,\x);
}
\end{tikzpicture}%
}
\begin{document}
\tikzcube{3}
\end{document}
Then the following commands were executed in the Terminal.
> cd ~
> lualatex Test
Consequently the file ~/Test.pdf was created. When opened in a PDF viewer, the file displayed as follows.
Consider this cube to be built from three levels that are arranged in a tower, one on top of the other.
I'd like to be able to specify a color and a level, and for the corresponding level to be colored with the specified color.
The cube should be considered opaque, and therefore the color should only affect the external walls. Consequently, when the top level is specified, the color should affect the two visible sides as well as the "roof", however when one of the other levels is specified, the color should affect only the sides.
The cube needs not have 3 levels; it can have any number of them: 1, 2, 3, 4, 5, etc. No matter how many levels it has, it remains a cube.


