tikzpicture 在流程图中排列框

tikzpicture 在流程图中排列框

我怎样才能让所有相同颜色的盒子整齐地排列?

有没有更好的方法来完成我想做的事情?

这是我第一次尝试 tikz......

谢谢你!

在此处输入图片描述

%% Package for creating diagrams
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}

\tikzstyle{1} = [rectangle, rounded corners, minimum width=2cm, minimum height=1cm,text centered, draw=black, fill=red!30]
\tikzstyle{2} = [rectangle, rounded corners, minimum width=2cm, minimum height=1cm,text centered, draw=black, fill=blue!30]
\tikzstyle{3} = [rectangle, rounded corners, minimum width=2cm, minimum height=1cm,text centered, draw=black, fill=orange!30]
\tikzstyle{4} = [rectangle, rounded corners, minimum width=2cm, minimum height=1cm,text centered, draw=black, fill=green!30]
\tikzstyle{5} = [rectangle, rounded corners, minimum width=2cm, minimum height=1cm,text centered, draw=black, fill=red!]

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


\begin{tikzpicture}

\node (disciplines) [1] {Disciplines};

\node (humanities) [2, right of=disciplines, xshift=2cm, yshift=2.5cm] {Humanities};
\node (visual_arts) [3, right of=humanities, xshift=3cm] {Visual Arts};
\node (applied_arts) [4, right of=visual_arts, xshift=3cm] {Applied Arts};
\node (architecture) [5, right of=applied_arts, xshift=3cm] {Architecture};

\draw [arrow] (disciplines) -- (humanities);
\draw [arrow] (humanities) -- (visual_arts);
\draw [arrow] (visual_arts) -- (applied_arts);
\draw [arrow] (applied_arts) -- (architecture);

\node (social_sciences) [2, right of=disciplines, xshift=2cm] {Social Sciences};
\node (sociology) [5, right of=social_sciences, xshift=9cm, yshift=0.625cm] {Architectural Sociology};
\node (psychology) [3, right of=social_sciences, xshift=3cm, yshift=-0.625cm] {Psychology};
\node (behavioural_sciences) [5, right of=psychology, xshift=6cm] {Behavioural Sciences};

\draw [arrow] (disciplines) -- (social_sciences);
\draw [arrow] (social_sciences) -- (sociology);
\draw [arrow] (social_sciences) -- (psychology);
\draw [arrow] (psychology) -- (behavioural_sciences);

\node (applied_sciences) [2, right of=disciplines, xshift=2cm, yshift=-2.5cm] {Applied Sciences};
\node (engineering_and_tech) [3, right of=applied_sciences, xshift=4cm] {Engineering and technology};
\node (building_physics) [5, right of=engineering_and_tech, xshift=5cm] {Building Physics};

\draw [arrow] (disciplines) -- (applied_sciences);
\draw [arrow] (applied_sciences) -- (engineering_and_tech);
\draw [arrow] (engineering_and_tech) -- (building_physics);

\end{tikzpicture}

答案1

这是一个解决方案matrix of nodes

在此处输入图片描述

注意

  1. 建议使用tikzset而不是tikzstyle。阅读:应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?
  2. text width=3cm设置为使节点具有相同的宽度。
  3. 在第二行添加一个中间节点,以便可以水平绘制指向最后一列的箭头。
  4. 在第二行和第三行进行了一些垂直间距调整,以在最后一列中获得均匀间距的节点。
  5. 给一个最小工作示例尽可能在问题中使用,而不是代码片段。请参阅生成上图的 MWE。

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{
1/.style={fill=red!30},
2/.style={fill=blue!30},
3/.style={fill=orange!30},
4/.style={fill=green!30},
5/.style={fill=red},
arrow/.style={thick,->,>=stealth},
}

