我是 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}