将节点对齐到 \tikz 的中心

将节点对齐到 \tikz 的中心

我正在尝试创建下图:

          |Welcome|
|Low|                   |High|

         |Truth|

          |Lie|

到目前为止,这是我的想法:

在此处输入图片描述

这个想法是,节点Welcome位于页面的中心,节点的两边是Low和。然后,节点和应该再次位于页面的中心。HighTruthLie

这是我的 MWE:

\documentclass[11pt]{report}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\usepackage{pgfplots}

\begin{document}

\tikzstyle{comp1} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=4cm, fill=gray!50]
\tikzstyle{comp2} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=4cm]
\tikzstyle{comp3} = [draw, ellipse, minimum height=1cm, minimum width=4cm, fill=gray!10, text centered]

\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{figure}
\centering

\begin{tikzpicture}[node distance=2cm]
%%% NODES %%%
\node (welcome)     [comp1]                             {Welcome};

\node (low)         [comp2, below of=welcome]           {Low};
\node (high)        [comp2, right of=low, xshift=3cm]   {High};
\node (truth)       [comp3, below of=low]               {Truth};
\node (lie)         [comp3, below of=truth]             {Lie};

%%% ARROWS %%%
\draw [arrow] (welcome) -- (low);
\draw [arrow] (welcome) -- (high);
\draw [arrow] (low) -- (truth);
\draw [arrow] (high) -- (truth);
\draw [arrow] (truth) -- (lie);
\end{tikzpicture}
\end{figure}

\end{document}

我知道这可能有一个简单的解决方案,但我似乎找不到,即使在谷歌搜索后也是如此。所以,有人能帮忙吗?

答案1

您可以添加coordinate below of=welcome然后将其用作参考:

在此处输入图片描述

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{shapes.geometric, arrows}

\begin{document}

\tikzstyle{comp1} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=4cm, fill=gray!50]
\tikzstyle{comp2} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=4cm]
\tikzstyle{comp3} = [draw, ellipse, minimum height=1cm, minimum width=4cm, fill=gray!10, text centered]

\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{figure}
\centering

\begin{tikzpicture}[node distance=2.2cm]
%%% NODES %%%
\node (welcome)     [comp1]                 {Welcome};
\coordinate[below of=welcome] (c);
\node (low)         [comp2, left of=c]      {Low};
\node (high)        [comp2, right of=c]     {High};
\node (truth)       [comp3, below of=c]     {Truth};
\node (lie)         [comp3, below of=truth] {Lie};

%%% ARROWS %%%
\draw [arrow] (welcome) -- (low);
\draw [arrow] (welcome) -- (high);
\draw [arrow] (low) -- (truth);
\draw [arrow] (high) -- (truth);
\draw [arrow] (truth) -- (lie);
\end{tikzpicture}
\end{figure}

\end{document}

更新

一些“标准”改进建议贡萨洛·梅迪纳

  • below=of welcome代替below of=welcome
  • 或者(如果你愿意)更精确below=2.4cm of welcome
  • \tikzset(新语法)代替\tikzstyle(旧语法)

有关的: 应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.geometric, arrows}

\begin{document}

\tikzset{
    comp1/.style={draw, rectangle, rounded corners, minimum height=1cm, minimum width=4cm, fill=gray!50},
    comp2/.style={draw, rectangle, rounded corners, minimum height=1cm, minimum width=4cm},
    comp3/.style={draw, ellipse, minimum height=1cm, minimum width=4cm, fill=gray!10, text centered},
    arrow/.style={thick,->,>=stealth}
}

\begin{figure}
\centering

\begin{tikzpicture}[node distance=2cm and 1cm]
%%% NODES %%%
\node [comp1]                 (welcome) {Welcome};
\coordinate[below=of welcome] (c);
\node [comp2, left=of c]      (low)     {Low};
\node [comp2, right=of c]     (high)    {High};
\node [comp3, below=of c]     (truth)   {Truth};
\node [comp3, below=of truth] (lie)     {Lie};

%%% ARROWS %%%
\draw [arrow] (welcome) -- (low);
\draw [arrow] (welcome) -- (high);
\draw [arrow] (low)     -- (truth);
\draw [arrow] (high)    -- (truth);
\draw [arrow] (truth)   -- (lie);
\end{tikzpicture}
\end{figure}

\end{document}

相关内容