如何改进我的 Tikz 脚本?(3D 立方体)

如何改进我的 Tikz 脚本?(3D 立方体)

我学习 LaTeX 已经有一段时间了,今天我第一次尝试使用 Tikz 创建图形元素。

这就是我想要实现的目标:

在此处输入图片描述

这就是我所做的:

\documentclass{standalone}
\usepackage{tikz}

\tikzset{
    cube/.pic={
        \draw[inside](-1,-1,-1)--(-1,1,-1)--(-1,1,1)--(-1,-1,1)--(-1,-1,-1);
        \draw[inside](-1,-1,-1)--(1,-1,-1)--(1,-1,1)--(-1,-1,1)--(-1,-1,-1);
        \draw[inside](-1,-1,-1)--(1,-1,-1)--(1,1,-1)--(-1,1,-1)--(-1,-1,-1);
        \draw[inside](1,1,1)--(1,-1,1)--(1,-1,-1)--(1,1,-1)--(1,1,1);
        \draw[inside](1,1,1)--(-1,1,1)--(-1,1,-1)--(1,1,-1)--(1,1,1);
        \draw[inside](1,1,1)--(-1,1,1)--(-1,-1,1)--(1,-1,1)--(1,1,1);
    },
    threads/.pic={
    
        \pic at (0, -2.5, -2.5) {cube};
        \pic at (2.5, -2.5, -2.5) {cube};
        \pic at (0, 0, -2.5) {cube};
        \pic at (2.5, 0, -2.5) {cube};
        \pic at (5, -2.5, -2.5) {cube};
        \pic at (5, 0, -2.5) {cube};
        
        \pic at (0, -2.5, 0) {cube};
        \pic at (2.5, -2.5, 0) {cube};
        \pic at (0, 0, 0) {cube};
        \pic at (2.5, 0, 0) {cube};
        \pic at (5, -2.5, 0) {cube};
        \pic at (5, 0, 0) {cube};
    },
    block/.pic={
        \draw[outside](-5,-5,-5)--(-5,5,-5)--(-5,5,5)--(-5,-5,5)--(-5,-5,-5);%kiri
        \draw[outside](-5,-5,-5)--(5,-5,-5)--(5,-5,5)--(-5,-5,5)--(-5,-5,-5);%bawah
        \draw[outside](-5,-5,-5)--(5,-5,-5)--(5,5,-5)--(-5,5,-5)--(-5,-5,-5);%belakang
        \pic at (-3.5, 0, 0) {threads};
        \draw(5,5,5)--(5,-5,5)--(5,-5,-5)--(5,5,-5)--(5,5,5);%kanan
        \draw(5,5,5)--(-5,5,5)--(-5,5,-5)--(5,5,-5)--(5,5,5);%atas
        \draw(5,5,5)--(-5,5,5)--(-5,-5,5)--(5,-5,5)--(5,5,5);%depan
    }
}
        
\begin{document}

    \begin{tikzpicture}[scale=0.4, transform shape, inside/.style={fill=orange,thin},
                        outside/.style={fill=yellow,thick},
                        every node/.style={font=\small}, grid/.style={fill=green,thick}]

        %%
        \draw[grid](-15,-15,-15)--(15,-15,-15)--(15,8,-15)--(-15,8,-15)--(-15,-15,-15);
        \pic at (0, 0, 0) {block};
        \pic at (12, 0, 0) {block};
    \end{tikzpicture}

\end{document}

在此处输入图片描述

问题:

  1. 作为初学者,我觉得我的 TeX 代码不是最优的,我该如何改进?
  2. 我怎样才能像参考图中那样添加文本?​​我知道这很愚蠢,但我还是一无所知。

谢谢你!

答案1

pics这是一种可能性,如果您组合或\newcommand宏(例如,您可以为块创建另一个宏)或\foreach循环,则会有更多。

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

\newcommand{\cube}[7]% position (center), size, color, front edges (1/0), back edges (1/0),
{%                      label, y shift for the label
  \begin{scope}[shift={#1},scale=0.5]
    \draw[fill=#3] (-#2,-#2,-#2) -- (-#2,-#2, #2) -- ( #2,-#2, #2) --
                   ( #2, #2, #2) -- ( #2, #2,-#2) -- (-#2, #2,-#2)  -- cycle;
    \ifnum #4 = 1% if we need front edges
      \draw ( #2,-#2,-#2) -- (-#2,-#2,-#2);
      \draw ( #2,-#2,-#2) -- ( #2, #2,-#2);
      \draw ( #2,-#2,-#2) -- ( #2,-#2, #2);    
    \fi
    \ifnum #5 = 1% if we need back edges
      \draw (-#2, #2, #2) -- ( #2, #2, #2);
      \draw (-#2, #2, #2) -- (-#2,-#2, #2);
      \draw (-#2, #2, #2) -- (-#2, #2,-#2);
    \fi
    \node[blue] at (0,-2*#7,0) {#6};
  \end{scope}
}

\begin{document}
\begin{tikzpicture}[line join=round, line cap=round,%
                    x={(1 cm,0 cm)}, y={(0 cm,-1cm)}, z={(0.5 cm,0.5 cm)}]
\foreach\i in {0,1}
{%
  \cube{(5*\i,0,0)}{4}{yellow}{0}{1}{}{0}
  \foreach\c in {1,0} \foreach\b in {1,0} \foreach\a in {0,1,2} 
  {%
    \cube{(5*\i+1.2*\a-1.2,1.2*\b-0.6,1.2*\c-0.6)}{1}{orange}{1}{0}{$\a,\b,\c$}{0.5+3*\b}
  }
  \cube{(5*\i,0,0)}{4}{none}{1}{0}{Block $\i$}{2}
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

編輯:我在宏中添加了文本节点\cube,这减少了代码量并使其更清晰(我希望如此)。我也改变了轴。

相关内容