我想在 tikz 中制作一个奇怪的图表。此图表在节点内有一些节点……如下图所示:
我尝试过这个:
\newcommand{\twonodes}[2]{%
\begin{tikzpicture}[node distance=0.7cm]
\node[ circle, fill=gray] (#1) {};
\node[circle, below of=#1](#2) {};
\end{tikzpicture}
}
\begin{tikzpicture}[->,>=stealth',grow=right, sloped,yscale=1.3,xscale=1.2,
level 1/.style={sibling distance = 2.6cm, level distance = 1.5cm},
level 2/.style={sibling distance = 1.3cm},
level 3/.style={sibling distance = 1cm},
every node/.style={rectangle, draw=black,thin, minimum size = 0.45cm},
emph/.style={edge from parent/.style={theme2,ultra thick,draw}},
norm/.style={edge from parent/.style={white,thin,draw}}]
\node[norm] {\twonodes{1}{2} }
child[norm] { node (3) {\twonodes{4}{5}}
child[norm] { node (6) {\twonodes{7}{8}} }
child[norm] { node (9) {\twonodes{10}{11}} }
}
child[norm] { node (12) {\twonodes{13}{14}}
child[norm] { node (15) {\twonodes{16}{17}} }
child[norm] { node (18) {\twonodes{19}{20}} }
};
\path (1) edge (12);
\end{tikzpicture}
但是我在箭头上看到了这个奇怪的位置:
答案1
评论
有关在 PGF 2.10 版本中运行的版本,请参阅答案的底部。
以下是使用 TikZ 及其pic
,如下所示克劳迪奥·菲安德里诺建议his comment
:
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}
\tikzset{
mynode/.pic={
\node[overlay,draw,circle,inner sep=3pt] at (0,-7.5pt) (-l) {};
\node[overlay,draw,fill=gray!60,circle,inner sep=3pt] at (0,7.5pt) (-r) {};
\node[overlay,inner sep=5pt,draw,fit={(-l) (-r)}] (-frame) {};
},
ar/.style={
->,
>=latex
}
}
\begin{document}
\begin{tikzpicture}
\pic (Root) {mynode};
\pic[above right=of Root-frame] (L) {mynode};
\pic[below right=of Root-frame] (R) {mynode};
\begin{scope}[node distance=5pt and 1cm]
\pic[above right=of L-frame] (LL) {mynode};
\pic[below right=of L-frame] (LR) {mynode};
\pic[above right=of R-frame] (RL) {mynode};
\pic[below right=of R-frame] (RR) {mynode};
\end{scope}
\foreach \Nodoi/\Nodof in {
Root/L,L/LL,R/RL}
{
\draw[ar]
(\Nodoi-r) -- (\Nodof-frame.west);
}
\foreach \Nodoi/\Nodof in {
Root/R,R/RR,L/LR}
{
\draw[ar]
(\Nodoi-l) -- (\Nodof-frame.west);
}
\end{tikzpicture}
\end{document}
更新
下面是同样的方法,展示了它如何接受覆盖beamer
:
代码:
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}
\tikzset{
invisible/.style={opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}%
},
}
\tikzset{
mynode/.pic={
\node[overlay,draw,circle,inner sep=3pt] at (0,-7.5pt) (-l) {};
\node[overlay,draw,fill=gray!60,circle,inner sep=3pt] at (0,7.5pt) (-r) {};
\node[overlay,inner sep=5pt,draw,fit={(-l) (-r)}] (-frame) {};
},
ar/.style={
->,
>=latex
}
}
\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}
\pic[visible on=<1->] (Root) {mynode};
\pic[above right=of Root-frame,visible on=<2->] (L) {mynode};
\pic[below right=of Root-frame,visible on=<2->] (R) {mynode};
\begin{scope}[node distance=5pt and 1cm]
\pic[above right=of L-frame,visible on=<3->] (LL) {mynode};
\pic[below right=of L-frame,visible on=<3->] (LR) {mynode};
\pic[above right=of R-frame,visible on=<3->] (RL) {mynode};
\pic[below right=of R-frame,visible on=<3->] (RR) {mynode};
\end{scope}
\draw[ar,visible on=<2->]
(Root-r) -- (L-frame.west);
\draw[ar,visible on=<3->]
(L-r) -- (LL-frame.west);
\draw[ar,visible on=<3->]
(R-r) -- (RL-frame.west);
\draw[ar,visible on=<2->]
(Root-l) -- (R-frame.west);
\draw[ar,visible on=<3->]
(R-l) -- (RR-frame.west);
\draw[ar,visible on=<3->]
(L-l) -- (LR-frame.west);
\end{tikzpicture}
\end{frame}
\end{document}
更新
对于 PGF 版本 2.10,显示所需结构并承认覆盖规范:
\documentclass{beamer}
\usepackage{tikz}
\tikzset{
invisible/.style={opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}%
},
ar/.style={
->,
>=latex
}
}
\newcommand\twonodes[1]{%
\begin{tikzpicture}[remember picture,overlay]
\node[draw,minimum height=30pt,minimum width=15pt] (#1) {};
\node[draw,fill=gray!60,circle,inner sep=3pt] at ([yshift=-7pt]#1.north) (#1-u) {};
\node[draw,circle,inner sep=3pt] at ([yshift=7pt]#1.south) (#1-l) {};
\end{tikzpicture}%
}
\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}[
remember picture,
grow=right,
sloped,
level 1/.style={sibling distance = 2.6cm, level distance = 1.5cm},
level 2/.style={sibling distance = 1.3cm},
level 3/.style={sibling distance = 1cm},
edge from parent/.style={draw=none}
]
\node[visible on=<1->] {\twonodes{Root} }
child { node[visible on=<5->] {\twonodes{L}}
child { node[visible on=<7->] {\twonodes{LL}} }
child { node[visible on=<6->] {\twonodes{LU}} }
}
child { node[visible on=<2->] {\twonodes{U}}
child { node[visible on=<4->] {\twonodes{UL}} }
child { node[visible on=<3->] {\twonodes{UU}} }
};
\draw[ar,visible on=<2->]
(Root-u) -- (U.west);
\draw[ar,visible on=<3->]
(U-u) -- (UU.west);
\draw[ar,visible on=<4->]
(U-l) -- (UL.west);
\draw[ar,visible on=<5->]
(Root-l) -- (L.west);
\draw[ar,visible on=<6->]
(L-u) -- (LU.west);
\draw[ar,visible on=<7->]
(L-l) -- (LL.west);
\end{tikzpicture}
\end{frame}
\end{document}
结果动画如下:
答案2
我认为这个解决方案更加干净。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\newcommand{\twonodes}[2]{%
\begin{tikzpicture}[remember picture,node distance=0.6cm]
\node[ circle, fill=gray] (#1) {};
\node[circle, below of = #1](#2) {};
\end{tikzpicture}
}
\begin{document}
\begin{frame}{}
\begin{tikzpicture}[remember picture,->,>=stealth',grow=right,yscale=1.3,xscale=1.2,
level 1/.style={sibling distance = 2.6cm, level distance = 1.5cm},
level 2/.style={sibling distance = 1.3cm},
every node/.style={rectangle, draw=black,thin, minimum size = 0.4cm},
norm/.style={edge from parent/.style={white,thin,draw}}]
\node[norm] {\twonodes{1}{2}}
child[norm] { node (3) {\twonodes{4}{5}}
child[norm] { node (6) {\twonodes{7}{8}} }
child[norm] { node (9) {\twonodes{10}{11}} }
}
child[norm] { node (12) {\twonodes{13}{14}}
child[norm] { node (15) {\twonodes{16}{17}} }
child[norm] { node (18) {\twonodes{19}{20}} }
};
\path (1) edge (12)
(2) edge (3)
(4) edge (9)
(5) edge (6)
(13) edge (18)
(14) edge (15);
\end{tikzpicture}
\end{frame}
\end{document}
答案3
这是一个(设计非常糟糕的)自定义形状,仅用于说明另一种方法,该方法可以根据子节点的数量对父节点进行参数化。仅允许两个子节点,但每个节点都可以使用和键circle 1
来circle 1
指定内圆的样式:
\documentclass[tikz,border=5]{standalone}
\pgfdeclareshape{twodots}{
\nodeparts{}
\anchor{center}{\pgfpointorigin}
\anchor{text}{\pgfpointorigin}
\anchor{circle 1}{\pgfpointxy{1/6}{1/4}}
\anchor{circle 2}{\pgfpointxy{1/6}{-1/4}}
\anchor{west}{\pgfpointxy{-1/4}{0}}
\behindbackgroundpath{
\draw (-1/4,-1/2) rectangle (1/4,1/2);
\draw [circle 2/.try] (0,-1/4) circle [radius=1/6];
\draw [circle 1/.try] (0,1/4) circle [radius=1/6];
}
\anchorborder{\pgfgetlastxy\x\y
\pgfpointborderrectangle{\pgfqpoint{\x}{\y}}{\pgfpointxy{1/2}{1}}}
}
\begin{document}
\begin{tikzpicture}[every node/.style={twodots}, >=stealth,
edge from parent path={ [->, shorten >=.0625cm] (\tikzparentnode.circle \the\tikznumberofcurrentchild) -- (\tikzchildnode.west) },
circle 1/.style={fill=gray!50},
level distance=2cm,
level 1/.style={sibling distance=5cm},
level 2/.style={sibling distance=5cm/2},
level 3/.style={sibling distance=5cm/4}]
\node {} [grow'=right]
child { node {}
child { node {}
child { node {} }
child { node {} }
}
child { node {} }
}
child { node {}
child { node {}
child { node {} }
child { node {} }
}
child { node {}
child { node {} }
child { node {} }
}
};
\end{tikzpicture}
\end{document}