![相对于多个节点定位节点](https://linux22.com/image/318308/%E7%9B%B8%E5%AF%B9%E4%BA%8E%E5%A4%9A%E4%B8%AA%E8%8A%82%E7%82%B9%E5%AE%9A%E4%BD%8D%E8%8A%82%E7%82%B9.png)
我想放置一个相对于多个节点的节点。在此邮政,calc
其中一个答案中建议使用该库。在下图中,居中节点 (E) 并未位于所有节点的正中心。
我可以通过确定每个节点的精确位置来解决这个问题,但我想利用positioning
包来自动执行它。有没有办法自动定位一个节点相对于多个节点的位置?(我的意思是像below=of right=of ... etc
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations.pathmorphing, backgrounds, positioning,fit,petri}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
[%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
place/.style={circle,draw=blue!50,fill=blue!20,thick,
inner sep=0pt,minimum size=6mm}
]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\node[place] (A) {A};
\node[place] (B) [right=of A] {B};
\node[place] (C) [below=of B] {C};
\node[place] (D) [left=of C] {D};
\coordinate (Middle) at ($(A)!0.5!(B)$);
\node[place] (E) [below of=Middle] {E};
\end{tikzpicture}
\end{document}
答案1
您可以使用baricentric cs
来完成此类作业。有关详细信息,请参阅pgfmanual
,第 133 页,第 13.2.2 节。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, decorations.pathmorphing, backgrounds, positioning,fit,petri}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
[%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
place/.style={circle,draw=blue!50,fill=blue!20,thick,
inner sep=0pt,minimum size=6mm}
]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\node[place] (A) {A};
\node[place] (B) [right=of A] {B};
\node[place] (C) [below=of B] {C};
\node[place] (D) [left=of C] {D};
\coordinate (Middle) at ($(A)!0.5!(B)$);
\node[place] (E) at (barycentric cs:A=0.5,B=0.5,C=0.5,D=0.5) {E};
\end{tikzpicture}
\end{document}
您可以调整它们0.5
来改变位置。
答案2
重心坐标系Harish 的回答提供了一个相当通用的方法;然而,在这个具体例子中,你可以使用calc
库(已经加载到你的代码中),只需说
\node[place] (E) at ( $ (A)!0.5!(C) $ ) {E};
完整代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[
place/.style={circle,draw=blue!50,fill=blue!20,thick,inner sep=0pt,minimum size=6mm}
]
\node[place] (A) {A};
\node[place] (B) [right=of A] {B};
\node[place] (C) [below=of B] {C};
\node[place] (D) [left=of C] {D};
\node[place] (E) at ( $ (A)!0.5!(C) $ ) {E};
\end{tikzpicture}
\end{document}