代码:

代码:

使用以下代码,如何使重复的立方体垂直和水平地连接在一起以形成一个实体块?

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{tikz} 
\begin{document}
\begin{frame}[t]
\frametitle{cube}
\begin{minipage}{\textwidth}
\newsavebox{\hh}
\savebox{\hh}{
\newcommand{\Depth}{1}
\newcommand{\Height}{1}
\newcommand{\Width}{1}
\begin{tikzpicture}[scale=.9, transform shape]
\coordinate (O) at (0,0,0);
\coordinate (A) at (0,\Width,0);
\coordinate (B) at (0,\Width,\Height);
\coordinate (C) at (0,0,\Height);
\coordinate (D) at (\Depth,0,0);
\coordinate (E) at (\Depth,\Width,0);
\coordinate (F) at (\Depth,\Width,\Height);
\coordinate (G) at (\Depth,0,\Height);
\draw[blue,fill=yellow!80] (O) -- (C) -- (G) -- (D) -- cycle;
\draw[blue,fill=blue!30] (O) -- (A) -- (E) -- (D) -- cycle;
\draw[blue,fill=red!10] (O) -- (A) -- (B) -- (C) -- cycle;
\draw[blue,fill=red!20,opacity=0.8] (D) -- (E) -- (F) -- (G) -- cycle;
\draw[blue,fill=red!20,opacity=0.6] (C) -- (B) -- (F) -- (G) -- cycle;
\draw[blue,fill=red!20,opacity=0.8] (A) -- (B) -- (F) -- (E) -- cycle;
\end{tikzpicture}}
\foreach \x in {0,...,3} {\usebox{\hh}}
\foreach \x in {0,...,3} {\usebox{\hh}}
\foreach \x in {0,...,3} {\usebox{\hh}}
\foreach \x in {0,...,3} {\usebox{\hh}}
\end{minipage}
\end{frame}
\end{document} 

在此处输入图片描述

让它看起来像这样

在此处输入图片描述

答案1

