这个问题直接关系到将 TikZ 树与其他节点对齐这是我前段时间问的。当我问这个问题的时候,我并没有意识到对齐多棵树和对齐一棵树是不一样的。事实上,Dmitry F. Volosnykh 提出的答案要求先创建树,然后再创建它必须与之对齐的其他节点。因此,这个问题应该命名为“将一棵 TikZ 树与其他节点对齐”。
但是,当你需要对齐多棵树时,它们不能同时出现在第一位。有些树必须出现在其他树之后。
我已经完成了上一个问题中给出的示例,作为我需要解决的完整示例,并揭示了我所谈论的问题。请注意,用于包含三棵树的三个拟合节点处于正确的位置。但所有树木都位于相同的坐标上,并叠加在“变换”框上。
这是我得到的图形:
当然,我希望能够将它们放置在各自的盒子中。
以下是代码:
\documentclass{article}
\usepackage{tikz,nicefrac,amsmath,pifont,graphicx}
\usetikzlibrary{arrows,snakes,backgrounds,patterns,matrix,shapes,fit,calc,positioning,trees}
\usepackage[graphics,tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}
\newlength{\imagewidth}
\newlength{\imagescale}
\graphicspath{{/Users/ludo/Documents/cours/xml/slides/gfx/}{/Users/ludo/Documents/cours/xml/slides/src/gfx/}}
\begin{document}
\begin{tikzpicture}[
>=stealth', semithick, node distance=1cm, level distance=7mm, level/.style={sibling distance=10mm/#1},
block/.style = {draw, rectangle, rounded corners, minimum height=1cm},
every node/.style={circle, draw, fill=none, anchor=north}
]
\node [block] (transfo) {\parbox{2.5cm}{\centering transformation}};
\node (source tree root) {}
child { node {} }
child { node {}
child { node {} }
child { node {} }
};
\node (source tree) [draw, rectangle, fit=(source tree root) (source tree root-1) (source tree root-2-2), left=of transfo] {};
\node [block] (source document) [left=of source tree] {\parbox{2cm}{\centering source document}};
\node (stylesheet tree root) {}
child { node {}
child { node {} }
child { node {} }
}
child { node {}
child { node {} }
child { node {} }
};
\node (stylesheet tree) [draw, rectangle, fit=(stylesheet tree root) (stylesheet tree root-1-1) (stylesheet tree root-2-2), above=of transfo] {};
\node [block] (stylesheet document) [above=of stylesheet tree] {stylesheet};
\node (result tree root) {}
child { node {}
child { node {} }
child { node {} }
}
child { node {}
child { node {} }
child { node {} }
};
\node (result tree) [draw, rectangle, fit=(result tree root) (result tree root-1-1) (result tree root-2-2), right=of transfo] {};
\node [block] (result document) [right=of result tree] {\parbox{2cm}{\centering result document}};
\begin{scope}[shorten >=1pt]
\draw [->] (source document) edge (source tree);
\draw [->] (source tree) edge (transfo);
\draw [->] (transfo) edge (result tree);
\draw [->] (result tree) edge (result document);
\draw [->] (stylesheet document) edge (stylesheet tree);
\draw [->] (stylesheet tree) edge (transfo);
\end{scope}
\end{tikzpicture}
\end{document}
答案1
有两个问题:一是您的树都没有放置在特定坐标上。因此它们都以 (0,0) 为根,使它们重叠。另一个问题是,left=of transfo
树边界节点的定位选项(等)会覆盖它们的拟合选项。
我能想到两种解决方法:
将树代码放在
\tikz{}
树边界节点内的命令中,如source tree
。您必须将选项传递给tikzpicture
要保留的更大环境。命名\tikzstyle
s 可能会有所帮助。然后仅使用树边界节点的定位选项。将树的根放置在其中
\node (source tree root) at (someplace)
,然后仅使用树边界节点的拟合选项。
我无法决定你应该采用哪种方法。我认为前者,因为它有助于定位树边界节点。
答案2
\tikz{}
使用Matthew Leingang 上述命令的第一种方法的改编示例是:
\documentclass{article}
\usepackage{tikz,nicefrac,amsmath,pifont,graphicx}
\usetikzlibrary{arrows,snakes,backgrounds,patterns,matrix,shapes,fit,calc,positioning,trees}
\usepackage[graphics,tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}
\newlength{\imagewidth}
\newlength{\imagescale}
\graphicspath{{/Users/ludo/Documents/cours/xml/slides/gfx/}{/Users/ludo/Documents/cours/xml/slides/src/gfx/}}
\begin{document}
\begin{tikzpicture}[
>=stealth', semithick, node distance=1cm, level distance=7mm, level/.style={sibling distance=10mm/#1},
block/.style = {draw, rectangle, rounded corners, minimum height=1cm},
every node/.style={circle, draw, fill=none, anchor=north},
]
%\node [outer sep=0pt, inner sep=0pt, draw=none] (transfo) {\includegraphics[width=1.5cm]{xslt1.0/machinery.jpg}};
\node [block] (transfo) {\parbox{2.5cm}{\centering transformation}};
\node (source tree) [draw=none, rectangle, left=of transfo] {\tikz{%
\node {}
child { node {} }
child { node {}
child { node {} }
child { node {} }
};
}};
\node [block] (source document) [left=of source tree] {\parbox{2cm}{\centering source document}};
\node (stylesheet tree) [draw=none, rectangle, above=of transfo] {\tikz{%
\node {}
child { node {}
child { node {} }
child { node {} }
}
child { node {}
child { node {} }
child { node {} }
};
}};
\node [block] (stylesheet document) [above=of stylesheet tree] {stylesheet};
\node (result tree) [draw=none, rectangle, right=of transfo] {\tikz{%
\node (result tree root) {}
child { node {}
child { node {} }
child { node {} }
}
child { node {}
child { node {} }
child { node {} }
};
}};
\node [block] (result document) [right=of result tree] {\parbox{2cm}{\centering result document}};
\begin{scope}[shorten >=1pt]
\draw [->] (source document) edge (source tree);
\draw [->] (source tree) edge (transfo);
\draw [->] (transfo) edge (result tree);
\draw [->] (result tree) edge (result document);
\draw [->] (stylesheet document) edge (stylesheet tree);
\draw [->] (stylesheet tree) edge (transfo);
\end{scope}
\end{tikzpicture}
\end{document}
生成的图片为: