![如何在 TikZ 中在节点和线之间画一条线?](https://linux22.com/image/322592/%E5%A6%82%E4%BD%95%E5%9C%A8%20TikZ%20%E4%B8%AD%E5%9C%A8%E8%8A%82%E7%82%B9%E5%92%8C%E7%BA%BF%E4%B9%8B%E9%97%B4%E7%94%BB%E4%B8%80%E6%9D%A1%E7%BA%BF%EF%BC%9F.png)
我是 TikZ 的初学者。我想绘制一个包含 2 个节点的简单网络和一个如下网络:
我的代码在这里:
\documentclass[border=1pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows, shapes, trees, calc}
\begin{document}
% Define styles
\tikzstyle{bnode}=[draw, fill=blue!20, text width=5em, text centered, minimum height=2.5em]
\begin{tikzpicture}
% draw two nodes
\node (node1) [bnode] at (0, 0) {Node 1};
\node (node2) [bnode] at (4, 0) {Node 2};
% draw network
% name this line to line1
\draw[line width=2pt] ($(node1.west |- node1.south)+(0, -.5)$) -- ($(node2.east |- node2.south)+(0, -.5)$);
% and draw a line between node1 and line1 like this:
% \draw[line width=2pt] (node1) -- (line1);
\draw[line width=2pt] (node1) -- (0, -.95);
\draw[line width=2pt] (node2) -- (4, -.95);
\end{tikzpicture}
\end{document}
我制作了根据Node1和Node2的位置绘制的水平线,但是不知道如何绘制相对的垂直线。你能帮忙优化一下代码吗?
答案1
像这样?我不确定它是否经过优化 - 这可能取决于你的想法。
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{% \tikzstyle is deprecated
bnode/.style={draw, fill=blue!20, text width=5em, text centered, minimum height=2.5em, line width=.4pt, above=.95}}
\begin{tikzpicture}
\path [draw, line width=2pt] (0,0) coordinate (o) node (node 1) [bnode, anchor=west] {Node 1} -- (6,0) node (node 2) [bnode, anchor=east] {Node 2} (o -| node 1.south) -- (node 1.south) (o -| node 2.south) -- (node 2.south);
\end{tikzpicture}
\end{document}