How can we make these make kinds of 3D plots easily in LaTex? Is there any easy way available so we can draw this in that and in the end we can get tikz code? Any help would be highly appreciated
1 Answers
A solution with 3d library can be:
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,math,3d}
\newcommand{\myCube}[2]{%
%back face
\begin{scope}[canvas is yz plane at x=#1]
\foreach \Hpnt in {1,...,4} {
\coordinate (bfb-\Hpnt) at (\Hpnt-1,0);
\coordinate (bfu-\Hpnt) at (\Hpnt-1,3);
}
\foreach \Vpnt in {1,2} {
\coordinate (bfr-\Vpnt) at (3,\Vpnt);
\coordinate (bfl-\Vpnt) at (0,\Vpnt);
}
\end{scope}
%front face
\begin{scope}[canvas is yz plane at x=#1+1]
\foreach \Hpnt in {1,...,4} {
\coordinate (ffb-\Hpnt) at (\Hpnt-1,0);
\coordinate (ffu-\Hpnt) at (\Hpnt-1,3);
}
\foreach \Vpnt in {1,2} {
\coordinate (ffr-\Vpnt) at (3,\Vpnt);
\coordinate (ffl-\Vpnt) at (0,\Vpnt);
}
\end{scope}
\draw[fill=#2] (bfu-1) -- (ffu-1) -- (ffb-1) -- (ffb-4) -- (bfb-4) -- (bfu-4) -- cycle;
\foreach \jur in {2,3} {
\draw (bfu-\jur) -- (ffu-\jur) -- (ffb-\jur);
\tikzmath{
integer \jlr;
\jlr=\jur-1;
}
\draw (bfr-\jlr) -- (ffr-\jlr) -- (ffl-\jlr);
}
\draw (ffu-1) -- (ffu-4) -- (ffb-4) (bfu-4) -- (ffu-4);
}
\begin{document}
\begin{tikzpicture}[z={(90:1cm)},y={(-30:1cm)},x={(210:0.75cm)}]
\myCube{-3}{green!50!black}
\node at ($($(ffb-4)!0.5!(bfb-4)$)!2mm!90:(ffb-4)$) {$1$};
\myCube{-1.5}{red!80!black}
\node at ($($(ffb-4)!0.5!(bfb-4)$)!2mm!90:(ffb-4)$) {$1$};
\myCube{0}{orange}
\draw[orange,dashed,very thick] let
\p1=($(current bounding box.south west)!0.5!(current bounding box.north east)$),
\p2=($(current bounding box.south) - (current bounding box.north)$),
\n1={veclen(\x2,\y2)}
in (\p1) circle[radius=\n1/2+6.28mm];
\node at ($($(ffb-4)!0.5!(bfb-4)$)!2mm!90:(ffb-4)$) {$1$};
\node at ($($(ffb-1)!0.5!(ffb-4)$)!2mm!90:(ffb-1)$) {$3$};
\node at ($($(ffb-1)!0.5!(ffu-1)$)!2mm!90:(ffu-1)$) {$3$};
\end{tikzpicture}
\end{document}
Edit
For add more cubes you must add another `\myCube{position}{color} at the code. If you add it at the end of the code then the cubes will be on the foreground, if you add it at the beginning of the code it will be on the background. The position of a cube must be lesser than that of the cube in front of it. For example:
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,math,3d}
\newcommand{\myCube}[2]{%
%back face
\begin{scope}[canvas is yz plane at x=#1]
\foreach \Hpnt in {1,...,4} {
\coordinate (bfb-\Hpnt) at (\Hpnt-1,0);
\coordinate (bfu-\Hpnt) at (\Hpnt-1,3);
}
\foreach \Vpnt in {1,2} {
\coordinate (bfr-\Vpnt) at (3,\Vpnt);
\coordinate (bfl-\Vpnt) at (0,\Vpnt);
}
\end{scope}
%front face
\begin{scope}[canvas is yz plane at x=#1+1]
\foreach \Hpnt in {1,...,4} {
\coordinate (ffb-\Hpnt) at (\Hpnt-1,0);
\coordinate (ffu-\Hpnt) at (\Hpnt-1,3);
}
\foreach \Vpnt in {1,2} {
\coordinate (ffr-\Vpnt) at (3,\Vpnt);
\coordinate (ffl-\Vpnt) at (0,\Vpnt);
}
\end{scope}
\draw[fill=#2] (bfu-1) -- (ffu-1) -- (ffb-1) -- (ffb-4) -- (bfb-4) -- (bfu-4) -- cycle;
\foreach \jur in {2,3} {
\draw (bfu-\jur) -- (ffu-\jur) -- (ffb-\jur);
\tikzmath{
integer \jlr;
\jlr=\jur-1;
}
\draw (bfr-\jlr) -- (ffr-\jlr) -- (ffl-\jlr);
}
\draw (ffu-1) -- (ffu-4) -- (ffb-4) (bfu-4) -- (ffu-4);
}
\begin{document}
\begin{tikzpicture}[z={(90:1cm)},y={(-30:1cm)},x={(210:0.75cm)}]
\myCube{-4.5}{blue}%<- another cube on background
\myCube{-3}{green!50!black}
\node at ($($(ffb-4)!0.5!(bfb-4)$)!2mm!90:(ffb-4)$) {$1$};
\myCube{-1.5}{red!80!black}
\node at ($($(ffb-4)!0.5!(bfb-4)$)!2mm!90:(ffb-4)$) {$1$};
\myCube{0}{orange}
\node at ($($(ffb-4)!0.5!(bfb-4)$)!2mm!90:(ffb-4)$) {$1$};
%\node at ($($(ffb-1)!0.5!(ffb-4)$)!2mm!90:(ffb-1)$) {$3$};%<- label not visible therefore commented
%\node at ($($(ffb-1)!0.5!(ffu-1)$)!2mm!90:(ffu-1)$) {$3$};%<- label not visible therefore commented
\myCube{1.5}{violet}%<- another cube on foreground
\draw[orange,dashed,very thick] let
\p1=($(current bounding box.south west)!0.5!(current bounding box.north east)$),
\p2=($(current bounding box.south) - (current bounding box.north)$),
\n1={veclen(\x2,\y2)}
in (\p1) circle[radius=\n1/2+6.28mm];%<- since the dashed circle is draw using the bounding box of the picture you must shift here this part of the code
\end{tikzpicture}
\end{document}
Edit slanted labels
Changing a little the macro \myCube (see the comment in the code) you can get the following:
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,math,3d}
\def\OffsetLabels{0.7mm}
\newcommand{\myCube}[5]{%
%back face
\begin{scope}[canvas is yz plane at x=#1]
\foreach \Hpnt in {1,...,4} {
\coordinate (bfb-\Hpnt) at (\Hpnt-1,0);
\coordinate (bfu-\Hpnt) at (\Hpnt-1,3);
}
\foreach \Vpnt in {1,2} {
\coordinate (bfr-\Vpnt) at (3,\Vpnt);
\coordinate (bfl-\Vpnt) at (0,\Vpnt);
}
\end{scope}
%front face
\begin{scope}[canvas is yz plane at x=#1+1]
\foreach \Hpnt in {1,...,4} {
\coordinate (ffb-\Hpnt) at (\Hpnt-1,0);
\coordinate (ffu-\Hpnt) at (\Hpnt-1,3);
}
\foreach \Vpnt in {1,2} {
\coordinate (ffr-\Vpnt) at (3,\Vpnt);
\coordinate (ffl-\Vpnt) at (0,\Vpnt);
}
\node[transform shape,anchor=north] at ($($(ffb-1)!0.5!(ffb-4)$)!\OffsetLabels!90:(ffb-1)$) {#3};
\node[transform shape,anchor=east] at ($($(ffb-1)!0.5!(ffu-1)$)!\OffsetLabels!90:(ffu-1)$) {#4};
\end{scope}
\begin{scope}[canvas is yz plane at x=#1+0.5,transform shape]
\node[anchor=west] at ($($(ffb-4)!0.5!(bfb-4)$)!\OffsetLabels!90:(ffb-4)$) {#5};
\end{scope}
\draw[fill=#2] (bfu-1) -- (ffu-1) -- (ffb-1) -- (ffb-4) -- (bfb-4) -- (bfu-4) -- cycle;
\foreach \jur in {2,3} {
\draw (bfu-\jur) -- (ffu-\jur) -- (ffb-\jur);
\tikzmath{
integer \jlr;
\jlr=\jur-1;
}
\draw (bfr-\jlr) -- (ffr-\jlr) -- (ffl-\jlr);
}
\draw (ffu-1) -- (ffu-4) -- (ffb-4) (bfu-4) -- (ffu-4);
}
\begin{document}
\begin{tikzpicture}[z={(90:1cm)},y={(-30:1cm)},x={(210:0.75cm)}]
%macro parameter meaning
%\myCube{position}{color}{bottom label}{left label}{right label}
\myCube{-4.5}{blue}{}{}{blue block}
\myCube{-3}{green!50!black}{}{}{green block}
\myCube{-1.5}{red!80!black}{}{}{red block}
\myCube{0}{orange}{}{}{orange block}
\myCube{1.5}{violet}{3}{3}{violet block}
\draw[orange,dashed,very thick] let
\p1=($(current bounding box.south west)!0.5!(current bounding box.north east)$),
\p2=($(current bounding box.south) - (current bounding box.north)$),
\n1={veclen(\x2,\y2)}
in (\p1) circle[radius=\n1/2+6.28mm];
\end{tikzpicture}
\end{document}
- 3,394
-
Thank you so much for your help but I am just curious that I will have to learn it from zero so I can make some more graphs similar to this one? or there is any other tool in which I can make this and get LaTex code?
Thank you
– Hadi Malik Nov 25 '20 at 17:30 -
I sometimes use TikzEdt, but if you don't know something about PGF/TikZ you can't do many things. If you read the tutorials on the PGFManual you can learn a lot. – vi pa Nov 25 '20 at 17:37
-
Thank you for your help, the last thing, please. Are you available to add a few more similar blocks with it? I tried TikzEdt but this is giving me an error like code 1 etc. I am learning it from PGF Manual but I need an image on an urgent basis. Thank you – Hadi Malik Nov 26 '20 at 06:01
-
@HadiMalik TikzEdt don't load by default the
3dtikzlibrary therefore if you want put the previous code on TikzEdt you must add this library. You must go to Settings>Settings...>Compiler ad then addusetikzlibrary{3d}. Then the code will compile.There are many ways to make the previous figure in TikzEdt the most simple is explained here. – vi pa Nov 26 '20 at 09:43 -
Hi, I am still facing this issue by adding that package in setting directory also.
Couldn't parse code. MismatchedTokenException: Expected token 'tikzpicture'. Instead found "scope" in line 6 at position 7. Compilation failed with exit code 1
– Hadi Malik Nov 26 '20 at 10:25 -
@vipa +1 :) How can we get the text (here "1") tobe slanted ? If you type "block 1" instead of "1" The text gets into the cube. – JeT Nov 26 '20 at 11:14
-
Thank you so much for your help, now can you guide me on how I can get the extreme right (image here in the link) like how we control the lines on blocks?
https://tex.stackexchange.com/questions/572372/3d-plot-in-tikz-latex
– Hadi Malik Nov 26 '20 at 11:49 -




3dlibrary see PGFManual v3.1.7 pag 566 – vi pa Nov 25 '20 at 10:59