使用 Forest 包

使用 Forest 包

我正在尝试使用 Forest 包获取这样的层次结构图

在此处输入图片描述

但是,无法做到这一点。我使用了这个代码

\documentclass[border=20pt,tikz]{standalone}
\usepackage[edges]{forest}

\forestset{
  direction switch/.style={
    for tree={edge+=thick, font=\sffamily},
    where level>=1{folder, grow'=0}{for children=forked edge},
    where level=3{}{draw},
  },
}
\begin{document}

\begin{forest}
  % forest preamble: determine layout and format of tree
  direction switch
  [Main
  [1.1]
  [1.2]
  [1.3]
  [1.4]
  [2
   [2.1]
   [2.2]
  ]
    [3
   [3.1]
   [3.2]
  ]
    [4
   [4.1]
   [4.2]
  ]
  ]
\end{forest}
\end{document} 

并得到了 在此处输入图片描述

在此先感谢您的帮助。

答案1

无需以下操作的手动解决方案forest

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usepackage{xcolor}

\definecolor{MyBlue}{RGB}{68,114,196}

\newlength\MyBoxWidth
\settowidth{\MyBoxWidth}{\sffamily Main}

\tikzset{
  MyBox/.style={
    fill=MyBlue,
    font=\color{white}\sffamily,
    minimum width=\MyBoxWidth,
  },
}

\begin{document}
\begin{tikzpicture}
  \path[every node/.style={MyBox}]
    node (Main) {Main}
    node[
      below=1em of Main,
      anchor=north east,
      xshift=-.5em,
    ] (1_1) {1.1}
    node[
      below=1em of Main,
      anchor=north west,
      xshift=.5em,
    ] (1_2) {1.2}
    node[below=1em of 1_1] (1_3) {1.3}
    node[below=1em of 1_2] (1_4) {1.4}
    ($(1_3.south east)!.5!(1_4.south west)$) ++ (0, -.5em) coordinate (3_top)
    node[below=.5em of 3_top, anchor=north] (3) {3}
    node[left=1em of 3] (2) {2}
    node[right=1em of 3] (4) {4}
    (2.south west) ++(.5em, 0) coordinate (2_bot)
    (3.south west) ++(.5em, 0) coordinate (3_bot)
    (4.south west) ++(.5em, 0) coordinate (4_bot)
    node[
      below=1em of 2_bot,
      anchor=north west,
      xshift=.5em,
    ] (2_1) {2.1}
    node[below=1em of 2_1] (2_2) {2.2}
    node[
      below=1em of 3_bot,
      anchor=north west,
      xshift=.5em,
    ] (3_1) {3.1}
    node[below=1em of 3_1] (3_2) {3.2}
    node[
      below=1em of 4_bot,
      anchor=north west,
      xshift=.5em,
    ] (4_1) {4.1}
    node[below=1em of 4_1] (4_2) {4.2}
    %
    (Main) edge (3)
    (1_1) edge (1_2)
    (1_3) edge (1_4)
  ;
  \draw (2) |- (4 |- 3_top) -- (4);
  \draw (2_bot) |- (2_2);
  \draw (3_bot) |- (3_2);
  \draw (4_bot) |- (4_2);
  \draw (2_bot |- 2_1) -- (2_1);
  \draw (3_bot |- 3_1) -- (3_1);
  \draw (4_bot |- 4_1) -- (4_1);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容