此脚本
\documentclass\[tikz,border=5pt\]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\begin{document}
\begin{tikzpicture}
\node\[draw, align = center,
minimum height = 1.5cm,
text width = 1.5cm\]
(a) at (0, 0){a};
\node\[draw\](b) at (a.north east)\[anchor= north west\]{b};
\end{tikzpicture}
\end{document}
提供预期的输出,其中节点b
相对于无节点定位a
:
好的!
rounded rectangle
然而,在节点上添加形状( )a
会破坏这种和谐:
\node[draw, align = center,
minimum height = 1.5cm,
text width = 1.5cm]
(a) at (0, 0)[rounded rectangle]{a};
预期输出为:
其中红线表示节点周围的假想边界a
。
如何解决这个问题呢?
答案1
根据你想要的,你可以做
\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\begin{document}
\begin{tikzpicture}
\node[draw, align = center,
minimum height = 1.5cm,
text width = 1.5cm]
(a) at (0, 0)[rounded rectangle]{a};
\node[draw](b) at (a.east |- a.north)[anchor=north west]{b};
\end{tikzpicture}
\end{document}
主要问题是你想实现什么。很明显,锚点north east
不在角落,因为 arounded rectangle
没有角。所以我“发明”了一个角。(a.east |- a.north)
正是a
节点周围矩形的一个假想角,
当然也可以让b
节点触及的边界a
。
\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.misc,intersections}
\begin{document}
\begin{tikzpicture}
\node[draw, align = center,name path=rr,
minimum height = 1.5cm,
text width = 1.5cm]
(a) at (0, 0)[rounded rectangle]{a};
\node[opacity=0,overlay](phantomb) at (a.east |- a.north)[anchor=north west]{b};
\path[name path=aux] (phantomb.south west) -- ++(-1,0);
\draw[name intersections={of=rr and aux}] node[draw](b) at (intersection-1)
[anchor=south west]{b};
\end{tikzpicture}
\end{document}
还有其他可能性,实际上取决于您到底想要实现什么。
附录:两个这样的形状的相对定位。我不知道你到底想要什么,但这是一种可能性。
\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.misc,intersections,calc}
\begin{document}
\begin{tikzpicture}
\node[draw, align = center,name path=rr,
minimum height = 1.5cm,
text width = 1.5cm]
(a) at (0, 0)[rounded rectangle]{a};
\node[opacity=0,rounded rectangle,overlay](phantomb) at (a.east |- a.north)[anchor=north west]{b};
\path[name path=aux] (phantomb.south west) -- ++(-1,0);
\draw[name intersections={of=rr and aux}]
let \p1=($(phantomb.south west)-(phantomb.south -| phantomb.west)$) in
node[draw,rounded rectangle](b) at ([xshift=\x1]intersection-1)
[anchor=south west]{b};
\end{tikzpicture}
\end{document}
编辑:添加overlay
以防止幻影节点扩大边界框。