3

I'm trying to find the easiest way to draw a 3D cube chain in latex. Help me enter image description here

I know how to draw cube in tikz. For example

\newcommand{\tikzcuboid}[4]{% width, height, depth, scale
\begin{tikzpicture}[scale=#4]
\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 );
}
\end{tikzpicture}
}

\newcommand{\tikzcube}[2]{% length, scale
\tikzcuboid{#1}{#1}{#1}{#2}
}

I find this code in Need help creating a 3D cube from a 2D set of nodes in TikZ

enter image description here

2
  • Welcome to TSE. What did you try? May 14 at 11:42
  • @JoséCarlosSantos I add new info above
    – gkndy
    May 14 at 11:48

2 Answers 2

4

Here's a tikz solution. You can change\cubesAmount to draw more or less cubes.

enter image description here

\documentclass{article}
\usepackage{tikz}

\begin{document}
 
\begin{tikzpicture}[z={(0.5,0.5)}]

\def\cubesAmount{3}
\foreach \i in {1,...,\cubesAmount}{
    \draw (\i-1,\i-1,\i-1) rectangle +(1,1,0) -- ++(0,1,0) -- ++(0,0,1) -- ++(1,0,0) edge +(0,0,-1) -- ++(0,-1,0) -- ++(0,0,-1);
    \ifnum\i<\cubesAmount
        \node[anchor=north west] at (\i,\i,\i) {$(\i,\i,\i)$};
    \fi
}
\node[anchor=north east] at (0,0,0){$(0,0,0)$};
\node[anchor=south west] at (\cubesAmount,\cubesAmount,\cubesAmount){$(\cubesAmount,\cubesAmount,\cubesAmount)$};
 
\end{tikzpicture}
 
\end{document}

Also, if you want to change the perspective, you can tweak around z={(yaw,pitch)}

enter image description here

5

Update: One can include the asymptote code in a LaTex document. It is something like this

\documentclass{article}
\usepackage{asymptote}
\begin{document}
\begin{asy}
// can be directly run on http://asymptote.ualberta.ca/
<asymptote code>        
\end{asy}
\end{document}

My suggestion is using Asymptote, then everything is available, built-in: unitcube is a surface; unitbox is an array of paths/segments in the 3D space. We are free to choose projection/point of view, say (0,-2,1) in this case.

enter image description here

// http://asymptote.ualberta.ca/
import three;
size(5cm);
currentprojection=orthographic(0,-2,1,center=true,zoom=.8);
path3[] p=unitbox;
//surface p=unitcube;
draw(p,red);
draw(shift(1,1,1)*p,blue);
draw(shift(2,2,2)*p,magenta);

With surface p=unitcube;

enter image description here

or this one

enter image description here

// http://asymptote.ualberta.ca/
import three;
size(5cm);
currentprojection=orthographic(0,-2,1,center=true,zoom=.8);
path3[] p=unitbox;
surface q=unitcube;

draw(q,red+opacity(.1));
draw(shift(1,1,1)*q,blue+opacity(.1));
draw(shift(2,2,2)*q,magenta+opacity(.1));

draw(p,red);
draw(shift(1,1,1)*p,blue);
draw(shift(2,2,2)*p,magenta);
2
  • 2
    Can you add some information how one can include the asymptote-Code in a LaTex document. This could help the OP and other users who find this answer.
    – Unknown
    May 14 at 14:04
  • 1
    Yeah! I added a link for that
    – Black Mild
    May 14 at 14: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.