![tikzpicture:使用相对位置控制画线的路径](https://linux22.com/image/427537/tikzpicture%EF%BC%9A%E4%BD%BF%E7%94%A8%E7%9B%B8%E5%AF%B9%E4%BD%8D%E7%BD%AE%E6%8E%A7%E5%88%B6%E7%94%BB%E7%BA%BF%E7%9A%84%E8%B7%AF%E5%BE%84.png)
\documentclass[border=80pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[on grid]
\draw [help lines] (0, 0) grid (3, 2);
\node (A) at (0, 0) {A};
\node[above right = 1 and 1 of A] (B) {B};
\node[above right = 1 and 1 of B] (C) {C};
\node[right = 3 of A] (D) {D};
\draw[->] (A) -- (0.8, 0.1) -- (B);
\draw[->] (B) -- (C);
\draw[->] (C) -- (D);
\end{tikzpicture}
\end{document}
我画了 4 个节点和 3 条线。当连接A
和 时B
,我希望线穿过(0.8, 01)
,它通过使用\draw[->] (A) -- (0.8, 0.1) -- (B);
但是当我使用相对位置时,tikz 无法解析坐标: \draw[->] (A) -- [above right = 0.1 and 0.8 of A] -- (B);
。那么我可以使用相对位置[above right = 0.1 and 0.8 of A]
来控制线条绘制的路径吗?
更新:
@Ignasi 提出了一个有趣的解决方案,不幸的是我无法使用关键字重现它shift
:
\documentclass[border=80pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[line width=1pt, on grid]
\draw [help lines] (0, 0) grid (3, 2);
\node (A) at (0, 0) {A};
\node[above right = 1 and 1 of A] (B) {B};
\node[above right = 1 and 1 of B] (C) {C};
\node[right = 3 of A] (D) {D};
%\draw[->] (A) -- (1, 0) -- (B);
\draw[->] (A) -- ([shift={(0, -1)}]B) -- (B);
\draw[->] (B) -- (C);
\draw[->] (C) -- (D);
\end{tikzpicture}
\end{document}
答案1
您可以使用该calc
库,然后可以将其作为 来引用($(A)+(0.8, 0.1)$)
。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[on grid]
\draw [help lines] (0, 0) grid (3, 2);
\node (A) at (0, 0) {A};
\node[above right = 1 and 1 of A] (B) {B};
\node[above right = 1 and 1 of B] (C) {C};
\node[right = 3 of A] (D) {D};
\draw[->] (A) -- ($(A)+(0.8, 0.1)$) -- (B);
\draw[->] (B) -- (C);
\draw[->] (C) -- (D);
\end{tikzpicture}
\end{document}
答案2
\documentclass[border=80pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[on grid]
\draw [help lines] (0, 0) grid (3, 2);
\node (A) at (0, 0) {A};
\node[above right = 1 and 1 of A] (B) {B};
\node[above right = 1 and 1 of B] (C) {C};
\node[right = 3 of A] (D) {D};
\draw[->, red] (A) -| (B);
\draw[->, green] (A) |- (B);
\end{tikzpicture}
\end{document}