如何修改tikz中路径节点的位置

如何修改tikz中路径节点的位置

我用 tikz 创建了下图。

图表

如您所见,路径上的每个数字都很难读取。我想修改路径上数字的位置。我该怎么做?

这是我目前的代码:

 \documentclass[titlepage,10pt, a4paper]{article}
 \usepackage{tikz}
 \usetikzlibrary{arrows,shapes,automata,positioning,calc}

 \begin{document}
 \begin{center}
 \tikzset{
 adim/.style={rectangle,minimum width=50,minimum height= 20,draw,thick},
 nn/.style={rectangle,minimum width=210, minimum height=20,draw,thick},}
 \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=7cm,
 thick,main node/.style={draw,font=\sffamily\Large\bfseries}]
 \node (Z1) [adim] at (-4,4)  {Z$_1$};
 \node (Z2) [adim] at (-1,4) {Z$_2$};
 \node (Z3) [adim] at (2,4)  {Z$_3$};
 \node (Z4) [adim] at (5,4)  {Z$_4$};
  \node (G1) [adim] at (-4,0.5)  {G$_1$};
  \node (G2) [adim] at (-1,0.5) {G$_2$};
  \node (G3) [adim] at (2,0.5)  {G$_3$};
  \node (G4) [adim] at (5,0.5)  {G$_4$};
  \path[every node/.style={font=\sffamily\small}]
      (Z1) edge node[midway, right] {8} (G1)
      (Z2) edge node[midway, right] {3} (G2)
      (Z2) edge node[midway, right] {2} (G3)
      (Z2) edge node[midway, right] {6} (G4)
      (Z3) edge node[above] {4} (G1)
      (Z3) edge node[midway, right] {12} (G2)
      (Z3) edge node[midway, right] {15} (G3)
      (Z4) edge node[midway, right] {3} (G3)
      (Z4) edge node[midway, right] {9} (G4)
 \end{tikzpicture}
 \end{center}
 \end{document}

答案1

您可以使用以下方法pos=来修改位置:

在此处输入图片描述

笔记:

  • 我相信该midway选项是默认选项并且已将其删除,特别是因为我用 覆盖了它pos=

代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}
 \tikzset{
 adim/.style={rectangle,minimum width=50,minimum height= 20,draw,thick},
 nn/.style={rectangle,minimum width=210, minimum height=20,draw,thick},}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=7cm,
 thick,main node/.style={draw,font=\sffamily\Large\bfseries}]
 \node (Z1) [adim] at (-4,4)  {Z$_1$};
 \node (Z2) [adim] at (-1,4) {Z$_2$};
 \node (Z3) [adim] at (2,4)  {Z$_3$};
 \node (Z4) [adim] at (5,4)  {Z$_4$};
  \node (G1) [adim] at (-4,0.5)  {G$_1$};
  \node (G2) [adim] at (-1,0.5) {G$_2$};
  \node (G3) [adim] at (2,0.5)  {G$_3$};
  \node (G4) [adim] at (5,0.5)  {G$_4$};
  \path[every node/.style={font=\sffamily\small}]
      (Z1) edge node[right] {8} (G1)
      (Z2) edge node[left,       pos=0.2] {3}  (G2)
      (Z2) edge node[right,      pos=0.7] {2}  (G3)
      (Z2) edge node[above,      pos=0.9] {6}  (G4)
      (Z3) edge node[above left, pos=0.7] {4}  (G1)
      (Z3) edge node[right,      pos=0.8] {12} (G2)
      (Z3) edge node[right,      pos=0.2] {15} (G3)
      (Z4) edge node[right,      pos=0.4] {3}  (G3)
      (Z4) edge node[right              ] {9}  (G4);
 \end{tikzpicture}

 \end{document}

相关内容