tikz中节点之间的距离问题

tikz中节点之间的距离问题

我正在尝试将两个节点放置在另一个节点的东北和西北。但是,我遇到了视觉问题。

这是我正在研究的例子。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\begin{document}


\tikzstyle{block} = [rectangle, draw, fill=white!20,  node distance=3cm,
    text width=6em, text centered, rounded corners, minimum height=3em]
\tikzstyle{line} = [draw, very thick, color=black!50, -latex']


\begin{tikzpicture}[scale=2,node distance = 2cm, auto]
    \node [block] (A) {Initial step};
    \node [block, right =of A] (B) {The next step};

    \begin{scope}[node distance=4cm and 5cm]
        \node [block, right = of B, anchor = south east] (C) {The third step};
        \node [block, right = of B, , anchor = north east ] (D) {The fourth step};
    \end{scope}

    \path [line] (A) -- (B);
    \path [line] (B) -- (C);
    \path [line] (B) -- (D);
\end{tikzpicture}
\end{document}

在此处输入图片描述

第一个问题是,如您所见,节点之间的水平距离不同。第二个问题是,我无法添加节点之间的垂直距离,如图所示。

答案1

作为@farahfeza 的补充 回答

  • 我只会定义node distance一次:
    • 图中距离均匀,
    • 更短的代码
  • 已弃用tikzstyle,相反,您应该使用\tikset或在“tikzpicture 选项”中定义样式(如下面的 MWE 中所做的那样)
  • 此外,该库arrows已被弃用。最好使用更强大的 arrows.meta
\documentclass[margin=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning,
                shapes}
                
\begin{document}
    \begin{tikzpicture}[
 node distance = 5mm and 22mm, 
  block/.style = {draw, rounded corners, fill=#1,
                  minimum height=3em, text width=6em, align=center},
block/.default = white,
every edge/.append style = {draw=black!50, thick, -Latex}
                    ]
\node [block] (A) {Initial step};
\node [block, right =of A] (B) {The next step};
\node [block, above right = of B] (C) {The third step};
\node [block, below right = of B] (D) {The fourth step};
%
\path   (A) edge (B)
        (B.+5) edge (C.west) 
        (B.-5) edge (D.west);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

您可以使用 tikz 库定位分别指定相对 x 和 y 位置,如下所示。

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\begin{document}

\tikzstyle{block} = [rectangle, draw, fill=white!20,  node distance=3cm,
    text width=6em, text centered, rounded corners, minimum height=3em]
\tikzstyle{line} = [draw, very thick, color=black!50, -latex']


\begin{tikzpicture}[scale=2,node distance = 2cm, auto]
    \node [block] (A) {Initial step};
    \node [block, right =of A] (B) {The next step};

    \begin{scope}[node distance=4cm and 5cm]
        \node [block, above right= 1cm and 4cm of B, anchor = south east] (C) {The third step};
        \node [block, below right= 1cm and 4cm of B,  anchor = north east] (D) {The fourth step};
    \end{scope}

    \path [line] (A) -- (B);
    \path [line] (B.10) -- (C.180);
    \path [line] (B.-10) -- (D.180);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容