向 tikz 添加超过 9 个节点

向 tikz 添加超过 9 个节点

我是 tikz 的绝对初学者,我修改了一个例子https://texample.net/tikz/examples/work-breakdown-structure/

我的问题:我不知道如何将箭头与最后 3 个字段连接起来。

以下是 MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning,shadows,trees}

\tikzset{
  basic/.style  = {draw, text width=2cm, drop shadow, font=\sffamily, rectangle},
  root/.style   = {basic, rounded corners=2pt, thin, align=center,
                   fill=green!30},
  level 2/.style = {basic, rounded corners=6pt, thin,align=center, fill=green!60,
                   text width=8em},
  level 3/.style = {basic, thin, align=left, fill=pink!60, text width=6.5em}
}

\begin{document}
\begin{tikzpicture}[
  level 1/.style={sibling distance=40mm},
  edge from parent/.style={->,draw},
  >=latex]

% root of the the initial tree, level 1
\node[root] {Drawing diagrams}
% The first level, as children of the initial tree
  child {node[level 2] (c1) {Defining node and arrow styles}}
  child {node[level 2] (c2) {Positioning the nodes}}
  child {node[level 2] (c3) {Drawing arrows between nodes}};

% The second level, relatively positioned nodes
\begin{scope}[every node/.style={level 3}]
\node [below of = c1, xshift=15pt] (c11) {Setting shape};
\node [below of = c11] (c12) {Choosing color};
\node [below of = c12] (c13) {Adding shading};

\node [below of = c2, xshift=15pt] (c21) {Using a Matrix};
\node [below of = c21] (c22) {Relatively};
\node [below of = c22] (c23) {Absolutely};
\node [below of = c23] (c24) {Using overlays};
\node [below of = c24] (c25) {Bending};
\node [below of = c25] (c26) {Resizing tips};
\node [below of = c26] (c27) {Shortening};
\node [below of = c27] (c28) {Bending};
\node [below of = c28] (c29) {Arrow library};
\node [below of = c29] (c30) {It};
\node [below of = c30] (c31) {does not};
\node [below of = c31] (c32) {work};

\node [below of = c3, xshift=15pt] (c31) {Default arrows};
\node [below of = c31] (c32) {Arrow library};
\node [below of = c32] (c33) {Resizing tips};
\node [below of = c33] (c34) {Shortening};
\node [below of = c34] (c35) {Bending};\node [below of = c31] (c32) {Arrow library};
\node [below of = c35] (c36) {Resizing tips};
\node [below of = c36] (c37) {Shortening};
\node [below of = c37] (c38) {Bending};
\node [below of = c38] (c39) {Arrow library};
\end{scope}

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

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

\foreach \value in {1,...,9}
  \draw[->] (c3.195) |- (c3\value.west);
\end{tikzpicture}
\end{document}

答案1

你曾料到过这样的事情吗?

在此处输入图片描述

使用该forest包可以非常轻松地绘制此树形图。代码简短易懂:

%\documentclass{article}
\documentclass[border=3.141592mm]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{forest}
for tree = {
    draw,
    font=\small\linespread{0.84}\selectfont,
    text width=7em,
    text centered,
    calign=edge midpoint,
    edge = {-Stealth},
if level = 0{fill=teal!10}{},
if level>= 1{grow'=0,
             folder,
             folder indent=4mm,
             l sep=7mm,
             s sep=2mm,
             fill=red!20}{},
if level = 1{fill=teal!20}{},
            }
[Drawing diagrams 
    [Defining node and arrow styles
        [Setting shape]
        [Choosing color]
        [Adding shading]
    ]
    [Positioning the nodes
        [Using a Matrix]
        [Relatively]
        [Absolutely]
        [Using overlays]
        [Bending]
        [Resizing tips]
        [Shortening]
        [Bending]
        [Arrow library]
        [It]
        [does]
        [work]
    ]
    [Drawing arrows between nodes
        [Default arrows]
        [Arrow library]
        [Resizing tips]
        [Shortening]
        [Bending]
        [Resizing tips]
        [Shortening]
        [Bending]
        [Arrow library]
    ]
]
    \end{forest}
\end{document}

附录: 如果红色节点(它们位于图的第 2 级,根位于第 0 级)中的文本必须左对齐,则forest设置应为:

for tree = {
    draw,
    font=\small\linespread{0.84}\selectfont,
    text width=7em,
    calign=edge midpoint,
    edge = {-Stealth},
if level = 0{fill=teal!10}{},
if level>= 1{grow'=0,
             folder,
             folder indent=4mm,
             l sep=7mm,
             s sep=2mm,
             fill=red!20}{},
if level = 1{fill=teal!20}{},
if level = 2{align=left}{text centered}, % <--- added
            }

与前一个相比,我们看到˙文本居中is removed from common settings and changed add as new style for level 2 (in code marked by<---添加了'):

在此处输入图片描述

答案2

我认为这解决了你的问题

\begin{document}
\begin{tikzpicture}[
    level 1/.style={sibling distance=40mm},
    edge from parent/.style={->,draw},
    >=latex]
    
    % root of the the initial tree, level 1
    \node[root] {Drawing diagrams}
    % The first level, as children of the initial tree
    child {node[level 2] (c1) {Defining node and arrow styles}}
    child {node[level 2] (c2) {Positioning the nodes}}
    child {node[level 2] (c3) {Drawing arrows between nodes}};
    
    % The second level, relatively positioned nodes
    \begin{scope}[every node/.style={level 3}]
        \node [below of = c1, xshift=15pt] (c11) {Setting shape};
        \node [below of = c11] (c12) {Choosing color};
        \node [below of = c12] (c13) {Adding shading};
        
        \node [below of = c2, xshift=15pt] (c21) {Using a Matrix};
        \node [below of = c21] (c22) {Relatively};
        \node [below of = c22] (c23) {Absolutely};
        \node [below of = c23] (c24) {Using overlays};
        \node [below of = c24] (c25) {Bending};
        \node [below of = c25] (c26) {Resizing tips};
        \node [below of = c26] (c27) {Shortening};
        \node [below of = c27] (c28) {Bending};
        \node [below of = c28] (c29) {Arrow library};
        \node [below of = c29] (c40) {It};
        \node [below of = c40] (c41) {does not};
        \node [below of = c41] (c42) {work};
        
        \node [below of = c3, xshift=15pt] (c31) {Default arrows};
        \node [below of = c31] (c32) {Arrow library};
        \node [below of = c32] (c33) {Resizing tips};
        \node [below of = c33] (c34) {Shortening};
        \node [below of = c34] (c35) {Bending};\node [below of = c31] (c32) {Arrow library};
        \node [below of = c35] (c36) {Resizing tips};
        \node [below of = c36] (c37) {Shortening};
        \node [below of = c37] (c38) {Bending};
        \node [below of = c38] (c39) {Arrow library};
    \end{scope}
    
    % lines from each level 1 node to every one of its "children"
    \foreach \value in {1,2,3}
    \draw[->] (c1.195) |- (c1\value.west);
    
    \foreach \value in {0,1,2}
    \draw[->] (c2.195) |- (c4\value.west);
    
    \foreach \value in {1,...,9}
    \draw[->] (c2.195) |- (c2\value.west);
    
    \foreach \value in {1,...,9}
    \draw[->] (c3.195) |- (c3\value.west);
\end{tikzpicture}

\结束{文档}

相关内容