3

I would like to represent a pallet. The pallet is a basement of 0.15m height, 0.8m wide and 1.2m depth and above there are boxes, which I represent with a big box.

I have used the code in "How to scale down a group of tikzpictures and label texts independently",

\documentclass[border=3mm,tikz]{standalone}
\usepackage{tikz}

\newif\ifcuboidshade
\newif\ifcuboidemphedge

\tikzset{
  cuboid/.is family,
  cuboid,
  shiftx/.initial=0,
  shifty/.initial=0,
  dimx/.initial=3,
  dimy/.initial=3,
  dimz/.initial=3,
  scale/.initial=1,
  densityx/.initial=1,
  densityy/.initial=1,
  densityz/.initial=1,
  rotation/.initial=0,
  anglex/.initial=0,
  angley/.initial=90,
  anglez/.initial=225,
  scalex/.initial=1,
  scaley/.initial=1,
  scalez/.initial=0.5,
  xedgelabel/.store in=\xedgelabel,
  yedgelabel/.store in=\yedgelabel,
  zedgelabel/.store in=\zedgelabel,
  xedgelabel={},
  yedgelabel={},
  zedgelabel={},
  edgelabel/.style={},
  front/.style={draw=black,fill=white},
  top/.style={draw=black,fill=white},
  right/.style={draw=black,fill=white},
  shade/.is if=cuboidshade,
  shadecolordark/.initial=black,
  shadecolorlight/.initial=white,
  shadeopacity/.initial=0.15,
  shadesamples/.initial=16,
  emphedge/.is if=cuboidemphedge,
  emphstyle/.style={thick},
}

