将 tickzpictures 放在彼此下方并居中时出现问题

将 tickzpictures 放在彼此下方并居中时出现问题

我有以下 tickz 图片,它们是不同尺寸的长方体。

\documentclass[tikz]{standalone}% 
\usepackage{pgf}
\usetikzlibrary{arrows,automata,shapes.multipart,calc,decorations.text}

\RequirePackage{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}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}



\begin{tikzpicture}[scale=0.6]

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

    \tikzcuboid{%
        shiftx=10.5cm,%
        shifty=0.7cm,%
        % shadeopacity=0.30,%
        dimx=5,%
        dimy=1,%
        dimz=5,%
        xedgelabel={6 m},%
        yedgelabel={2.14 m},%
        zedgelabel={4 m},%
        /tikz/edgelabel/.style={sloped,scale=1.4,transform shape},%
        }

\end{tikzpicture}



\begin{tikzpicture}[scale=0.6]
% \begin{tikzpicture}[thick,scale=0.6, every node/.style={transform shape}]

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

    \tikzcuboid{%
        shiftx=10.5cm,%
        shifty=0.7cm,%
        % shadeopacity=0.30,%
        dimx=6,%
        dimy=1,%
        dimz=6,%
        xedgelabel={7.2 m},%
        yedgelabel={2.15 m},%
        zedgelabel={4.8 m},%
        /tikz/edgelabel/.style={sloped,scale=1.4,transform shape},%
        }

\end{tikzpicture}

% \begin{tikzpicture}[scale=0.6]
\begin{tikzpicture}[scale=0.6, every node/.style={transform shape}]

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

    \tikzcuboid{%
        shiftx=10.5cm,%
        shifty=0.7cm,%
        % shadeopacity=0.30,%
        dimx=7,%
        dimy=1,%
        dimz=7,%
        xedgelabel={8.4 m},%
        yedgelabel={2.15 m},%
        zedgelabel={5.6 m},%
        /tikz/edgelabel/.style={sloped,scale=1.4,transform shape},%
        }

\end{tikzpicture}

\end{document}

编译结果如下:

在此处输入图片描述

但是,我想获得一对位于其他下方的长方体,如下所示:

在此处输入图片描述

在之前的帖子中如何强制两个或多个 tikzpicture 在独立类中彼此位于彼此下方?告诉使用varwidth。如果我使用varwidth结果图是这样的:

在此处输入图片描述

这与上面期望的结果不同,但它没有将长方体对置于中心,并且裁剪了右下角的长方体(较大的一个)。

问候

答案1

一张好照片,花了不少功夫。但剩下要做的只是,嗯,改变shiftys 的值,然后把整个东西放在一个 里面{tikzpicture}

\documentclass[tikz]{standalone}% 
\usepackage{pgf}
\usetikzlibrary{arrows,automata,shapes.multipart,calc,decorations.text}

\RequirePackage{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}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}



\begin{tikzpicture}[scale=0.6]

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

    \tikzcuboid{%
        shiftx=10.5cm,%
        shifty=0.7cm,%
        % shadeopacity=0.30,%
        dimx=5,%
        dimy=1,%
        dimz=5,%
        xedgelabel={6 m},%
        yedgelabel={2.14 m},%
        zedgelabel={4 m},%
        /tikz/edgelabel/.style={sloped,scale=1.4,transform shape},%
        }

%\end{tikzpicture}



%\begin{tikzpicture}[scale=0.6]
% \begin{tikzpicture}[thick,scale=0.6, every node/.style={transform shape}]

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

    \tikzcuboid{%
        shiftx=10.5cm,%
        shifty=-4.3cm,%
        % shadeopacity=0.30,%
        dimx=6,%
        dimy=1,%
        dimz=6,%
        xedgelabel={7.2 m},%
        yedgelabel={2.15 m},%
        zedgelabel={4.8 m},%
        /tikz/edgelabel/.style={sloped,scale=1.4,transform shape},%
        }

    \tikzcuboid{%
    shiftx=0cm,%
    shifty=-10.5cm,%
    scale=1.00,%
    rotation=0,%
    densityx=1,%
    densityy=1,%
    densityz=1,%
    dimx=4,%
    dimy=1,%
    dimz=4,%
    front/.style={draw=blue!75!black,fill=blue!25!white},%
    right/.style={draw=blue!25!black,fill=blue!75!white},%
    top/.style={draw=blue!50!black,fill=blue!50!white},%
    anglex=-10,%
    angley=90,%
    anglez=205,%
    scalex=1.2,%
    scaley=2.15,%
    scalez=0.7,%
    emphedge=false,%
    shade,%
    shadeopacity=0.15,%
    xedgelabel={4.8 m},%
    yedgelabel={2.15 m},%
    zedgelabel={3.2 m},%
    /tikz/edgelabel/.style={sloped,scale=1.4,transform shape},%
    }

    \tikzcuboid{%
        shiftx=10.5cm,%
        shifty=-9.8cm,%
        % shadeopacity=0.30,%
        dimx=7,%
        dimy=1,%
        dimz=7,%
        xedgelabel={8.4 m},%
        yedgelabel={2.15 m},%
        zedgelabel={5.6 m},%
        /tikz/edgelabel/.style={sloped,scale=1.4,transform shape},%
        }

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

