我正在尝试使用 TikZ 创建一个简单的流程图(它实际上是一个称为哈里斯矩阵的考古图)。下面是完成的图表的一小部分代码,说明了我遇到的问题。在当前图表中,从标记为 7 的节点开始的线向下延伸,然后接触右侧标记为 9 的节点。我想要做的是让线仍然从节点 7 向下延伸,向左延伸,然后再次向下延伸以接触顶部的节点 9。感谢您的帮助,我对 TikZ 还很陌生。
我现在拥有的:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\tikzstyle{block} = [rectangle, draw, text centered]
\tikzstyle{line} = [draw]
\[ \begin{tikzpicture}
\node [block] (g) {7};
\node [block, below left of = g] (h) {9};
\node [block, below right of = g] (i) {12};
\path [line] (g) |- (h);
\path [line] (g) |- (i);
\end{tikzpicture} \]
\end{document}
答案1
使用以下语法的一种可能性let
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,shapes,arrows}
\begin{document}
\tikzset{
block/.style={rectangle, draw, text centered},
line/.style={draw}
}
\[
\begin{tikzpicture}
\node [block] (g) {7};
\node [block, below left of = g] (h) {9};
\node [block, below right of = g] (i) {12};
\path[line] let \p1=(g.south), \p2=(h.north) in (g.south) -- +(0,0.5*\y2-0.5*\y1) -| (h.north);
\path [line] let \p1=(g.south), \p2=(i.north) in (g.south) -- +(0,0.5*\y2-0.5*\y1) -| (i.north);
\end{tikzpicture}
\]
\end{document}
也可以进行一些手动调整,但如果使用了错误的移位,这可能会产生不良结果:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,shapes,arrows}
\begin{document}
\tikzset{
block/.style={rectangle, draw, text centered},
line/.style={draw}
}
\[
\begin{tikzpicture}
\node [block] (g) {7};
\node [block, below left of = g] (h) {9};
\node [block, below right of = g] (i) {12};
\path[line] (g.south) -- +(0,-3pt) -| (h.north);
\path[line] (g.south) -- +(0,-3pt) -| (i.north);
\end{tikzpicture}
\]
\end{document}