\begin{document}
\begin{tikzpicture}
\matrix(m)[matrix of nodes,column sep=1cm, row sep=0.5cm,
nodes={rectangle, rounded corners, text width=3cm, minimum height=1cm,text centered, draw=black,anchor=west},
]{
 & \node[2](humanities){Humanities}; &\node[3](visual_arts){Visual Arts}; & \node[4](applied_arts){Applied Arts}; & \node[5](architecture){Architecture};\\
 &  & \node[draw=none](int){}; & & \node[5](sociology){Architectural Sociology};\\[-0.75cm]
\node[1](disciplines){Disciplines}; &\node[2](social_sciences){Social Sciences}; & & &\\[-0.75cm]
 &   & \node[3](psychology){Psychology}; & & \node[5](behavioural_sciences){Behavioural Sciences};\\
& \node[2](applied_sciences){Applied Science}; & \node[3](engineering_and_tech){Engineering and  technology}; & & \node[5](building_physics){Building Physics};\\
};

\draw [arrow] (disciplines) -- (humanities);
\draw [arrow] (humanities) -- (visual_arts);
\draw [arrow] (visual_arts) -- (applied_arts);
\draw [arrow] (applied_arts) -- (architecture);

\draw [arrow] (disciplines) -- (social_sciences);
\draw [arrow] (social_sciences) --(int.center)-- (sociology);
\draw [arrow] (social_sciences) -- (psychology);
\draw [arrow] (psychology) -- (behavioural_sciences);

\draw [arrow] (disciplines) -- (applied_sciences);
\draw [arrow] (applied_sciences) -- (engineering_and_tech);
\draw [arrow] (engineering_and_tech) -- (building_physics);
\end{tikzpicture}
\end{document}

答案2

这种图称为树,绘制这种图的一个方便的工具是forest,它基于 TitierZ. 通过设置适当的s即可实现对齐。

\documentclass[tikz,border=3mm]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{shadows.blur}
\begin{document}
\begin{forest}
for tree={grow'=0,rounded corners,text centered,draw,
    edge={-stealth,semithick},
    forked edges,
    anchor=center,
    l sep=2em,  
    fork sep=1em,
    font=\sffamily,
    blur shadow,
    text width={width("and technology")},
    minimum height=1cm,
    where level=0{fill=red!30}{},
    where level=1{fill=blue!30}{},
    where level=2{fill=orange!30}{},
    where level=3{fill=green!30}{},
    if n children=0{fill=red,tier=murmel}{}
    }
 [Disciplines
  [Humanities
   [Visual arts
    [Applied arts
     [Architecture]
    ]
   ]
  ]
  [Social sciences
   [Architectural Sociology]
   [Psychology
    [Behavioural Sciences]
   ]
  ]
  [Applied sciences
   [Engineering and technology
    [Building Physics]
   ]
  ]
 ]
\end{forest}
\end{document}

在此处输入图片描述

答案3

您的“流程图”有点不同:

在此处输入图片描述

使用 及其forest选项forked edge、分组s sep距离、节点在西边的锚定。节点的颜色和层选项借鉴了薛定谔的猫的第一个答案版本,节点的样式与@nidhin 的答案类似。MWE 是:

\documentclass[tikz,border=3mm]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={
% nodes style
    draw, rounded corners,
    text centered,
    text width=24mm,
    minimum height=1cm,
    anchor=west,
% tree style
    grow' = 0,
    forked edge,    % for forked edge
    s sep = 4mm,    % "sibling" distance
    l sep = 6mm,    % "level" distance
 fork sep = 3mm,    % distance from parent to branching point
% nodes coloring and grouping 
    where level=0{fill=red!30}{s sep=2mm},
    where level=1{fill=blue!30}{},
    where level=2{fill=orange!30}{},
    where level=3{fill=green!30}{},
    if n children=0{fill=red, tier=level}{},
    }
% tree body
 [Disciplines, calign=center
  [Humanities
   [Visual arts
    [Applied arts
     [Architecture]
    ]
   ]
  ]
  [Social sciences, calign=center
        [Architectural Sociology]
   [Psychology
    [Behavioural Sciences]
   ]
  ]
  [Applied sciences
   [Engineering and technology
    [Building Physics]
   ]
  ]
 ]
\end{forest}
\end{document}

相关内容