我如何绘制两个相邻的立方体?我的意思是两个像这样但彼此相邻的立方体:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\foreach \n/\x/\l/\p in{
1/{(0, 4)}/{$1$}/left,
2/{(4, 4)}/{$2$}/south west,
3/{(0, 0)}/{$3$}/below,
4/{(4, 0)}/{$4$}/below,
5/{(0.8, 4.8)}/{$5$}/above,
6/{(4.8, 4.8)}/{$6$}/above,
7/{(.8, 1)}/{$7$}/south east,
8/{(4.8, 1)}/{$8$}/right
}{
\node[inner sep=2pt,circle,draw,fill,label={\p:\l}] (\n) at \x {};
}
\draw (1) -- (2) -- (4) -- (3) -- cycle;
\draw (1) -- (2) -- (6) -- (5) -- cycle;
\draw (2) -- (4) -- (8) -- (6) -- cycle;
\draw (3) -- (1) -- (5);
\draw[dashed] (5) -- (7) -- (3);
\draw[dashed] (7) -- (8);
\draw[pattern=north west lines] ($(1)!0.5!(3)$) -- ($(5)!0.5!(7)$) -- ($(6)!0.5!(8)$) -- ($(2)!0.5!(4)$) -- cycle;
\node (a1) at ($(3)!0.5!(4) - (0,0.5)$) {$1\rightarrow3$};
\node (a2) at ($(a1) - (0,0.5)$) {$5\rightarrow7$};
\node (a3) at ($(a2) - (0,0.5)$) {$6\rightarrow8$};
\node (a4) at ($(a3) - (0,0.5)$) {$2\rightarrow4$};
\end{tikzpicture}
\end{document}
答案1
一种“强力”方法是重复所有代码,在相同的 内tikzpicture
,但增加所有 x 坐标。虽然这很不方便,但使用起来更容易
\begin{scope}[xshift=6cm]
<code for cube>
\end{scope}
第三种选择可能是使用两个tikzpicture
环境,一个紧接着一个。下面我使用了这种scope
方法。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\foreach \n/\x/\l/\p in{
1/{(0, 4)}/{$1$}/left,
2/{(4, 4)}/{$2$}/south west,
3/{(0, 0)}/{$3$}/below,
4/{(4, 0)}/{$4$}/below,
5/{(0.8, 4.8)}/{$5$}/above,
6/{(4.8, 4.8)}/{$6$}/above,
7/{(.8, 1)}/{$7$}/south east,
8/{(4.8, 1)}/{$8$}/right
}{
\node[inner sep=2pt,circle,draw,fill,label={\p:\l}] (\n) at \x {};
}
\draw (1) -- (2) -- (4) -- (3) -- cycle;
\draw (1) -- (2) -- (6) -- (5) -- cycle;
\draw (2) -- (4) -- (8) -- (6) -- cycle;
\draw (3) -- (1) -- (5);
\draw[dashed] (5) -- (7) -- (3);
\draw[dashed] (7) -- (8);
\draw[pattern=north west lines] ($(1)!0.5!(3)$) -- ($(5)!0.5!(7)$) -- ($(6)!0.5!(8)$) -- ($(2)!0.5!(4)$) -- cycle;
\node (a1) at ($(3)!0.5!(4) - (0,0.5)$) {$1\rightarrow3$};
\node (a2) at ($(a1) - (0,0.5)$) {$5\rightarrow7$};
\node (a3) at ($(a2) - (0,0.5)$) {$6\rightarrow8$};
\node (a4) at ($(a3) - (0,0.5)$) {$2\rightarrow4$};
\begin{scope}[xshift=6cm]
\foreach \n/\x/\l/\p in{
1/{(0, 4)}/{$1$}/left,
2/{(4, 4)}/{$2$}/south west,
3/{(0, 0)}/{$3$}/below,
4/{(4, 0)}/{$4$}/below,
5/{(0.8, 4.8)}/{$5$}/above,
6/{(4.8, 4.8)}/{$6$}/above,
7/{(.8, 1)}/{$7$}/south east,
8/{(4.8, 1)}/{$8$}/right
}{
\node[inner sep=2pt,circle,draw,fill,label={\p:\l}] (\n) at \x {};
}
\draw (1) -- (2) -- (4) -- (3) -- cycle;
\draw (1) -- (2) -- (6) -- (5) -- cycle;
\draw (2) -- (4) -- (8) -- (6) -- cycle;
\draw (3) -- (1) -- (5);
\draw[dashed] (5) -- (7) -- (3);
\draw[dashed] (7) -- (8);
\draw[pattern=north west lines] ($(1)!0.5!(3)$) -- ($(5)!0.5!(7)$) -- ($(6)!0.5!(8)$) -- ($(2)!0.5!(4)$) -- cycle;
\node (a1) at ($(3)!0.5!(4) - (0,0.5)$) {$1\rightarrow3$};
\node (a2) at ($(a1) - (0,0.5)$) {$5\rightarrow7$};
\node (a3) at ($(a2) - (0,0.5)$) {$6\rightarrow8$};
\node (a4) at ($(a3) - (0,0.5)$) {$2\rightarrow4$};
\end{scope}
\end{tikzpicture}
\end{document}
答案2
您还可以使用保存盒或图片。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{patterns}
\newsavebox{\tempbox}
\begin{document}
\savebox{\tempbox}{\begin{tikzpicture}
\foreach \n/\x/\l/\p in{
1/{(0, 4)}/{$1$}/left,
2/{(4, 4)}/{$2$}/south west,
3/{(0, 0)}/{$3$}/below,
4/{(4, 0)}/{$4$}/below,
5/{(0.8, 4.8)}/{$5$}/above,
6/{(4.8, 4.8)}/{$6$}/above,
7/{(.8, 1)}/{$7$}/south east,
8/{(4.8, 1)}/{$8$}/right
}{
\node[inner sep=2pt,circle,draw,fill,label={\p:\l}] (\n) at \x {};
}
\draw (1) -- (2) -- (4) -- (3) -- cycle;
\draw (1) -- (2) -- (6) -- (5) -- cycle;
\draw (2) -- (4) -- (8) -- (6) -- cycle;
\draw (3) -- (1) -- (5);
\draw[dashed] (5) -- (7) -- (3);
\draw[dashed] (7) -- (8);
\draw[pattern=north west lines] ($(1)!0.5!(3)$) -- ($(5)!0.5!(7)$) -- ($(6)!0.5!(8)$) -- ($(2)!0.5!(4)$) -- cycle;
\node (a1) at ($(3)!0.5!(4) - (0,0.5)$) {$1\rightarrow3$};
\node (a2) at ($(a1) - (0,0.5)$) {$5\rightarrow7$};
\node (a3) at ($(a2) - (0,0.5)$) {$6\rightarrow8$};
\node (a4) at ($(a3) - (0,0.5)$) {$2\rightarrow4$};
\end{tikzpicture}}%
\usebox{\tempbox}\quad\usebox{\tempbox}\quad\usebox{\tempbox}
\end{document}