我正在尝试绘制一个带有节点和边权重的图形。以下是我目前所做的工作。我放置了节点名称。但我想将节点权重放在圆圈下。我也想放置边权重。我不知道如何放置边标签。以下是我当前的代码。
\begin{tikzpicture}[transform shape]
\node[vertex][](t) at (2, 0) {$ \omega_{i} $};
\node[vertex][](r1) at (1, 2) {$ r^{\omega_{i}}_{1} $};
\node[vertex][](r2) at (3, 2) {$ r^{\omega_{i}}_{2} $};
\node[vertex][](q1) at (0,-2) {$ q^{\omega_{i}}_{1} $};
\node[vertex][](q2) at (2,-2) {$ q^{\omega_{i}}_{2} $};
\node[vertex][](q3) at (4,-2) {$ q^{\omega_{i}}_{2} $};
\begin{scope}[every path/.style={-, dashed}]
\draw (t) -- (q1);
\draw (t) -- (q2);
\draw (t) -- (q3);
\draw (t) -- (r1);
\draw (t) -- (r2);
\end{scope}
\begin{scope}[every path/.style={-}]
\draw (r1) -- (q1);
\draw (r1) -- (q2);
\draw (r1) -- (q3);
\draw (r2) -- (q1);
\draw (r2) -- (q2);
\draw (r2) -- (q3);
\end{scope}
\end{tikzpicture}
答案1
您可以像这样添加边缘标签:\draw (t) -- node [anchor=south] {$p_1$} (q1)
。您必须尝试一下以获得正确的锚点(选择north
、south west
等,以查看效果)。您可能需要查看的其他参数是inner sep
(标签周围的空白区域)和pos
(确定标签出现在边缘的哪个位置)。
可以通过以下方式添加节点权重\node[vertex, label=below:$10$](t) at (2, 0) {$ \omega_{i} $};
(另请注意,您不需要额外的[]
)。
干得好:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikzstyle{vertex}=[circle, draw]
\begin{tikzpicture}[transform shape]
\node[vertex, label=right:$1$](t) at (4, 0) {$ \omega_{i} $};
\node[vertex, label=above:$2$](r1) at (2, 2) {$ r^{\omega_{i}}_{1} $};
\node[vertex, label=above:$3$](r2) at (6, 2) {$ r^{\omega_{i}}_{2} $};
\node[vertex, label=below:$4$](q1) at (0,-2) {$ q^{\omega_{i}}_{1} $};
\node[vertex, label=below:$5$](q2) at (4,-2) {$ q^{\omega_{i}}_{2} $};
\node[vertex, label=below:$6$](q3) at (8,-2) {$ q^{\omega_{i}}_{2} $};
\begin{scope}[every path/.style={-, dashed}, every node/.style={inner sep=1pt}]
\draw (t) -- node [anchor=north west] {$1$} (q1);
\draw (t) -- node [anchor=west] {$2$} (q2);
\draw (t) -- node [anchor=north east] {$3$} (q3);
\draw (t) -- node [anchor=north east] {$4$} (r1);
\draw (t) -- node [anchor=north west] {$5$} (r2);
\end{scope}
\begin{scope}[every path/.style={-}, every node/.style={inner sep=1pt}]
\draw (r1) -- node [anchor=east] {$6$} (q1);
\draw (r1) -- node [pos=0.15, anchor=north east] {$7$} (q2);
\draw (r1) -- node [pos=0.15, anchor=south west] {$8$} (q3);
\draw (r2) -- node [pos=0.15, anchor=south east] {$9$} (q1);
\draw (r2) -- node [pos=0.15, anchor=north west] {$10$} (q2);
\draw (r2) -- node [anchor=west] {$11$} (q3);
\end{scope}
\end{tikzpicture}
\end{document}
如果您希望边缘标签自动旋转,可以使用sloped
:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikzstyle{vertex}=[circle, draw]
\begin{tikzpicture}[transform shape]
\node[vertex, label=right:$1$](t) at (4, 0) {$ \omega_{i} $};
\node[vertex, label=above:$2$](r1) at (2, 2) {$ r^{\omega_{i}}_{1} $};
\node[vertex, label=above:$3$](r2) at (6, 2) {$ r^{\omega_{i}}_{2} $};
\node[vertex, label=below:$4$](q1) at (0,-2) {$ q^{\omega_{i}}_{1} $};
\node[vertex, label=below:$5$](q2) at (4,-2) {$ q^{\omega_{i}}_{2} $};
\node[vertex, label=below:$6$](q3) at (8,-2) {$ q^{\omega_{i}}_{2} $};
\begin{scope}[every path/.style={-, dashed}, every node/.style={sloped, inner sep=1pt}]
\draw (t) -- node [anchor=north] {$1$} (q1);
\draw (t) -- node [anchor=south] {$2$} (q2);
\draw (t) -- node [anchor=north] {$3$} (q3);
\draw (t) -- node [anchor=north] {$4$} (r1);
\draw (t) -- node [anchor=north] {$5$} (r2);
\end{scope}
\begin{scope}[every path/.style={-}, every node/.style={sloped, inner sep=1pt}]
\draw (r1) -- node [anchor=south] {$6$} (q1);
\draw (r1) -- node [pos=0.15, anchor=north] {$7$} (q2);
\draw (r1) -- node [pos=0.15, anchor=south] {$8$} (q3);
\draw (r2) -- node [pos=0.15, anchor=south] {$9$} (q1);
\draw (r2) -- node [pos=0.15, anchor=north] {$10$} (q2);
\draw (r2) -- node [anchor=south] {$11$} (q3);
\end{scope}
\end{tikzpicture}
\end{document}