正如 Peter Grill 指出的那样,如果你使用tikzpicturefor,那么你就可以做你想做的事。我认为你\savebox在这里使用 es 的方法是一种方法,但 Ti最好的方法是在这里使用pics。这会\newcommand用 s 来交换立方体的参数。

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{tikz} 
\begin{document}
\tikzset{pics/.cd,
cube/.style args={#1/#2/#3}{code={
\coordinate (O) at (0,0,0);
\coordinate (A) at (0,#2,0);
\coordinate (B) at (0,#2,#3);
\coordinate (C) at (0,0,#3);
\coordinate (D) at (#1,0,0);
\coordinate (E) at (#1,#2,0);
\coordinate (F) at (#1,#2,#3);
\coordinate (G) at (#1,0,#3);
\draw[blue,fill=yellow!80] (O) -- (C) -- (G) -- (D) -- cycle;
\draw[blue,fill=blue!30] (O) -- (A) -- (E) -- (D) -- cycle;
\draw[blue,fill=red!10] (O) -- (A) -- (B) -- (C) -- cycle;
\draw[blue,fill=red!20,opacity=0.8] (D) -- (E) -- (F) -- (G) -- cycle;
\draw[blue,fill=red!20,opacity=0.6] (C) -- (B) -- (F) -- (G) -- cycle;
\draw[blue,fill=red!20,opacity=0.8] (A) -- (B) -- (F) -- (E) -- cycle;
}}}
\begin{frame}[t]
\frametitle{cube}
\begin{tikzpicture}
\foreach \x in {0,1} 
{\pic at (\x,1,-\x-1) {cube={1/1/1}};}
\foreach \x in {0,...,2} 
{\pic at (\x,0,-\x) {cube={1/1/1}};}
\foreach \x in {0,1} 
{\pic at (\x+1,-1,-\x) {cube={1/1/1}};}
\end{tikzpicture}
\end{frame}
\end{document} 

在此处输入图片描述

您也可以使用\saveboxes 实现相同的效果(编译速度可能更快一些)。我还删除了多余的空格,非常感谢 Peter Grill!

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{tikz} 
\begin{document}
\begin{frame}[t]
\frametitle{cube}
\begin{minipage}{\textwidth}
\newsavebox{\hh}
\savebox{\hh}{%
\newcommand{\Depth}{1}%
\newcommand{\Height}{1}%
\newcommand{\Width}{1}%
\begin{tikzpicture}[scale=.9, transform shape]
\coordinate (O) at (0,0,0);
\coordinate (A) at (0,\Width,0);
\coordinate (B) at (0,\Width,\Height);
\coordinate (C) at (0,0,\Height);
\coordinate (D) at (\Depth,0,0);
\coordinate (E) at (\Depth,\Width,0);
\coordinate (F) at (\Depth,\Width,\Height);
\coordinate (G) at (\Depth,0,\Height);
\draw[blue,fill=yellow!80] (O) -- (C) -- (G) -- (D) -- cycle;
\draw[blue,fill=blue!30] (O) -- (A) -- (E) -- (D) -- cycle;
\draw[blue,fill=red!10] (O) -- (A) -- (B) -- (C) -- cycle;
\draw[blue,fill=red!20,opacity=0.8] (D) -- (E) -- (F) -- (G) -- cycle;
\draw[blue,fill=red!20,opacity=0.6] (C) -- (B) -- (F) -- (G) -- cycle;
\draw[blue,fill=red!20,opacity=0.8] (A) -- (B) -- (F) -- (E) -- cycle;
\end{tikzpicture}}
\begin{tikzpicture}
\foreach \x in {0,1} 
{\node at (0.9*\x,0.9,-0.9*\x-0.9) {\usebox{\hh}};}
\foreach \x in {0,...,2} 
{\node at (0.9*\x,0,-0.9*\x) {\usebox{\hh}};}
\foreach \x in {0,1} 
{\node at (0.9*\x+0.9,-0.9,-0.9*\x) {\usebox{\hh}};}
\end{tikzpicture}
\end{minipage}
\end{frame}
\end{document} 

答案2

这是为了回答评论中关于如何使用负空间来做到这一点的问题。我是不是不过,建议使用此解决方案。此图像看起来古怪的因为底部立方体位于上部立方体之上。

使用tabular有点容易,所以也提供了该选项。

在此处输入图片描述

代码:

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{array}%            \newcolumntype
\usepackage{tikz} 

\newcolumntype{B}{>{\hspace*{-0.90em}}c<{}}

\newsavebox{\hh}

\begin{document}
\begin{frame}[t]
\frametitle{cube}
%\begin{minipage}{\textwidth}
\savebox{\hh}{%
    \newcommand{\Depth}{1}%
    \newcommand{\Height}{1}%
    \newcommand{\Width}{1}%
    \begin{tikzpicture}[scale=.9, transform shape]
        \coordinate (O) at (0,0,0);
        \coordinate (A) at (0,\Width,0);
        \coordinate (B) at (0,\Width,\Height);
        \coordinate (C) at (0,0,\Height);
        \coordinate (D) at (\Depth,0,0);
        \coordinate (E) at (\Depth,\Width,0);
        \coordinate (F) at (\Depth,\Width,\Height);
        \coordinate (G) at (\Depth,0,\Height);
        \draw[blue,fill=yellow!80] (O) -- (C) -- (G) -- (D) -- cycle;
        \draw[blue,fill=blue!30] (O) -- (A) -- (E) -- (D) -- cycle;
        \draw[blue,fill=red!10] (O) -- (A) -- (B) -- (C) -- cycle;
        \draw[blue,fill=red!20,opacity=0.8] (D) -- (E) -- (F) -- (G) -- cycle;
        \draw[blue,fill=red!20,opacity=0.6] (C) -- (B) -- (F) -- (G) -- cycle;
        \draw[blue,fill=red!20,opacity=0.8] (A) -- (B) -- (F) -- (E) -- cycle;
    \end{tikzpicture}%
}
%% ---------------------
\foreach \x in {0,...,3} {\usebox{\hh}\hspace*{-0.90em}}\par\vspace{-2.3ex}
\foreach \x in {0,...,3} {\usebox{\hh}\hspace*{-0.90em}}\par\vspace{-2.3ex}
\foreach \x in {0,...,3} {\usebox{\hh}\hspace*{-0.90em}}\par\vspace{-2.3ex}
\foreach \x in {0,...,3} {\usebox{\hh}\hspace*{-0.90em}}
\par
\begin{tabular}{@{}c@{}B@{}B@{}B@{}}
    \usebox{\hh} & \usebox{\hh} & \usebox{\hh} & \usebox{\hh} \\[-2.9ex]
    \usebox{\hh} & \usebox{\hh} & \usebox{\hh} & \usebox{\hh} \\[-2.9ex]
    \usebox{\hh} & \usebox{\hh} & \usebox{\hh} & \usebox{\hh} \\[-2.9ex]
    \usebox{\hh} & \usebox{\hh} & \usebox{\hh} & \usebox{\hh} \\[-2.9ex]
\end{tabular}%
%\end{minipage}
\end{frame}
\end{document}

相关内容