使用tikz的图形中节点之间的不同水平和垂直间距

使用tikz的图形中节点之间的不同水平和垂直间距

我正在绘制一个简单的图形,我想在 x 方向上比在 y 方向上有更大的间距。但是,使用“node distance=4cm”我只能控制距离。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,automata}
\usepackage{adjustbox}

\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=4cm,
semithick]
\tikzstyle{every state}=[circle, black, draw=red, very thick]
\node[state]          (1)       [label=above:$k_1 {=} 10$]              {$1$};
\node[state]         (2) [below of=1]   [label=below:$k_2 {=} -4$]   {$2$};
\node[state]         (3) [ right of=1] [label=above:$k_3 {=} -8$]{$3$};
\node[state]         (4) [below of=3][label=below:$k_4 {=} 6$] {$4$};


\path (1) edge        node [sloped,above] {2, 8, 1} (2)
(1) edge              node[sloped,above,near start]  {0, 2, 7} (4)
(1) edge              node[sloped,above]  {4, 12, 3} (3)

(2) edge              node[sloped,above,near start]  {2, 4} (3)
(2) edge              node[sloped,above ]  {3, 3} (4)

(3) edge              node [sloped,above] {3,2} (4)
;
\end{tikzpicture}
\end{document}

答案1

使用positioningtikzlibrary 语法(below=of 1),并使用node distance=2cm and 6cm(垂直和水平移动):

节点距离

平均能量损失

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, matrix,shapes,arrows,fit,automata}
\usepackage{adjustbox}

\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2cm and 6cm,
semithick]
\tikzstyle{every state}=[circle, black, draw=red, very thick]
\node[state]          (1)       [label=above:$k_1 {=} 10$]              {$1$};
\node[state]         (2) [below=of 1]   [label=below:$k_2 {=} -4$]   {$2$};
\node[state]         (3) [ right=of 1] [label=above:$k_3 {=} -8$]{$3$};
\node[state]         (4) [below=of 3][label=below:$k_4 {=} 6$] {$4$};


\path (1) edge        node [sloped,above] {2, 8, 1} (2)
(1) edge              node[sloped,above,near start]  {0, 2, 7} (4)
(1) edge              node[sloped,above]  {4, 12, 3} (3)

(2) edge              node[sloped,above,near start]  {2, 4} (3)
(2) edge              node[sloped,above ]  {3, 3} (4)

(3) edge              node [sloped,above] {3,2} (4)
;
\end{tikzpicture}
\end{document}

手册 p 243 说明了两个节点距离规范之间的差异,以及如何计算移位差异。

答案2

您可以按如下方式使用 yshift 命令:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,automata}
\usepackage{adjustbox}

\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=4cm,
semithick]
\tikzstyle{every state}=[circle, black, draw=red, very thick]
\node[state]          (1)       [label=above:$k_1 {=} 10$]              {$1$};
\node[state]         (2) [below of=1,yshift=-1cm]   [label=below:$k_2 {=} -4$]   {$2$};
\node[state]         (3) [ right of=1] [label=above:$k_3 {=} -8$]{$3$};
\node[state]         (4) [below of=3,yshift = -1cm][label=below:$k_4 {=} 6$] {$4$};


\path (1) edge        node [sloped,above] {2, 8, 1} (2)
(1) edge              node[sloped,above,near start]  {0, 2, 7} (4)
(1) edge              node[sloped,above]  {4, 12, 3} (3)

(2) edge              node[sloped,above,near start]  {2, 4} (3)
(2) edge              node[sloped,above ]  {3, 3} (4)

(3) edge              node [sloped,above] {3,2} (4)
;
\end{tikzpicture}
\end{document}

相关内容