\newcommand{\tikzcuboidkey}[1]{\pgfkeysvalueof{/tikz/cuboid/#1}}

% Commands
\newcommand{\tikzcuboid}[1]{
    \tikzset{cuboid,#1} % Process Keys passed to command
  \pgfmathsetlengthmacro{\vectorxx}{\tikzcuboidkey{scalex}*cos(\tikzcuboidkey{anglex})*28.452756}
  \pgfmathsetlengthmacro{\vectorxy}{\tikzcuboidkey{scalex}*sin(\tikzcuboidkey{anglex})*28.452756}
  \pgfmathsetlengthmacro{\vectoryx}{\tikzcuboidkey{scaley}*cos(\tikzcuboidkey{angley})*28.452756}
  \pgfmathsetlengthmacro{\vectoryy}{\tikzcuboidkey{scaley}*sin(\tikzcuboidkey{angley})*28.452756}
  \pgfmathsetlengthmacro{\vectorzx}{\tikzcuboidkey{scalez}*cos(\tikzcuboidkey{anglez})*28.452756}
  \pgfmathsetlengthmacro{\vectorzy}{\tikzcuboidkey{scalez}*sin(\tikzcuboidkey{anglez})*28.452756}
  \begin{scope}[xshift=\tikzcuboidkey{shiftx}, yshift=\tikzcuboidkey{shifty}, scale=\tikzcuboidkey{scale}, rotate=\tikzcuboidkey{rotation}, x={(\vectorxx,\vectorxy)}, y={(\vectoryx,\vectoryy)}, z={(\vectorzx,\vectorzy)}]
    \pgfmathsetmacro{\steppingx}{1/\tikzcuboidkey{densityx}}
  \pgfmathsetmacro{\steppingy}{1/\tikzcuboidkey{densityy}}
  \pgfmathsetmacro{\steppingz}{1/\tikzcuboidkey{densityz}}
  \newcommand{\dimx}{\tikzcuboidkey{dimx}}
  \newcommand{\dimy}{\tikzcuboidkey{dimy}}
  \newcommand{\dimz}{\tikzcuboidkey{dimz}}
  \pgfmathsetmacro{\secondx}{2*\steppingx}
  \pgfmathsetmacro{\secondy}{2*\steppingy}
  \pgfmathsetmacro{\secondz}{2*\steppingz}
  \foreach \x in {\steppingx,\secondx,...,\dimx}
  {\ifnum\dimy=1
   \def\lsty{\dimy}
   \else
   \def\lsty{\steppingy,\secondy,...,\dimy}
   \fi
    \foreach \y in \lsty
    {   \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
      \pgfmathsetmacro{\lowy}{(\y-\steppingy)}
      \filldraw[cuboid/front] (\lowx,\lowy,\dimz) -- (\lowx,\y,\dimz) -- (\x,\y,\dimz) -- (\x,\lowy,\dimz) -- cycle;
    }
    }
  \ifnum\dimx=1
  \def\lstx{\dimx}
  \else
  \def\lstx{\steppingx,\secondx,...,\dimx}
  \fi
  \foreach \x in \lstx
  { \ifnum\dimz=1
   \def\lstz{\dimz}
   \else
   \def\lstz{\steppingz,\secondz,...,\dimz}
   \fi
    \foreach \z in \lstz
    {   \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
      \pgfmathsetmacro{\lowz}{(\z-\steppingz)}
      \filldraw[cuboid/top] (\lowx,\dimy,\lowz) -- (\lowx,\dimy,\z) -- (\x,\dimy,\z) -- (\x,\dimy,\lowz) -- cycle;
        }
    }
    \ifnum\dimy=1
    \def\lsty{\dimy}
    \else
    \def\lsty{\steppingy,\secondy,...,\dimy}
    \fi
    \foreach \y in \lsty
  { \foreach \z in {\steppingz,\secondz,...,\dimz}
    {   \pgfmathsetmacro{\lowy}{(\y-\steppingy)}
      \pgfmathsetmacro{\lowz}{(\z-\steppingz)}
      \filldraw[cuboid/right] (\dimx,\lowy,\lowz) -- (\dimx,\lowy,\z) -- (\dimx,\y,\z) -- (\dimx,\y,\lowz) -- cycle;
    }
  }
    \path (0,0,\dimz) -- (0,\dimy,\dimz) node[midway,above,edgelabel]{\yedgelabel};
    \path (0,0,\dimz) -- (\dimx,0,\dimz) node[midway,below,edgelabel]{\xedgelabel};
    \path (\dimx,0,\dimz) -- (\dimx,0,0) node[midway,below,edgelabel]{\zedgelabel};
  \ifcuboidemphedge
    \draw[cuboid/emphstyle] (0,\dimy,0) -- (\dimx,\dimy,0) -- (\dimx,\dimy,\dimz) -- (0,\dimy,\dimz) -- cycle;%
    \draw[cuboid/emphstyle] (0,\dimy,\dimz) -- (0,0,\dimz) -- (\dimx,0,\dimz) -- (\dimx,\dimy,\dimz);%
    \draw[cuboid/emphstyle] (\dimx,\dimy,0) -- (\dimx,0,0) -- (\dimx,0,\dimz);%
    \fi
    \ifcuboidshade
    \pgfmathsetmacro{\cstepx}{\dimx/\tikzcuboidkey{shadesamples}}
    \pgfmathsetmacro{\cstepy}{\dimy/\tikzcuboidkey{shadesamples}}
    \pgfmathsetmacro{\cstepz}{\dimz/\tikzcuboidkey{shadesamples}}
    \foreach \s in {1,...,\tikzcuboidkey{shadesamples}}
    {   \pgfmathsetmacro{\lows}{\s-1}
        \pgfmathsetmacro{\cpercent}{(\lows)/(\tikzcuboidkey{shadesamples}-1)*100}
        \fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (0,\s*\cstepy,\dimz) -- (\s*\cstepx,\s*\cstepy,\dimz) -- (\s*\cstepx,0,\dimz) -- (\lows*\cstepx,0,\dimz) -- (\lows*\cstepx,\lows*\cstepy,\dimz) -- (0,\lows*\cstepy,\dimz) -- cycle;
        \fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (0,\dimy,\s*\cstepz) -- (\s*\cstepx,\dimy,\s*\cstepz) -- (\s*\cstepx,\dimy,0) -- (\lows*\cstepx,\dimy,0) -- (\lows*\cstepx,\dimy,\lows*\cstepz) -- (0,\dimy,\lows*\cstepz) -- cycle;
        \fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (\dimx,0,\s*\cstepz) -- (\dimx,\s*\cstepy,\s*\cstepz) -- (\dimx,\s*\cstepy,0) -- (\dimx,\lows*\cstepy,0) -- (\dimx,\lows*\cstepy,\lows*\cstepz) -- (\dimx,0,\lows*\cstepz) -- cycle;
    }
    \fi 

  \end{scope}
}

\makeatother

\begin{document}


\begin{tikzpicture}[scale=1.6]

    \tikzcuboid{%
    shiftx=0cm,%
    shifty=0cm,%
    scale=1.00,%
    rotation=0,%
    densityx=1,%
    densityy=1,%
    densityz=1,%
    dimx=1,%
    dimy=1,%
    dimz=1,%
    front/.style={draw=brown!75!black,fill=brown!25!white},%
    right/.style={draw=brown!25!black,fill=brown!60!white},%
      top/.style={draw=brown!50!black,fill=brown!30!white},%
    % anglex=-7,%
    anglex=0,%
    angley=90,%
    anglez=205,%
    scalex=1.5,%
    scaley=3,%
    scalez=1.2,%
    emphedge=false,%
    shade,%
    shadeopacity=0.15,%
    % xedgelabel={0.8 m},%
    yedgelabel={2 m},%
%     zedgelabel={1.2 m},%
    /tikz/edgelabel/.style={sloped,scale=0.7,transform shape},%
    }


\end{tikzpicture}

\begin{tikzpicture}[scale=1.6]
        \tikzcuboid{%
        shiftx=0cm,%
        shifty=0cm,%
        scale=1.00,%
        rotation=0,%
        densityx=1,%
        densityy=1,%
        densityz=1,%
        dimx=1,%
        dimy=1,%
        dimz=1,%
        front/.style={draw=brown!75!black,fill=brown!55!white},%
        right/.style={draw=brown!25!black,fill=brown!90!white},%
          top/.style={draw=brown!50!black,fill=brown!60!white},%
        anglex=0,%
        angley=90,%
        anglez=205,%
        scalex=1.5,%
        scaley=0.2,%
        scalez=1.2,%
        emphedge=false,%
        shade,%
        shadeopacity=0.15,%
        xedgelabel={0.8 m},%
        yedgelabel={0.15 m},%
        zedgelabel={1.2 m},%
        /tikz/edgelabel/.style={sloped,scale=0.7,transform shape},%
        }
\end{tikzpicture}

%   \caption{Cuboid}
%   \label{fig:block}
% \end{figure}

\end{document}

enter image description here I do not know how to remove the sides that do not belong to the cuboids and how to place the big box (cuboid) on the small cuboid (pallet)

4

My bad, sorry! When I gave the previous answer, I missed the fact that there are actually two \x loops. Correspondingly, I only adjusted one to also allow one cube in x direction. In your previous post, this did not matter, but here it does. Other than that you can just add the larger cuboid on top of the smaller one by drawing it last and shifting it up.

\documentclass[border=3mm,tikz]{standalone}
\usepackage{tikz}

\newif\ifcuboidshade
\newif\ifcuboidemphedge

\tikzset{
  cuboid/.is family,
  cuboid,
  shiftx/.initial=0,
  shifty/.initial=0,
  dimx/.initial=3,
  dimy/.initial=3,
  dimz/.initial=3,
  scale/.initial=1,
  densityx/.initial=1,
  densityy/.initial=1,
  densityz/.initial=1,
  rotation/.initial=0,
  anglex/.initial=0,
  angley/.initial=90,
  anglez/.initial=225,
  scalex/.initial=1,
  scaley/.initial=1,
  scalez/.initial=0.5,
  xedgelabel/.store in=\xedgelabel,
  yedgelabel/.store in=\yedgelabel,
  zedgelabel/.store in=\zedgelabel,
  xedgelabel={},
  yedgelabel={},
  zedgelabel={},
  edgelabel/.style={},
  front/.style={draw=black,fill=white},
  top/.style={draw=black,fill=white},
  right/.style={draw=black,fill=white},
  shade/.is if=cuboidshade,
  shadecolordark/.initial=black,
  shadecolorlight/.initial=white,
  shadeopacity/.initial=0.15,
  shadesamples/.initial=16,
  emphedge/.is if=cuboidemphedge,
  emphstyle/.style={thick},
}

\newcommand{\tikzcuboidkey}[1]{\pgfkeysvalueof{/tikz/cuboid/#1}}

% Commands
\newcommand{\tikzcuboid}[1]{
    \tikzset{cuboid,#1} % Process Keys passed to command
  \pgfmathsetlengthmacro{\vectorxx}{\tikzcuboidkey{scalex}*cos(\tikzcuboidkey{anglex})*28.452756}
  \pgfmathsetlengthmacro{\vectorxy}{\tikzcuboidkey{scalex}*sin(\tikzcuboidkey{anglex})*28.452756}
  \pgfmathsetlengthmacro{\vectoryx}{\tikzcuboidkey{scaley}*cos(\tikzcuboidkey{angley})*28.452756}
  \pgfmathsetlengthmacro{\vectoryy}{\tikzcuboidkey{scaley}*sin(\tikzcuboidkey{angley})*28.452756}
  \pgfmathsetlengthmacro{\vectorzx}{\tikzcuboidkey{scalez}*cos(\tikzcuboidkey{anglez})*28.452756}
  \pgfmathsetlengthmacro{\vectorzy}{\tikzcuboidkey{scalez}*sin(\tikzcuboidkey{anglez})*28.452756}
  \begin{scope}[xshift=\tikzcuboidkey{shiftx}, yshift=\tikzcuboidkey{shifty}, scale=\tikzcuboidkey{scale}, rotate=\tikzcuboidkey{rotation}, x={(\vectorxx,\vectorxy)}, y={(\vectoryx,\vectoryy)}, z={(\vectorzx,\vectorzy)}]
    \pgfmathsetmacro{\steppingx}{1/\tikzcuboidkey{densityx}}
  \pgfmathsetmacro{\steppingy}{1/\tikzcuboidkey{densityy}}
  \pgfmathsetmacro{\steppingz}{1/\tikzcuboidkey{densityz}}
  \newcommand{\dimx}{\tikzcuboidkey{dimx}}
  \newcommand{\dimy}{\tikzcuboidkey{dimy}}
  \newcommand{\dimz}{\tikzcuboidkey{dimz}}
  \pgfmathsetmacro{\secondx}{2*\steppingx}
  \pgfmathsetmacro{\secondy}{2*\steppingy}
  \pgfmathsetmacro{\secondz}{2*\steppingz}
  \ifnum\dimx=1
  \def\lstx{\dimx}
  \else
  \def\lstx{\steppingx,\secondx,...,\dimx}
  \fi
  \foreach \x in \lstx
  {\ifnum\dimy=1
   \def\lsty{\dimy}
   \else
   \def\lsty{\steppingy,\secondy,...,\dimy}
   \fi
    \foreach \y in \lsty
    {   \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
      \pgfmathsetmacro{\lowy}{(\y-\steppingy)}
      \filldraw[cuboid/front] (\lowx,\lowy,\dimz) -- (\lowx,\y,\dimz) -- (\x,\y,\dimz) -- (\x,\lowy,\dimz) -- cycle;
    }
    }
  \ifnum\dimx=1
  \def\lstx{\dimx}
  \else
  \def\lstx{\steppingx,\secondx,...,\dimx}
  \fi
  \foreach \x in \lstx
  { \ifnum\dimz=1
   \def\lstz{\dimz}
   \else
   \def\lstz{\steppingz,\secondz,...,\dimz}
   \fi
    \foreach \z in \lstz
    {   \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
      \pgfmathsetmacro{\lowz}{(\z-\steppingz)}
      \filldraw[cuboid/top] (\lowx,\dimy,\lowz) -- (\lowx,\dimy,\z) -- (\x,\dimy,\z) -- (\x,\dimy,\lowz) -- cycle;
        }
    }
    \ifnum\dimy=1
    \def\lsty{\dimy}
    \else
    \def\lsty{\steppingy,\secondy,...,\dimy}
    \fi
    \foreach \y in \lsty
  { \ifnum\dimz=1
    \def\lstz{\dimz}
    \else
    \def\lstz{\steppingz,\secondz,...,\dimz}
    \fi
    \foreach \z in \lstz
    {   \pgfmathsetmacro{\lowy}{(\y-\steppingy)}
      \pgfmathsetmacro{\lowz}{(\z-\steppingz)}
      \filldraw[cuboid/right] (\dimx,\lowy,\lowz) -- (\dimx,\lowy,\z) -- (\dimx,\y,\z) -- (\dimx,\y,\lowz) -- cycle;
    }
  }
    \path (0,0,\dimz) -- (0,\dimy,\dimz) node[midway,above,edgelabel]{\yedgelabel};
    \path (0,0,\dimz) -- (\dimx,0,\dimz) node[midway,below,edgelabel]{\xedgelabel};
    \path (\dimx,0,\dimz) -- (\dimx,0,0) node[midway,below,edgelabel]{\zedgelabel};
  \ifcuboidemphedge
    \draw[cuboid/emphstyle] (0,\dimy,0) -- (\dimx,\dimy,0) -- (\dimx,\dimy,\dimz) -- (0,\dimy,\dimz) -- cycle;%
    \draw[cuboid/emphstyle] (0,\dimy,\dimz) -- (0,0,\dimz) -- (\dimx,0,\dimz) -- (\dimx,\dimy,\dimz);%
    \draw[cuboid/emphstyle] (\dimx,\dimy,0) -- (\dimx,0,0) -- (\dimx,0,\dimz);%
    \fi
    \ifcuboidshade
    \pgfmathsetmacro{\cstepx}{\dimx/\tikzcuboidkey{shadesamples}}
    \pgfmathsetmacro{\cstepy}{\dimy/\tikzcuboidkey{shadesamples}}
    \pgfmathsetmacro{\cstepz}{\dimz/\tikzcuboidkey{shadesamples}}
    \foreach \s in {1,...,\tikzcuboidkey{shadesamples}}
    {   \pgfmathsetmacro{\lows}{\s-1}
        \pgfmathsetmacro{\cpercent}{(\lows)/(\tikzcuboidkey{shadesamples}-1)*100}
        \fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (0,\s*\cstepy,\dimz) -- (\s*\cstepx,\s*\cstepy,\dimz) -- (\s*\cstepx,0,\dimz) -- (\lows*\cstepx,0,\dimz) -- (\lows*\cstepx,\lows*\cstepy,\dimz) -- (0,\lows*\cstepy,\dimz) -- cycle;
        \fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (0,\dimy,\s*\cstepz) -- (\s*\cstepx,\dimy,\s*\cstepz) -- (\s*\cstepx,\dimy,0) -- (\lows*\cstepx,\dimy,0) -- (\lows*\cstepx,\dimy,\lows*\cstepz) -- (0,\dimy,\lows*\cstepz) -- cycle;
        \fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (\dimx,0,\s*\cstepz) -- (\dimx,\s*\cstepy,\s*\cstepz) -- (\dimx,\s*\cstepy,0) -- (\dimx,\lows*\cstepy,0) -- (\dimx,\lows*\cstepy,\lows*\cstepz) -- (\dimx,0,\lows*\cstepz) -- cycle;
    }
    \fi 

  \end{scope}
}

\makeatother

\begin{document}



\begin{tikzpicture}[scale=1.6]
        \tikzcuboid{%
        shiftx=0cm,%
        shifty=0cm,%
        scale=1.00,%
        rotation=0,%
        densityx=1,%
        densityy=1,%
        densityz=1,%
        dimx=1,%
        dimy=1,%
        dimz=1,%
        front/.style={draw=brown!75!black,fill=brown!55!white},%
        right/.style={draw=brown!25!black,fill=brown!90!white},%
          top/.style={draw=brown!50!black,fill=brown!60!white},%
        anglex=0,%
        angley=90,%
        anglez=205,%
        scalex=1.5,%
        scaley=0.2,%
        scalez=1.2,%
        emphedge=false,%
        shade,%
        shadeopacity=0.15,%
        xedgelabel={0.8 m},%
        yedgelabel={0.15 m},%
        zedgelabel={1.2 m},%
        /tikz/edgelabel/.style={sloped,scale=0.7,transform shape},%
        }
% big cuboid
\begin{scope}[yshift=2mm]
    \tikzcuboid{%
    shiftx=0cm,%
    shifty=0cm,%
    scale=1.00,%
    rotation=0,%
    densityx=1,%
    densityy=1,%
    densityz=1,%
    dimx=1,%
    dimy=1,%
    dimz=1,%
    front/.style={draw=brown!75!black,fill=brown!25!white},%
    right/.style={draw=brown!25!black,fill=brown!60!white},%
      top/.style={draw=brown!50!black,fill=brown!30!white},%
    % anglex=-7,%
    anglex=0,%
    angley=90,%
    anglez=205,%
    scalex=1.5,%
    scaley=3,%
    scalez=1.2,%
    emphedge=false,%
    shade,%
    shadeopacity=0.15,%
    xedgelabel={},%
    yedgelabel={2 m},%
    zedgelabel={},%
    /tikz/edgelabel/.style={sloped,scale=0.7,transform shape},%
    }
\end{scope} 
\end{tikzpicture}

\end{document}

enter image description here

  • Thank you very much. – user1993416 Jan 11 at 17:45
  • I am trying to make the figure of a shopping cart using the cuboid code, but I need to place a handle to the cart and wheels. Please, may I ask you if this question is something I can post as a question?. I only know how to draw the cart basket with the cuboid code. – user1993416 Jan 11 at 21:31
  • @user1993416 I would be happy to try but I have no idea how the cart should look like and I will be very busy in the next hours. Could you please ask a separate question? (It is free of charge. ;-) – marmot Jan 11 at 21:52
  • Thank you. Of course, I would place another question. I can draw a scheme of the cart and put the code of the cuboid. However, I will think if I post it because I think I can draw the figure using an external program. It will not be the same clarity and quality than TikZ but.... – user1993416 Jan 11 at 22:28
  • @user1993416 If you give me a sketch I can try to do something. Note, however, that (1) TikZ has no 3d engine and (2) if you want to add a lot of 3d stuff, it might be advantageous to use tikz-3dplot because this has a lot of stuff built in which the above macros use. So in this sense a separate question may make more sense, even if it is just to transfer this code to tikz-3dplot and to add some simple things. tikz3d-plot may allow one to cook the above macros down substantially, see e.g. this post to get an idea. – marmot Jan 12 at 1:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.