这只是要提一下,当您在同一张图片中时,无需一遍又一遍地重复所有键。此外,我会pic为立方体使用。(是的,代码很长,许多库代码也是如此。只是这个不是库的一部分。)

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

\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}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}



\begin{tikzpicture}[scale=0.6,transform shape,
pics/cuboid/.style={code=\tikzcuboid{#1}},
    cuboid/.cd,
    scale=1.00,%
    rotation=0,%
    densityx=1,%
    densityy=1,%
    densityz=1,%
    anglex=-10,%
    angley=90,%
    anglez=205,%
    scalex=1.2,%
    scaley=2.15,%
    scalez=0.7,%
    emphedge=false,%
    shade,%
    shadeopacity=0.15,%
    front/.style={draw=blue!75!black,fill=blue!25!white},%
    right/.style={draw=blue!25!black,fill=blue!75!white},%
    top/.style={draw=blue!50!black,fill=blue!50!white},%
]
%\matrix (mat) at (0,0) {

\pic  {cuboid={%
    dimx=2,%
    dimy=1,%
    dimz=2,%
    xedgelabel={2.4 m},%
    yedgelabel={2.14 m},%
    zedgelabel={1.6 m},%
    /tikz/edgelabel/.style={sloped,scale=1.4,transform shape},%
    }}; %&  \\
    %}

    \pic{cuboid={%
        shiftx=10.5cm,%
        shifty=0.7cm,%
        % shadeopacity=0.30,%
        dimx=5,%
        dimy=1,%
        dimz=5,%
        xedgelabel={6 m},%
        yedgelabel={2.14 m},%
        zedgelabel={4 m},%
        /tikz/edgelabel/.style={sloped,scale=1.4,transform shape},%
        }};

\begin{scope}[yshift=-5.2cm]
    \pic{cuboid={%
    shiftx=0cm,%
    shifty=0cm,%
    dimx=3,%
    dimy=1,%
    dimz=3,%
    xedgelabel={3.6 m},%
    yedgelabel={2.15 m},%
    zedgelabel={2.4 m},%
    /tikz/edgelabel/.style={sloped,scale=1.4,transform shape},%
    }};

    \pic{cuboid={%
        shiftx=10.5cm,%
        shifty=0.7cm,%
        % shadeopacity=0.30,%
        dimx=6,%
        dimy=1,%
        dimz=6,%
        xedgelabel={7.2 m},%
        yedgelabel={2.15 m},%
        zedgelabel={4.8 m},%
        /tikz/edgelabel/.style={sloped,scale=1.4,transform shape},%
        }};

\end{scope}

\begin{scope}[yshift=-10.6cm]

    \pic{cuboid={%
    shiftx=0cm,%
    shifty=0cm,%
    dimx=4,%
    dimy=1,%
    dimz=4,%
    xedgelabel={4.8 m},%
    yedgelabel={2.15 m},%
    zedgelabel={3.2 m},%
    /tikz/edgelabel/.style={sloped,scale=1.4,transform shape},%
    }};

    \pic{cuboid={%
        shiftx=10.5cm,%
        shifty=0.7cm,%
        % shadeopacity=0.30,%
        dimx=7,%
        dimy=1,%
        dimz=7,%
        xedgelabel={8.4 m},%
        yedgelabel={2.15 m},%
        zedgelabel={5.6 m},%
        /tikz/edgelabel/.style={sloped,scale=1.4,transform shape},%
        }};
\end{scope}
\end{tikzpicture}

在此处输入图片描述

相关内容