正如所描述的那里,下面的代码无法编译:
\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node at (0, 0) (node1) {Hello};
\node at (0, -2) (node2) {World};
% working:
\draw ($ (node1.south) + (1,0) $) to ( node1.south |- node2.north);
% not working:
\draw ($ (node1.south) + (1,0) $) to ( ($ (node1.south) + (1,0) $) |- node2.north);
\end{tikzpicture}
\end{document}
为什么?如何让它发挥作用?
答案1
line to
手册中给出的操作的语法是放置|-
在两个坐标之间:
有时你想通过仅水平和垂直的直线连接两个点。为此,你可以使用两个路径构造操作。
\path . . . -|<坐标或循环> . . . ;
这个操作的意思是“先横,后竖”。
以下为示例:
\begin{tikzpicture}
\draw (0,0) node(a) [draw] {A} (1,1) node(b) [draw] {B};
\draw (a.north) |- (b.west);
\draw[color=red] (a.east) -| (2,1.5) -| (b.north);
\end{tikzpicture}
因此,这种双路径操作并非旨在将一个点平移到另一个点。当您需要先绘制水平线,然后绘制垂直线或反之亦然时,这是一种方便的快捷方式。
您只需要在代码中(语法上)写入:
\draw ($ (node1.south) + (1,0) $) to ($ (node1.south) + (1,0) $) |- (node2.north);
代替
\draw ($ (node1.south) + (1,0) $) to ( ( ( $ (node1.south) + (1,0) $) |- node2.north);`
你的代码变成这样:
\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node at (0, 0) (node1) {Hello};
\node at (0, -2) (node2) {World};
% working:
%\draw ($ (node1.south) + (1,0) $) to ( node1.south |- node2.north);
% now working too:
\draw ($ (node1.south) + (1,0) $) to ($ (node1.south) + (1,0) $) |- (node2.north);
\end{tikzpicture}
\end{document}
要获得与所需路径相同的路径,您必须按照@marmot 或@ignasi 的指示构建您的路径。
答案2
只是为了完整性:一个版本实际上与第一个版本相当。AndreC 的回答很好,是正确的,但我不明白第二条路径(有一个拐角)是如何成为第一条路径的移位版本的。
\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node at (0, 0) (node1) {Hello};
\node at (0, -2) (node2) {World};
% working:
\draw ($ (node1.south) + (1,0) $) to ( node1.south |- node2.north);
% not working:
\draw ($ (node1.south) + (1,0) $) to ([xshift=1cm] node1.south |- node2.north);
\end{tikzpicture}
\end{document}
答案3
您始终可以声明辅助坐标并使用它。这样您就无需记住哪个是有效的语法 ;-)
\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node at (0, 0) (node1) {Hello};
\node at (0, -2) (node2) {World};
% working:
\draw ($ (node1.south) + (1,0) $) to ( node1.south |- node2.north);
% working:
\draw ($ (node1.south) + (1,0) $) coordinate (aux) to (aux |- node2.north);
\end{tikzpicture}
\end{document}