Tikz 使用垂直水平代替水平

Tikz 使用垂直水平代替水平

以下代码:

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{external,arrows,shapes,positioning,shadows,trees}

\tikzset{
  basic/.style  = {draw, drop shadow, rectangle},
  root/.style   = {basic, rounded corners=2pt, thin, align=center,
                   fill=green!30},
  level 2/.style = {basic, rounded corners=6pt, thin,align=center, fill=blue!10},
  level 3/.style = {basic, thin, align=left, fill=green!10}
}

\begin{document}
\begin{tikzpicture}[scale=0.4,
  level 1/.style={sibling distance=500mm},
  level 2/.append style={sibling distance=160mm},
  edge from parent/.style={->,draw},
  >=latex]

% root of the the initial tree, level 1
\node[root] {\Huge Fuselage}
% The first level, as children of the initial tree
  child {node[level 2, yshift=-30pt] (ch1) {\huge Functions}
    child {node[level 2, yshift=-30pt] (c1) {\Large Shell containing payload}}
    child {node[level 2, yshift=-30pt] (c2) {\Large Protection against climate}}
    child {node[level 2, yshift=-30pt] (c3) {\Large Central structural member}}
    child {node[level 2, yshift=-30pt] (c4) {\Large Houses aircraft systems}}
  }
  child {node[level 2, yshift=-30pt] (ch2) {\huge Requirements}
    child {node[level 2, yshift=-30pt] (c5) {\Large Low drag}}
    child {node[level 2, yshift=-30pt] (c6) {\Large Structural}}
    child {node[level 2, yshift=-30pt] (c7) {\Large Costs}}
  };

% The second level, relatively positioned nodes
\begin{scope}[every node/.style={level 3}]
\node [below = of  c1, xshift=60pt] (c11) {Rapid loading Huge and unloading};

\node [below = of  c2, xshift=30pt] (c21) {Low temperatures};
\node [below = of  c21] (c22) {Low pressures};
\node [below = of  c22] (c23) {High wind speeds};

\node [below = of  c4, xshift=20pt] (c41) {Electrical};
\node [below = of  c41] (c42) {Undercarriage};

\node [below = of  c6, xshift=20pt] (c61) {Strong};
\node [below = of  c61] (c62) {Rigid};
\node [below = of  c62] (c63) {Light};
\node [below = of  c63] (c64) {Fixed useful life};

\node [below = of  c7, xshift=75pt] (c71) { Minimise operating costs};
\node [below = of  c71] (c72) {Maximise earning capacity};

\end{scope}

% lines from each level 1 node to every one of its "children"
\foreach \value in {1,...,1}
  \draw[->] (c1.195) |- (c1\value.west);

\foreach \value in {1,...,3}
  \draw[->] (c2.195) |- (c2\value.west);

\foreach \value in {1,...,2}
  \draw[->] (c4.195) |- (c4\value.west);

\foreach \value in {1,...,4}
  \draw[->] (c6.195) |- (c6\value.west);

\foreach \value in {1,...,2}
  \draw[->] (c7.195) |- (c7\value.west);

\end{tikzpicture}
\end{document}

生成此树形图:

在此处输入图片描述

请注意,第三层是水平展开的。

有没有办法让第三层也像第四层一样垂直分布?我想要的结果应该有这种结构:

在此处输入图片描述

答案1

此解决方案用于forest移动事物。所有这些都打包成一种样式,可以非常简洁地指定树本身。

方向开关样式

树本身以括号语法指定:

  [Fuselage
    [Functions
      [Shell containing payload
        [Rapid loading and unloading
        ]
      ]
      [Protection against climate
        [Low temperatures
        ]
        [Low pressures
        ]
        [High wind speeds
        ]
      ]
      [Central structural member
      ]
      [Houses aircraft systems
        [Electrical
        ]
        [Undercarriage
        ]
      ]
    ]
    [Requirements
      [Low Drag
      ]
      [Structural
        [Strong
        ]
        [Rigid
        ]
        [Light
        ]
        [Fixed useful life
        ]
      ]
      [Costs
        [Minimise operating costs
        ]
        [Maximise earning capacity
        ]
      ]
    ]
  ]

一旦你习惯了它,它显然会非常高效,但一开始看起来有点陌生。关于这个语法的简单介绍以及一些forest亮点,请参阅第二部分我对另一个问题的回答

\PassOptionsToPackage{rgb,svgnames,dvipsnames,x11names}{xcolor}
\documentclass[border=5pt,tikz]{standalone}
\usepackage{forest}
\forestset{% this specifies a style, direction switch, which manages the layout, formatting and tweaking of the various levels and nodes of the tree itself
  direction switch/.style={
    for tree={
      draw=DodgerBlue2,
      thick,
      inner ysep=2pt,
      edge={thick},
      if level=1{% the root node is level 0 so this is the layer of horizontally aligned nodes beneath it
        child anchor=north,
        edge path={
          \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- ++(0,-.5em) -| (.child anchor)\forestoption{edge label};
        },
        for descendants={% apply to everything beneath level 1
          child anchor=west,
          align=left,
          edge path={
            \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) ++(1em,0) |- (.child anchor)\forestoption{edge label};
          },
          s sep+=2.5pt,
          fit=band,
        },
        for tree={% apply to everything in level 1 or beneath level 1
          parent anchor=south west,
          anchor=mid west,
          grow'=0,
          font=\sffamily,
          if n children=0{}{
            delay={
              prepend={[,phantom, calign with current]}
            }
          },
          before computing xy={
            l=2em
          }
        },
      }{
        if level=0{% this is the root node of the tree, level 0
          parent anchor=south,
          anchor=south,
          before drawing tree={% we repack just the top bit of the tree, right at the end, to account for the increased spacing introduced by moving things around; after repacking, we need to explicitly recompute the x-y coordinates as well, before the tree is drawn
            pack',
            compute xy,
          }
        }{},
      },
    },
  }
}

\begin{document}
\begin{forest}
  % forest preamble: determine layout and format of tree
  direction switch% apply the style defined above
  % specify the content of the tree
  [Fuselage
    [Functions
      [Shell containing payload
        [Rapid loading and unloading
        ]
      ]
      [Protection against climate
        [Low temperatures
        ]
        [Low pressures
        ]
        [High wind speeds
        ]
      ]
      [Central structural member
      ]
      [Houses aircraft systems
        [Electrical
        ]
        [Undercarriage
        ]
      ]
    ]
    [Requirements
      [Low Drag
      ]
      [Structural
        [Strong
        ]
        [Rigid
        ]
        [Light
        ]
        [Fixed useful life
        ]
      ]
      [Costs
        [Minimise operating costs
        ]
        [Maximise earning capacity
        ]
      ]
    ]
  ]
\end{forest}
\end{document}

相关内容