Repeat geometry / body / object / shape in PGFplots

I depicted a pentahedron by means of the code

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset
{
}

% Plot pentahedron.
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xlabel=$x$,
ylabel=$y$,
zlabel=$z$,
view={45}{20},
colorbar,
]

\newcommand{\Ax}{-1/sqrt(3)}
\newcommand{\Ay}{-1}
\newcommand{\Bx}{-2/sqrt(3)}
\newcommand{\By}{-2}
\newcommand{\Cx}{0}
\newcommand{\Cy}{-2}
\newcommand{\ABCz}{1}
\addplot3[plotOptsA] coordinates{(\Ax,\Ay,0) (\Bx,\By,0) (\Cx,\Cy,0)}; % x, xy ground plane
\addplot3[plotOptsA] coordinates{(\Ax,\Ay,\ABCz) (\Bx,\By,\ABCz) (\Cx,\Cy,\ABCz)}; % x xy plane
\addplot3[plotOptsB] coordinates{(\Ax,\Ay,0) (\Bx,\By,0) (\Bx,\By,\ABCz) (\Ax,\Ay,\ABCz)}; % x, xz plane
\addplot3[plotOptsB] coordinates{(\Bx,\By,0) (\Cx,\Cy,0) (\Cx,\Cy,\ABCz) (\Bx,\By,\ABCz)}; % x, xz plane
\addplot3[plotOptsB] coordinates{(\Ax,\Ay,0) (\Cx,\Cy,0) (\Cx,\Cy,\ABCz) (\Ax,\Ay,\ABCz)}; % x, xz plane
\end{axis}
\end{tikzpicture}
\end{document}


The result looks like I want to efficiently repeat this pentahedron many times but for different \Ax, \Ay, \Bx, \By, \Cx, \Cy and \ABCz, but I do not know what is a good way to do this in LaTeX / / PGF / TikZ / PGFplots.

In Matlab, I would use the code

% Clean up.
clear all;
clear global;
close all;
clc;
format compact;

% Figure definitions.
figure;
ha=gca;
az=225;
el=10;
view(az,el);

ABCz=1;
for yn=1:4
Ay=-1*(yn-1);
By=-yn;
Cy=-yn;
for q=-(yn-1):2:(yn-1)
Ax=q/sqrt(3);
Bx=Ax-1/sqrt(3);
Cx=Ax+1/sqrt(3);
pentahedron(Ax,Ay,Bx,By,Cx,Cy,ABCz);
ABCz=ABCz+1;
end
end

xlabel('x');
ylabel('y');
zlabel('z');


where the function pentahedron is defined by

function pentahedron(Ax,Ay,Bx,By,Cx,Cy,ABCz)

X=[Ax;Bx;Cx];Y=[Ay;By;Cy];Z=[0;0;0];fill3(X,Y,Z,Z);
X=[Ax;Bx;Cx];Y=[Ay;By;Cy];Z=[ABCz;ABCz;ABCz];fill3(X,Y,Z,Z);
X=[Ax;Bx;Bx;Ax];Y=[Ay;By;By;Ay];Z=[0;0;ABCz;ABCz];fill3(X,Y,Z,Z);
X=[Bx;Cx;Cx;Bx];Y=[By;Cy;Cy;By];Z=[0;0;ABCz;ABCz];fill3(X,Y,Z,Z);
X=[Ax;Cx;Cx;Ax];Y=[Ay;Cy;Cy;Ay];Z=[0;0;ABCz;ABCz];fill3(X,Y,Z,Z);


, to repeat the pentahedron. Is it perhaps possible to construct some macro or a new environment in LaTeX to mimic this Matlab function?

• I have removed some of the tags as they are not related to the question but maybe to the answer. However I think you should move to table solutions since as you encounter there is very little you can do about coordinates syntax. – percusse Mar 19 '15 at 11:29
• What about a \newcommand with arguments and you pass \Ax \Ay etc as arguments? – remus Mar 19 '15 at 11:55
• @percusse What would the table have to offer which coordinates does not? Nonetheless, I wonder if this is really an issue. Repeating a geometry like this in Matlab is trivial. One just has to write a function. So I guess the question is how such a function should be written in LaTeX. – Adriaan Mar 19 '15 at 12:32
• Then you can just plot all entries of a table with one command instead of reading line by line which you emulate here. I'm too biased to comment on matlab side of things. You can include your matlab function and we can translate it if you wish. – percusse Mar 19 '15 at 12:41
• @percusse As per your request, I just added the Matlab code. – Adriaan Mar 19 '15 at 15:16

A direct translation of your matlab script can be as the following

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset
{
}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xmin=-4/sqrt(3),
xmax=4/sqrt(3),
ymin=-4,
ymax=0,
zmin=0,
view={225}{10}
]
\foreach\yn
[
evaluate=
{
\Ay=int(1-\yn);
\By=-\yn;
\Cy=-\yn;%
}
] in {1,...,4}
{
\foreach\q
[
evaluate=
{
\Ax=(1/sqrt(3)*\q);
\Bx=\Ax-(1/sqrt(3));
\Cx=\Ax+(1/sqrt(3));%
},
count=\ABCz
] in{\Ay,\numexpr\Ay+2,...,-\Ay}
{

I hope it was supposed to look like this • Sorry for that :(. Everybody has their own style I guess ;). Btw, I think there is still an error in {\Ay,\numexpr\Ay+2,...,-\Ay}, since the pentahedron are not drawn properly. One can see this by executing the Matlab script. That's how it's supposed to be. The q should be element of -(yn-1):2:(yn-1)=\Ay,\Ay+2,...,-\Ay where apparently you need the command \numexpr. Somehow this is going wrong. Do you know how to fix it? – Adriaan Mar 20 '15 at 11:58