![在拟合内定位拟合节点](https://linux22.com/image/350740/%E5%9C%A8%E6%8B%9F%E5%90%88%E5%86%85%E5%AE%9A%E4%BD%8D%E6%8B%9F%E5%90%88%E8%8A%82%E7%82%B9.png)
我正在尝试绘制如下所示的嵌套结构。想法是让下方框中的节点完美地融入绘制的轮廓中。如何做到这一点?
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (0) {Hello};
\node (1) [right=3cm of 0] { World};
\draw (1) -- (0) node [below] {} ;
\node [fit= (1) (0), draw] (4) {};
\node (2) [below= of 0, ] {it's};
\node (3) [right=3cm of 2] {really really here};
\draw (2) -- (3) node [below] {};
\node [fit= (2) (3), below=of 4, draw] (5) {};
\draw (4) -- (5) node [right] {} ;
\node [fit= (4) (5), draw] (6) {};
\end{tikzpicture}
\end{document}
我所能做的就是这个,但是这里,结构没有正确对齐,因为我必须在节点 2 和 3 组合在一起之前给出节点 2 的位置。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (0) {Hello};
\node (1) [right=3cm of 0] { World};
\draw (1) -- (0) node [below] {} ;
\node [fit= (1) (0), draw] (4) {};
\node (2) [below= of 0, ] {it's};
\node (3) [right=3cm of 2] {really really here};
\draw (2) -- (3) node [below] {};
\node [fit= (2) (3), draw] (5) {};
\draw (4) -- (5) node [right] {} ;
\node [fit= (4) (5), draw] (6) {};
\end{tikzpicture}
\end{document}
答案1
我假设你喜欢这样的东西:
\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{fit, positioning}
\begin{document}
\begin{tikzpicture}[node distance = 12mm and 0mm]
\node (0) {Hello};
\node (1) [right=3cm of 0] {World};
\draw (0) -- (1);
\node (a) [draw, fit=(0) (1)] {};
\node (2) [below right=of 0.west] {it's};
\node (3) [right=3cm of 2] {really, really here};
\draw (2) -- (3);
\node (b) [draw,fit=(2) (3)] {};
\draw (a) -- (a |- b.north);
\node (c) [draw,fit=(a) (b)] {};
\end{tikzpicture}
\end{document}
请注意:
- 没有人你的MWE工作缺少必不可少的包和库
- 使用的函数未在 MWE 中定义(我将其删除)
- 问题不清楚,如果节点必须像上图所示那样定位,或者你喜欢水平居中
附录:水平居中,那么什么应该居中?文本周围的节点用线连接?对此的第一个近似值是:
\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{fit, positioning}
\begin{document}
\begin{tikzpicture}[node distance = 12mm and 15mm]
\node (n1) {it's};
\node (n2) [right=3cm of n1] {really, really here};
\draw (n1) -- (n2);
\node (n3) [draw,fit=(n1) (n2)] {};
\node (n4) [above left=of n3.north] {Hello};
\node (n5) [above right=of n3.north] {World};
\draw (n4) -- (n5);
\node (n6) [draw, fit=(n4) (n5)] {};
\draw (n3) -- (n3 |- n6.south);
\node (n7) [draw,fit=(n3) (n6)] {};
\end{tikzpicture}
\end{document}
答案2
我认为简单方法\rule
可以简化问题,因为它将两个节点+拟合节点转换为一个普通节点。
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning, fit}
\begin{document}
\begin{tikzpicture}
\node[draw] (a) {Hello \rule[.5ex]{3cm}{.4pt} World};
\node[draw, below=of a] (b) {it's \rule[.5ex]{3cm}{.4pt} really, really here};
\draw (a)--(b);
\node[draw, fit=(a) (b)]{};
\end{tikzpicture}
\end{document}