如何将一个node
坐标相对于另一个坐标放置?理论上:
\begin{tikzpicture}
\node (source) {X};
\node (destn) {Y} at source.coordinates +(2,1);
\end{tikzpicture}
会产生类似这样的结果:
Y
X
我知道right=of somenode
和类似的键,但它们似乎使用极坐标: 的节点below right=of somenode
不会与定位的其他东西垂直对齐right=of somenode
。
如何才能做到这一点?
看看这个:我正在做的事情并不比你们发布的示例复杂多少。尽管我已经positioning
加载了,三个加法器没有垂直对齐. (这就是让我相信该语法使用极坐标的原因)
\begin{tikzpicture}
% Place everything
\node (source) {\(X\)};
\coordinate[right= of source] (split) {};
\node [circle, draw, above right=of split] (adder1) {\(+\)} ;
\node [circle, draw, right=of split] (adder2) {\(+\)};
\node [circle, draw, below right=of split] (adder3) {\(+\)};
% Place edges
\draw [->] (source) -- (split);
\draw [->] (split) |- (adder1);
\draw [->] (split) -- (adder2);
\draw [->] (split) |- (adder3);
\end{tikzpicture}
语法shift
有效,所以这是我暂时使用的,尽管使用更具语义的语法会更好。
谢谢
答案1
如果您加载positioning
库,则将below right=of
与节点垂直对齐right=of
。但是,您可以使用shift
将节点相对于现有节点放置。此外,您还可以使用positioning
库对距离进行硬编码。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (source) {X};
\node (destn) at ([shift={(2,1)}]source) {Y};
\end{tikzpicture}
With \verb|positioning| library
\begin{tikzpicture}
\node (source) {X};
\node[below right= of source] (destn) {Y};
\node[right= of source] (destnn) {Y};
\end{tikzpicture}
With \verb|positioning| library and hard coded distances
\begin{tikzpicture}
\node (source) {X};
\node[below right= 2cm and 3cm of source] (destn) {Y};
\node[right= 3cm of source] (destnn) {Y};
\end{tikzpicture}
\end{document}
答案2
与 Harish 的回答类似,但有更多关于定位语法的细节
我认为语法有点混乱positioning
。它不使用极坐标,但如果只给出一个参数,它适用于水平和垂直移位。所以你可以说below right = <horizontal shift> and <vertical shift> of <ref>
它还会将要放置的对象的锚点更改为给定键的对立面。换句话说,如果您说below = of a
那么对象会自动假设anchor=north
,或者类似地,如果您说above right= of a
那么对象会自动假设anchor=below left
这是一个测试示例
\usetikzlibrary{positioning}
\begin{tikzpicture}
\node (source) {X};
\node (test) at ([shift={(2,1)}]source) {t};
\node[above right=1cm and 2cm of source.center,anchor=center] (destn) {Y};
\end{tikzpicture}
答案3
您的问题在pgfmanual
,章节“17.5.3 高级安置选项”。这一节很长,但值得一读。
\node [circle, draw, right=of split] (adder2) {\(+\)};
right=of split
相当于从 向右right=1cm of split
移动1cm并将锚点固定到。adder2
split
adder2
west
\node [circle, draw, above right=of split] (adder1) {\(+\)};
above right= of split
相当于above right=1cm of split
(不同于above right=1cm and 1cm of split
)。
从pgfmanual
<shifting part>
当的形式为时<number or dimension>
,节点将按<number or dimension>
135º 的方向移动。这意味着和 之间存在<shifting part>
差异:在第二种情况下,节点向上移动 1cm 并向右移动 1cm;在第一种情况下,它向上移动 1/2√2cm 并向右移动相同的量。1cm
1cm and 1cm
一旦移动,锚点就会被设置为south west
。这就是垂直对齐问题的开始。adder1
和adder2
是圆形节点,在圆形节点中west
,south west
锚点并不像矩形节点那样处于同一垂直方向上,如下图所示:
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (2,2);
\coordinate (split) at (0,0) {};
\node [draw=red, above right=1cm and 1cm of split] (adder1) {\(+\)} ;
\node [draw=green, above right=1cm of split] (adder1) {\(+\)} ;
\node [draw, right=1cm of split] (adder1) {\(+\)} ;
\draw[dotted] (1,0) arc[start angle=0,end angle=90,radius=1cm];
\begin{scope}[xshift=3cm]
\draw[help lines] (0,0) grid (2,2);
\coordinate (split) at (0,0) {};
\node [circle, draw=red, above right=1cm and 1cm of split] (adder1) {\(+\)} ;
\node [circle, draw=green, above right=1cm of split] (adder1) {\(+\)} ;
\node [circle, draw, right=1cm of split] (adder1) {\(+\)} ;
\draw[dotted] (1,0) arc[start angle=0,end angle=90,radius=1cm];
\end{scope}
\end{tikzpicture}
\end{document}
如果您想垂直对齐用声明的圆形节点above right
,我知道的唯一解决方案就是明确设置anchor=center
。
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (2,2);
\coordinate (split) at (0,0) {};
\node [circle, draw=red, above right=1cm and 1cm of split, anchor=center] (adder1) {\(+\)} ;
\node [circle, draw=green, above right=1cm of split, anchor=center] (adder1) {\(+\)} ;
\node [circle, draw, right=1cm of split, anchor=center] (adder1) {\(+\)} ;
\draw[dotted] (1,0) arc[start angle=0,end angle=90,radius=1cm];
\end{tikzpicture}
\end{document}