\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds, calc, positioning, fit}
\begin{document}
\begin{tikzpicture}
\node[draw] (a) {A node};
\node[draw,align=center] (b) at (2,0.25) {Another\\node};
\draw[-stealth] ($(a.north east)!0.2!(a.south east)$) -- (a-|b.west);
\end{tikzpicture}
\end{document}
生产
然而,我希望隐身$(a.north east)!0.2!(a.south east)$)
出去水平移动直到与 相交b.west
。
我该如何做呢?
答案1
最简单的选择是定义一个辅助坐标:
\coordinate (start) at ($(a.north east)!0.2!(a.south east)$);
供以后使用:
\draw[-stealth] (start) -- (start-|b.west);
完整示例:
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds, calc, positioning, fit}
\begin{document}
\begin{tikzpicture}
\node[draw] (a) {A node};
\node[draw,align=center, anchor=south west,xshift=0.5cm] (b) at (a.south east) {Another\\node};
\coordinate (start) at ($(a.north east)!0.2!(a.south east)$);
\draw[-stealth] (start) -- (start-|b.west);
\end{tikzpicture}
\end{document}
结果:
笔记
我擅自将这些块垂直对齐。
答案2
一个解决方案是创建水平-
(和垂直|
)路径样式。
\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{calc}
\tikzset{
-/.style ={to path={(\tikztostart)--(\tikztostart-|\tikztotarget)\tikztonodes}},
|/.style ={to path={(\tikztostart)--(\tikztostart|-\tikztotarget)\tikztonodes}}
}
\begin{document}
\begin{tikzpicture}
\node[draw] (a) {A node};
\node[draw,align=center] (b) at (2,0.25) {Another\\node};
\draw[-stealth]
($(a.north east)!0.2!(a.south east)$) to[-] (b.west)
(a) edge[|] (b.north);
\end{tikzpicture}
\end{document}