我在 tikzpicture 中有两个节点,两个节点中都有一个嵌套的 tikzpicture 图形。现在,我希望有一个箭头从一个图形指向另一个图形 - 从一个图形的中心指向另一个图形的中心。这就是图形位于节点中的原因 - 我可以用箭头连接两个节点。问题是第二个图形中的边缘标签没有精确地放置在边缘上。 代码:
\documentclass[12pt,a4paper]{standalone}
\usepackage[czech]{babel}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,arrows,decorations.pathreplacing, snakes}
\begin{document}
\begin{tikzpicture}
\tikzstyle{every node}=[circle, minimum size=0pt, inner sep=0pt, outer sep=0pt, fill=white],
\tikzstyle{vertex}=[circle, minimum size=4pt, inner sep=0pt, fill=orange]
\node(graph) at (0,1) {
\begin{tikzpicture}[font=\footnotesize, thick]
\begin{scope}
\node[vertex] (root) at (4, 5) {};
\node[vertex] (o) at (4, 4) {};
\node[vertex] (oc) at (4, 3) {};
\node[vertex] (a) at (5, 4) {};
\foreach \xfrom/\xto/\xlabel in {
root/o/o, o/oc/c,
root/a/a} {
\draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
};
\end{scope}
\end{tikzpicture}
};
\node[right = of graph] (graph2) {
\begin{tikzpicture}[font=\footnotesize, thick]
\begin{scope}
\node[vertex] (root) at (4, 5) {};
\node[vertex] (o) at (4, 4) {};
\node[vertex] (oc) at (4, 3) {};
\node[vertex] (a) at (5, 4) {};
\foreach \xfrom/\xto/\xlabel in {
root/o/o, o/oc/c}
{
\draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
};
\foreach \xfrom/\xto/\xlabel in {
root/a/a}
{
\draw (\xfrom) to node[midway]{\xlabel} (\xto);
};
\end{scope}
\end{tikzpicture}
};
\begin{scope}[segment amplitude=4]
\draw[snake=triangles] (graph) -- (graph2);
\end{scope}
\end{tikzpicture}
\end{document}
可能的解决方案是使用pic
,但这并不是我想要的,因为它在两个图中选择了一个精确点并连接它们而不是图本身(因此每次图更改为从其中心引导时都必须重定向箭头)。
代码:
\documentclass[12pt,a4paper]{standalone}
\usepackage[czech]{babel}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,arrows,decorations.pathreplacing, snakes}
\begin{document}
\tikzset{every node/.style={circle, minimum size=0pt, inner sep=0pt, outer sep=0pt, fill=white},
vertex/.style={circle, minimum size=4pt, inner sep=0pt, fill=orange}}
\tikzset{
mygraph/.pic={
\begin{scope}[font=\footnotesize, thick]
\node[vertex] (root) at (4, 5) {};
\node[vertex] (o) at (4, 4) {};
\node[vertex] (oc) at (4, 3) {};
\node[vertex] (oco) at (4, 2) {};
\node[vertex] (a) at (5, 4) {};
\foreach \xfrom/\xto/\xlabel in {
root/o/o, o/oc/c, oc/oco/o,
root/a/a} {
\draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
};
\end{scope}
}
}
\begin{tikzpicture}
\pic (graph) at (0,0) {mygraph=Hallo};
\pic (graph2) at (3,0) {mygraph=Hallo};
\path (grapha) -- (graph2o) coordinate[pos=0.2] (start) coordinate[pos=0.8] (end); \draw[snake=triangles,segment amplitude=4] (start) to (end);
\end{tikzpicture}
\end{document}
答案1
tikzpicture
这是不建议嵌套的一个例子。
另一种选择可能是两个,每个设置scopes
一个,第二个设置一个。local bounding box
xshift
\documentclass[12pt,a4paper,border=5mm]{standalone}
\usepackage[czech]{babel}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,arrows,decorations.pathreplacing, snakes}
\begin{document}
\begin{tikzpicture}[
every node/.style={circle, minimum size=0pt, inner sep=0pt, outer sep=0pt, fill=white},
vertex/.style={circle, minimum size=4pt, inner sep=0pt, fill=orange}
]
\begin{scope}[local bounding box=graph1]
\node[vertex] (root) at (4, 5) {};
\node[vertex] (o) at (4, 4) {};
\node[vertex] (oc) at (4, 3) {};
\node[vertex] (a) at (5, 4) {};
\foreach \xfrom/\xto/\xlabel in {
root/o/o, o/oc/c,
root/a/a} {
\draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
};
\end{scope}
\begin{scope}[local bounding box=graph2, xshift=3cm]
\node[vertex] (root) at (4, 5) {};
\node[vertex] (o) at (4, 4) {};
\node[vertex] (oc) at (4, 3) {};
\node[vertex] (a) at (5, 4) {};
\foreach \xfrom/\xto/\xlabel in {
root/o/o, o/oc/c}
{
\draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
};
\foreach \xfrom/\xto/\xlabel in {
root/a/a}
{
\draw (\xfrom) to node[midway]{\xlabel} (\xto);
};
\end{scope}
\path (graph1) -- (graph2) coordinate[pos=0.3] (start) coordinate[pos=0.8] (end);
\draw[snake=triangles,segment amplitude=4] (start) to (end);
\end{tikzpicture}
\end{document}
答案2
Torbjørn T. 的local bounding box
技巧非常高超,所以功劳应归于他。不过我想说的是,这个pic
小玩意儿有助于避免重复。然后人们可以用前缀方法轻松地引用这些图片的元素。
\documentclass[12pt,a4paper]{standalone}
\usepackage[czech]{babel}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,arrows,decorations.pathreplacing, snakes}
\begin{document}
\tikzset{every node/.style={circle, minimum size=0pt, inner sep=0pt, outer sep=0pt, fill=white},
vertex/.style={circle, minimum size=4pt, inner sep=0pt, fill=orange}}
\tikzset{
mygraph/.pic={
\begin{scope}[font=\footnotesize, thick]
\node[vertex] (root) at (4, 5) {};
\node[vertex] (o) at (4, 4) {};
\node[vertex] (oc) at (4, 3) {};
\node[vertex] (oco) at (4, 2) {};
\node[vertex] (a) at (5, 4) {};
\foreach \xfrom/\xto/\xlabel in {
root/o/o, o/oc/c, oc/oco/o,
root/a/a} {
\draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
};
\end{scope}
}
}
\begin{tikzpicture}
\begin{scope}[local bounding box=graph1]
\pic (graph1) at (0,0) {mygraph=Hallo};
\end{scope}
\begin{scope}[local bounding box=graph2]
\pic (graph2) at (3,0) {mygraph=Hallo};
\end{scope}
\draw[snake=triangles,segment amplitude=4] ([xshift=2mm]graph1.east) to
([xshift=-2mm]graph2.west);
% just for fun
\draw[-latex,shorten >=1mm,shorten <=1mm] (graph1a) to[out=-10,in=160] (graph2root);
\end{tikzpicture}
\end{document}
一种可能性是将图片包装到节点中。
\documentclass[12pt,a4paper]{standalone}
\usepackage[czech]{babel}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,shadows,arrows,decorations.pathreplacing,
snakes,fit,backgrounds}
\begin{document}
\tikzset{every node/.style={circle, minimum size=0pt, inner sep=0pt, outer sep=0pt, fill=white},
vertex/.style={circle, minimum size=4pt, inner sep=0pt, fill=orange}}
\tikzset{
mygraph/.pic={
\begin{scope}[font=\footnotesize, thick]
\node[vertex] (root) at (4, 5) {};
\node[vertex] (o) at (4, 4) {};
\node[vertex] (oc) at (4, 3) {};
\node[vertex] (oco) at (4, 2) {};
\node[vertex] (a) at (5, 4) {};
\foreach \xfrom/\xto/\xlabel in {
root/o/o, o/oc/c, oc/oco/o,
root/a/a} {
\draw (\xfrom) to node[pos=0.5]{\xlabel} (\xto);
};
\end{scope}
}
}
\begin{tikzpicture}
\pic (graph1) at (0,0) {mygraph=Hallo};
\pic (graph2) at (3,0) {mygraph=Hallo};
\begin{scope}[on background layer]
\node[fit=(graph1oco) (graph1a) (graph1root),rectangle] (leftgraph){};
\node[fit=(graph2oco) (graph2a) (graph2root),rectangle] (rightgraph){};
\end{scope}
\draw[snake=triangles,segment amplitude=4] ([xshift=2mm]leftgraph.east) to
([xshift=-2mm]rightgraph.west);
\end{tikzpicture}
\end{document}
另一种可能性是使用路径图片,但我认为这会变得更加混乱(但我可能会被证明是错的并且学到一些东西;-)。