我需要编辑此 Tikzpicture 的部分内容

我需要编辑此 Tikzpicture 的部分内容

我想编辑第五个元素,也就是最后一个元素,也是这张图片右下角的元素。我想把它改成(第一个到第四个元素)我不知道该怎么做。

这是我尝试的输出

看看我尝试过什么

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{calc}
\tikzset{plane/.style n args={3}{insert path={%
            #1 -- ++ #2 -- ++ #3 -- ++ ($-1*#2$) -- cycle}},
    unit xy plane/.style={plane={#1}{(1,0,0)}{(0,1,0)}},
    unit xz plane/.style={plane={#1}{(1,0,0)}{(0,0,1)}},
    unit yz plane/.style={plane={#1}{(0,1,0)}{(0,0,1)}},
    get projections/.style={insert path={%
            let \p1=(1,0,0),\p2=(0,1,0)  in 
            [/utils/exec={\pgfmathtruncatemacro{\xproj}{sign(\x1)}\xdef\xproj{\xproj}
                \pgfmathtruncatemacro{\yproj}{sign(\x2)}\xdef\yproj{\yproj}
                \pgfmathtruncatemacro{\zproj} 
   {sign(cos(\tdplotmaintheta))}\xdef\zproj{\zproj}}]}},
    pics/unit cube/.style={code={
            \path[get projections];
            \draw (0,0,0) -- (1,1,1);
            \ifnum\zproj=-1
            \path[3d cube/every face,3d cube/xy face,unit xy plane={(0,0,0)}]; 
            \fi
            \ifnum\yproj=1
            \path[3d cube/every face,3d cube/yz face,unit yz plane={(1,0,0)}]; 
            \else
            \path[3d cube/every face,3d cube/yz face,unit yz plane={(0,0,0)}]; 
            \fi
            \ifnum\xproj=1
            \path[3d cube/every face,3d cube/xz face,unit xz plane={(0,0,0)}]; 
            \else
            \path[3d cube/every face,3d cube/xz face,unit xz plane={(0,1,0)}]; 
            \fi
            \ifnum\zproj>-1
            \path[3d cube/every face,3d cube/xy face,unit xy plane={(0,0,1)}]; 
            \fi
    }},
    3d cube/.cd,
    xy face/.style={fill=blue!10},
    xz face/.style={fill=blue!20},
    yz face/.style={fill=blue!30},
    num cubes x/.estore in=\NumCubesX,
    num cubes y/.estore in=\NumCubesY,
    num cubes z/.estore in=\NumCubesZ,
    num cubes x=1,num cubes y/.initial=1,num cubes z/.initial=1,
    cube scale/.initial=0.9,
    every face/.style={draw,very thick},
    /tikz/pics/.cd,
    cube array/.style={code={%
            \tikzset{3d cube/.cd,#1}
            %\typeout{\NumCubesX,\NumCubesY,\NumCubesZ}
            \path[get projections];
            \ifnum\yproj=1
            \def\LstX{1,...,\NumCubesX}
            \else 
            \ifnum\NumCubesX>1
            \pgfmathtruncatemacro{\NextToLast}{\NumCubesX-1}
            \def\LstX{\NumCubesX,\NextToLast,...,1}
            \else
            \def\LstX{1}   
            \fi 
            \fi
            \ifnum\xproj=-1
            \def\LstY{1,...,\NumCubesY}
            \else 
            \ifnum\NumCubesY>1
            \pgfmathtruncatemacro{\NextToLast}{\NumCubesX-1}
            \def\LstY{\NumCubesY,\NextToLast,...,1}
            \else
            \def\LstY{1}   
            \fi 
            \fi
            \ifnum\zproj=1
            \def\LstZ{1,...,\NumCubesZ}
            \else 
            \ifnum\NumCubesZ>1
            \pgfmathtruncatemacro{\NextToLast}{\NumCubesX-1}
            \def\LstZ{\NumCubesZ,\NextToLast,...,1}
            \else
            \def\LstZ{1}   
            \fi 
            \def\LstZ{\NumCubesZ,\NextToLast,...,1}
            \fi
            \foreach \X in \LstX
            {\foreach \Y in \LstY
            {\foreach \Z in \LstZ
                    {\path (\X-\NumCubesX/2-1,\Y-\NumCubesY/2-1,\Z-\NumCubesY/2-1)
                        pic[scale=\pgfkeysvalueof{/tikz/3d cube/cube scale}]{unit cube};}}
            } 
    }}
}
\begin{document}
    \tdplotsetmaincoords{60}{200} % the first argument cannot be larger than 90
    \begin{tikzpicture}[line join=round,font=\sffamily,3d cube/.cd,
    num cubes x=1,num cubes y=1,num cubes z=1]
        \begin{scope}[local bounding box=first row]
            \begin{scope}[tdplot_main_coords,local bounding box=scalar]
                \path pic{cube array={num cubes x=1}};
            \end{scope}
            \begin{scope}[xshift=4cm,tdplot_main_coords,local bounding box=vector]
                \path pic{cube array={num cubes x=3}};
            \end{scope}
            \begin{scope}[xshift=8cm,tdplot_main_coords,local bounding box=matrix]
                \path pic{cube array={num cubes x=3,num cubes z=3}};
            \end{scope}
            %\begin{scope}[xshift=8cm,tdplot_main_coords,local bounding box=array]
                %\path pic{cube array={num cubes x=3,num cubes y=3,num cubes z=3}};
            %\end{scope}
        \end{scope}
        \foreach \X in {scalar, vector, matrix}
        {\node[above] at (\X|-first row.north){\X};}
        %
        \begin{scope}[yshift=-5.5cm,local bounding box=data frame,tdplot_main_coords]
            \foreach \Col [count=\XX] in {blue,purple,green}
            {\path (2-\XX,0,0)pic{cube array={num cubes z=3,
                        xy face/.style={fill=\Col!5},
                        xz face/.style={fill=\Col!10},
                        yz face/.style={fill=\Col!15}}};}
        \end{scope}
        \node[above] (df) at (data frame.north){data frame};
        %
        \begin{scope}[yshift=-3.5cm,xshift=5cm,local bounding box=list]
            \path foreach \xy in {-1.5,0,1.5}
            {(\xy,0,0) pic[tdplot_main_coords,scale=0.4]{cube array={num cubes x=1,num cubes y=3,num cubes z=3,cube scale=0.36}}};
        \end{scope}
        \node at (df-|list){list};
    
    \end{tikzpicture}
\end{document}

我想要的是

我希望右下角的三个小元素是四(,,scalarvector)。matrixdata frame

这是我想改变的部分

        %
        \begin{scope}[yshift=-3.5cm,xshift=5cm,local bounding box=list]
            \path foreach \xy in {-1.5,0,1.5}
            {(\xy,0,0) pic[tdplot_main_coords,scale=0.4]{cube array={num cubes x=1,num cubes y=3,num cubes z=3,cube scale=0.36}}};
        \end{scope}
        \node at (df-|list){list};

foreach 函数让我很难处理。

答案1

这是一张相当复杂的图片——我不会称其为一个最小示例。但如果不了解 3D 部分,如果我理解正确,我试图模仿其余部分的操作,然后通过反复试验来调整坐标。如果你用

\begin{scope}[yshift=-5.5cm,xshift=5cm,local bounding box=list]
            % use a parametric \nx, \ny, \nz for the first three object
            \path foreach \xy/\nx/\ny/\nz in 
                {-1.5/1/1/1,0/3/1/1,1.5/3/1/3}
            {(\xy,0,0) pic[tdplot_main_coords,scale=0.4]{cube array={num cubes x=\nx,num cubes y=\ny,num cubes z=\nz,cube scale=0.36}}};
            % the last one is a different object, for the colors, so do that
            % alone
            \foreach \Col [count=\XX] in {blue,purple,green}
                {\path (4.2-0.43*\XX,-0.4+0.1*\XX,0)pic[tdplot_main_coords, scale=0.4] {cube array={num cubes z=3,
                        xy face/.style={fill=\Col!5},
                        xz face/.style={fill=\Col!10},
                        yz face/.style={fill=\Col!15},
                        cube scale=0.36}};}
\end{scope}

您将获得:

以下代码片段的输出

代码利用了最有用的功能之一\foreach:跨过多个值。如果你写

\foreach \x/\y in {1/A, 2/B, 3/C} { ... };

中的代码{...}将“运行”三次,并且\x\y将采用指定的值:在第一个循环中,\x将是1\y将是A;在第二个循环中,\x2\yB;等等。您可以拥有任意数量的部分,这些部分之间用/;在答案的情况下,我使用了其中的 4 个(位置、x 立方体的数量、y 立方体的数量和 z 立方体的数量)。

完整代码:

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{calc}
\tikzset{plane/.style n args={3}{insert path={%
            #1 -- ++ #2 -- ++ #3 -- ++ ($-1*#2$) -- cycle}},
    unit xy plane/.style={plane={#1}{(1,0,0)}{(0,1,0)}},
    unit xz plane/.style={plane={#1}{(1,0,0)}{(0,0,1)}},
    unit yz plane/.style={plane={#1}{(0,1,0)}{(0,0,1)}},
    get projections/.style={insert path={%
            let \p1=(1,0,0),\p2=(0,1,0)  in 
            [/utils/exec={\pgfmathtruncatemacro{\xproj}{sign(\x1)}\xdef\xproj{\xproj}
                \pgfmathtruncatemacro{\yproj}{sign(\x2)}\xdef\yproj{\yproj}
                \pgfmathtruncatemacro{\zproj} 
   {sign(cos(\tdplotmaintheta))}\xdef\zproj{\zproj}}]}},
    pics/unit cube/.style={code={
            \path[get projections];
            \draw (0,0,0) -- (1,1,1);
            \ifnum\zproj=-1
            \path[3d cube/every face,3d cube/xy face,unit xy plane={(0,0,0)}]; 
            \fi
            \ifnum\yproj=1
            \path[3d cube/every face,3d cube/yz face,unit yz plane={(1,0,0)}]; 
            \else
            \path[3d cube/every face,3d cube/yz face,unit yz plane={(0,0,0)}]; 
            \fi
            \ifnum\xproj=1
            \path[3d cube/every face,3d cube/xz face,unit xz plane={(0,0,0)}]; 
            \else
            \path[3d cube/every face,3d cube/xz face,unit xz plane={(0,1,0)}]; 
            \fi
            \ifnum\zproj>-1
            \path[3d cube/every face,3d cube/xy face,unit xy plane={(0,0,1)}]; 
            \fi
    }},
    3d cube/.cd,
    xy face/.style={fill=blue!10},
    xz face/.style={fill=blue!20},
    yz face/.style={fill=blue!30},
    num cubes x/.estore in=\NumCubesX,
    num cubes y/.estore in=\NumCubesY,
    num cubes z/.estore in=\NumCubesZ,
    num cubes x=1,num cubes y/.initial=1,num cubes z/.initial=1,
    cube scale/.initial=0.9,
    every face/.style={draw,very thick},
    /tikz/pics/.cd,
    cube array/.style={code={%
            \tikzset{3d cube/.cd,#1}
            %\typeout{\NumCubesX,\NumCubesY,\NumCubesZ}
            \path[get projections];
            \ifnum\yproj=1
            \def\LstX{1,...,\NumCubesX}
            \else 
            \ifnum\NumCubesX>1
            \pgfmathtruncatemacro{\NextToLast}{\NumCubesX-1}
            \def\LstX{\NumCubesX,\NextToLast,...,1}
            \else
            \def\LstX{1}   
            \fi 
            \fi
            \ifnum\xproj=-1
            \def\LstY{1,...,\NumCubesY}
            \else 
            \ifnum\NumCubesY>1
            \pgfmathtruncatemacro{\NextToLast}{\NumCubesX-1}
            \def\LstY{\NumCubesY,\NextToLast,...,1}
            \else
            \def\LstY{1}   
            \fi 
            \fi
            \ifnum\zproj=1
            \def\LstZ{1,...,\NumCubesZ}
            \else 
            \ifnum\NumCubesZ>1
            \pgfmathtruncatemacro{\NextToLast}{\NumCubesX-1}
            \def\LstZ{\NumCubesZ,\NextToLast,...,1}
            \else
            \def\LstZ{1}   
            \fi 
            \def\LstZ{\NumCubesZ,\NextToLast,...,1}
            \fi
            \foreach \X in \LstX
            {\foreach \Y in \LstY
            {\foreach \Z in \LstZ
                    {\path (\X-\NumCubesX/2-1,\Y-\NumCubesY/2-1,\Z-\NumCubesY/2-1)
                        pic[scale=\pgfkeysvalueof{/tikz/3d cube/cube scale}]{unit cube};}}
            } 
    }}
}
\begin{document}
    \tdplotsetmaincoords{60}{200} % the first argument cannot be larger than 90
    \begin{tikzpicture}[line join=round,font=\sffamily,3d cube/.cd,
    num cubes x=1,num cubes y=1,num cubes z=1]
        \begin{scope}[local bounding box=first row]
            \begin{scope}[tdplot_main_coords,local bounding box=scalar]
                \path pic{cube array={num cubes x=1}};
            \end{scope}
            \begin{scope}[xshift=4cm,tdplot_main_coords,local bounding box=vector]
                \path pic{cube array={num cubes x=3}};
            \end{scope}
            \begin{scope}[xshift=8cm,tdplot_main_coords,local bounding box=matrix]
                \path pic{cube array={num cubes x=3,num cubes z=3}};
            \end{scope}
            %\begin{scope}[xshift=8cm,tdplot_main_coords,local bounding box=array]
                %\path pic{cube array={num cubes x=3,num cubes y=3,num cubes z=3}};
            %\end{scope}
        \end{scope}
        \foreach \X in {scalar, vector, matrix}
        {\node[above] at (\X|-first row.north){\X};}
        %
        \begin{scope}[yshift=-5.5cm,local bounding box=data frame,tdplot_main_coords]
            \foreach \Col [count=\XX] in {blue,purple,green}
            {\path (2-\XX,0,0)pic{cube array={num cubes z=3,
                        xy face/.style={fill=\Col!5},
                        xz face/.style={fill=\Col!10},
                        yz face/.style={fill=\Col!15}}};}
        \end{scope}
        \node[above] (df) at (data frame.north){data frame};
        %
        \begin{scope}[yshift=-5.5cm,xshift=5cm,local bounding box=list]
            % use a parametric \nx, \ny, \nz for the first three object
            \path foreach \xy/\nx/\ny/\nz in 
                {-1.5/1/1/1,0/3/1/1,1.5/3/1/3}
            {(\xy,0,0) pic[tdplot_main_coords,scale=0.4]{cube array={num cubes x=\nx,num cubes y=\ny,num cubes z=\nz,cube scale=0.36}}};
            % the last one is a different object, for the colors, so do that
            % alone
            \foreach \Col [count=\XX] in {blue,purple,green}
                {\path (4.2-0.43*\XX,-0.4+0.1*\XX,0)pic[tdplot_main_coords, scale=0.4] {cube array={num cubes z=3,
                        xy face/.style={fill=\Col!5},
                        xz face/.style={fill=\Col!10},
                        yz face/.style={fill=\Col!15},
                        cube scale=0.36}};}
        \end{scope}
        \node at (df-|list){list};
    
    \end{tikzpicture}
\end{document}

答案2

foreach 设置了三个形状各自的位置,要获得第四个形状,请尝试像这样添加另一个数字:

\path foreach \xy in {-1.5,0,1.5,3.0}

因为它们彼此相差 1.5,所以我将 1.5 加到最后一个值上,得到 3.0。

另外,为了使形状更低,使文本不在其上方,您可以更改此行:

{(\xy,0,0) pic[tdplot_main_coords,scale=0.4]{cube array={num cubes x=1,num cubes y=3,num cubes z=3,cube scale=0.36}}};

到:

{(\xy,-1.0,0) pic[tdplot_main_coords,scale=0.4]{cube array={num cubes x=1,num cubes y=3,num cubes z=3,cube scale=0.36}}};

这会使图像在页面上稍微向下移动。

这将给你这样的结果: 在此处输入图片描述

相关内容